Skip to content

Commit af9bceb

Browse files
committed
Add archive_dir property to be used with source_dir that will prefix all content
1 parent fea779a commit af9bceb

File tree

6 files changed

+42
-13
lines changed

6 files changed

+42
-13
lines changed

internal/provider/archiver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
type Archiver interface {
99
ArchiveContent(content []byte, infilename string) error
1010
ArchiveFile(infilename string) error
11-
ArchiveDir(indirname string, excludes []string) error
11+
ArchiveDir(indirname string, excludes []string, archiveDirs string) error
1212
ArchiveMultiple(content map[string][]byte) error
1313
SetOutputFileMode(outputFileMode string)
1414
}

internal/provider/data_source_archive_file.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ func dataSourceFile() *schema.Resource {
7777
ForceNew: true,
7878
ConflictsWith: []string{"source_content", "source_content_filename", "source_file"},
7979
},
80+
"archive_dir": {
81+
Type: schema.TypeString,
82+
Optional: true,
83+
ForceNew: true,
84+
},
8085
"excludes": {
8186
Type: schema.TypeSet,
8287
Optional: true,
@@ -181,15 +186,18 @@ func archive(d *schema.ResourceData) error {
181186
archiver.SetOutputFileMode(outputFileMode)
182187
}
183188

189+
archiveDirs := d.Get("archive_dir").(string)
190+
184191
if dir, ok := d.GetOk("source_dir"); ok {
192+
185193
if excludes, ok := d.GetOk("excludes"); ok {
186194
excludeList := expandStringList(excludes.(*schema.Set).List())
187195

188-
if err := archiver.ArchiveDir(dir.(string), excludeList); err != nil {
196+
if err := archiver.ArchiveDir(dir.(string), excludeList, archiveDirs); err != nil {
189197
return fmt.Errorf("error archiving directory: %s", err)
190198
}
191199
} else {
192-
if err := archiver.ArchiveDir(dir.(string), []string{""}); err != nil {
200+
if err := archiver.ArchiveDir(dir.(string), []string{""}, archiveDirs); err != nil {
193201
return fmt.Errorf("error archiving directory: %s", err)
194202
}
195203
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is sub-dir1/file4.txt

internal/provider/zip_archiver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
type ZipArchiver struct {
1515
filepath string
16+
archiveDirs string
1617
outputFileMode string // Default value "" means unset
1718
filewriter *os.File
1819
writer *zip.Writer
@@ -94,7 +95,7 @@ func checkMatch(fileName string, excludes []string) (value bool) {
9495
return false
9596
}
9697

97-
func (a *ZipArchiver) ArchiveDir(indirname string, excludes []string) error {
98+
func (a *ZipArchiver) ArchiveDir(indirname string, excludes []string, archiveDirs string) error {
9899
_, err := assertValidDir(indirname)
99100
if err != nil {
100101
return err
@@ -142,7 +143,7 @@ func (a *ZipArchiver) ArchiveDir(indirname string, excludes []string) error {
142143
if err != nil {
143144
return fmt.Errorf("error creating file header: %s", err)
144145
}
145-
fh.Name = filepath.ToSlash(relname)
146+
fh.Name = filepath.Join(archiveDirs, filepath.ToSlash(relname))
146147
fh.Method = zip.Deflate
147148
// fh.Modified alone isn't enough when using a zero value
148149
fh.SetModTime(time.Time{})

internal/provider/zip_archiver_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,34 +99,51 @@ func TestZipArchiver_FileModified(t *testing.T) {
9999
func TestZipArchiver_Dir(t *testing.T) {
100100
zipfilepath := "archive-dir.zip"
101101
archiver := NewZipArchiver(zipfilepath)
102-
if err := archiver.ArchiveDir("./test-fixtures/test-dir", []string{""}); err != nil {
102+
if err := archiver.ArchiveDir("./test-fixtures/test-dir", []string{""}, ""); err != nil {
103103
t.Fatalf("unexpected error: %s", err)
104104
}
105105

106106
ensureContents(t, zipfilepath, map[string][]byte{
107-
"file1.txt": []byte("This is file 1"),
108-
"file2.txt": []byte("This is file 2"),
109-
"file3.txt": []byte("This is file 3"),
107+
"file1.txt": []byte("This is file 1"),
108+
"file2.txt": []byte("This is file 2"),
109+
"file3.txt": []byte("This is file 3"),
110+
"sub-dir1/file4.txt": []byte("This is sub-dir1/file4.txt"),
111+
})
112+
}
113+
114+
func TestZipArchiver_Dir_WithArchiveDirs(t *testing.T) {
115+
zipfilepath := "archive-dir.zip"
116+
archiver := NewZipArchiver(zipfilepath)
117+
if err := archiver.ArchiveDir("./test-fixtures/test-dir", []string{""}, "root/test-dir"); err != nil {
118+
t.Fatalf("unexpected error: %s", err)
119+
}
120+
121+
ensureContents(t, zipfilepath, map[string][]byte{
122+
"root/test-dir/file1.txt": []byte("This is file 1"),
123+
"root/test-dir/file2.txt": []byte("This is file 2"),
124+
"root/test-dir/file3.txt": []byte("This is file 3"),
125+
"root/test-dir/sub-dir1/file4.txt": []byte("This is sub-dir1/file4.txt"),
110126
})
111127
}
112128

113129
func TestZipArchiver_Dir_Exclude(t *testing.T) {
114130
zipfilepath := "archive-dir.zip"
115131
archiver := NewZipArchiver(zipfilepath)
116-
if err := archiver.ArchiveDir("./test-fixtures/test-dir", []string{"file2.txt"}); err != nil {
132+
if err := archiver.ArchiveDir("./test-fixtures/test-dir", []string{"file2.txt"}, "root"); err != nil {
117133
t.Fatalf("unexpected error: %s", err)
118134
}
119135

120136
ensureContents(t, zipfilepath, map[string][]byte{
121-
"file1.txt": []byte("This is file 1"),
122-
"file3.txt": []byte("This is file 3"),
137+
"root/file1.txt": []byte("This is file 1"),
138+
"root/file3.txt": []byte("This is file 3"),
139+
"root/sub-dir1/file4.txt": []byte("This is sub-dir1/file4.txt"),
123140
})
124141
}
125142

126143
func TestZipArchiver_Dir_Exclude_With_Directory(t *testing.T) {
127144
zipfilepath := "archive-dir.zip"
128145
archiver := NewZipArchiver(zipfilepath)
129-
if err := archiver.ArchiveDir("./test-fixtures/", []string{"test-dir", "test-dir2/file2.txt"}); err != nil {
146+
if err := archiver.ArchiveDir("./test-fixtures/", []string{"test-dir", "test-dir2/file2.txt"}, ""); err != nil {
130147
t.Fatalf("unexpected error: %s", err)
131148
}
132149

website/docs/d/archive_file.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ NOTE: One of `source`, `source_content_filename` (with `source_content`), `sourc
7878

7979
* `source_dir` - (Optional) Package entire contents of this directory into the archive.
8080

81+
* `archive_dir` - (Optional) When used with source_dir, the top-level directories inside the archive that will contain all the contents
82+
8183
* `source` - (Optional) Specifies attributes of a single source file to include into the archive.
8284

8385
* `excludes` - (Optional) Specify files to ignore when reading the `source_dir`.

0 commit comments

Comments
 (0)