Skip to content

Commit c03e0a6

Browse files
bendbennettmoajo
andauthored
Follow symlinks when creating archive (#183)
* Follow symlinks when creating archive (#6) Co-authored-by: moajo <[email protected]> * Adding changie entries (#6) * Modifying changie entries (#6) * Switching to using filepath.EvalSymlinks and adding acceptance test coverage (#6) * Removing unused constant (#6) * Checking usage of absolute file path in test (#6) * Adding further symlink acceptance tests to archive_file data source and resource (#6) * Cleaning up test fixtures (#6) * Adding symlink dir in regular dir for test fixtures (#6) * Calling ToSlash() before Abs() (#6) * Only call ToSlash() if not using Abs() (#6) * Calling ToSlash() after calling Abs() (#6) * Amending final test step in TestAccArchiveFile_Symlinks to use ToSlash() (#6) * Adding test for multiple directories and files (#6) * Fix resource test (#6) * Adding optional attribute for following symlinks (#6) * Modifying tests to pass on windows (#6) * Altering attribute name to exclude_symlink_directories, which default to false (#6) * Updating attribute description and regenerating the documentation (#6) * Updating change log entries (#6) * Renaming tests (#6) * Separating each of the acceptance tests for symlinks into individual tests (#6) --------- Co-authored-by: moajo <[email protected]>
1 parent 6b6d7b7 commit c03e0a6

23 files changed

+1776
-66
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: ENHANCEMENTS
2+
body: 'data-source/archive_file: Added attribute `exclude_symlink_directories` which
3+
will exclude symbolically linked directories from the archive when set to true.
4+
Defaults to false'
5+
time: 2023-06-06T06:58:24.84474+01:00
6+
custom:
7+
Issue: "183"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: ENHANCEMENTS
2+
body: 'resource/archive_file: Added attribute `exclude_symlink_directories` which
3+
will exclude symbolically linked directories from the archive when set to true.
4+
Defaults to false'
5+
time: 2023-06-06T06:58:47.674171+01:00
6+
custom:
7+
Issue: "183"

docs/data-sources/file.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ data "archive_file" "lambda_my_function" {
6969

7070
### Optional
7171

72+
- `exclude_symlink_directories` (Boolean) Boolean flag indicating whether symbolically linked directories should be excluded during the creation of the archive. Defaults to false.
7273
- `excludes` (Set of String) Specify files to ignore when reading the `source_dir`.
7374
- `output_file_mode` (String) String that specifies the octal file mode for all archived files. For example: `"0666"`. Setting this will ensure that cross platform usage of this module will not vary the modes of archived files (and ultimately checksums) resulting in more deterministic behavior.
7475
- `source` (Block Set) Specifies attributes of a single source file to include into the archive. One and only one of `source`, `source_content_filename` (with `source_content`), `source_file`, or `source_dir` must be specified. (see [below for nested schema](#nestedblock--source))

docs/resources/file.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ description: |-
2222

2323
### Optional
2424

25+
- `exclude_symlink_directories` (Boolean) Boolean flag indicating whether symbolically linked directories should be excluded during the creation of the archive. Defaults to false.
2526
- `excludes` (Set of String) Specify files to ignore when reading the `source_dir`.
2627
- `output_file_mode` (String) String that specifies the octal file mode for all archived files. For example: `"0666"`. Setting this will ensure that cross platform usage of this module will not vary the modes of archived files (and ultimately checksums) resulting in more deterministic behavior.
2728
- `source` (Block Set) Specifies attributes of a single source file to include into the archive. One and only one of `source`, `source_content_filename` (with `source_content`), `source_file`, or `source_dir` must be specified. (see [below for nested schema](#nestedblock--source))

internal/provider/archiver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import (
55
"os"
66
)
77

8+
type ArchiveDirOpts struct {
9+
Excludes []string
10+
ExcludeSymlinkDirectories bool
11+
}
12+
813
type Archiver interface {
914
ArchiveContent(content []byte, infilename string) error
1015
ArchiveFile(infilename string) error
11-
ArchiveDir(indirname string, excludes []string) error
16+
ArchiveDir(indirname string, opts ArchiveDirOpts) error
1217
ArchiveMultiple(content map[string][]byte) error
1318
SetOutputFileMode(outputFileMode string)
1419
}

internal/provider/data_source_archive_file.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ func (d *archiveFileDataSource) Schema(ctx context.Context, req datasource.Schem
142142
),
143143
},
144144
},
145+
"exclude_symlink_directories": schema.BoolAttribute{
146+
Optional: true,
147+
Description: "Boolean flag indicating whether symbolically linked directories should be excluded during " +
148+
"the creation of the archive. Defaults to false.",
149+
},
145150
"output_path": schema.StringAttribute{
146151
Description: "The output of the archive file.",
147152
Required: true,
@@ -211,7 +216,15 @@ func archive(ctx context.Context, model fileModel) error {
211216
}
212217
}
213218

214-
if err := archiver.ArchiveDir(model.SourceDir.ValueString(), excludeList); err != nil {
219+
opts := ArchiveDirOpts{
220+
Excludes: excludeList,
221+
}
222+
223+
if !model.ExcludeSymlinkDirectories.IsNull() {
224+
opts.ExcludeSymlinkDirectories = model.ExcludeSymlinkDirectories.ValueBool()
225+
}
226+
227+
if err := archiver.ArchiveDir(model.SourceDir.ValueString(), opts); err != nil {
215228
return fmt.Errorf("error archiving directory: %s", err)
216229
}
217230
case !model.SourceFile.IsNull():
@@ -309,23 +322,24 @@ func (d *archiveFileDataSource) Metadata(_ context.Context, req datasource.Metad
309322
}
310323

311324
type fileModel struct {
312-
ID types.String `tfsdk:"id"`
313-
Source types.Set `tfsdk:"source"` // sourceModel
314-
Type types.String `tfsdk:"type"`
315-
SourceContent types.String `tfsdk:"source_content"`
316-
SourceContentFilename types.String `tfsdk:"source_content_filename"`
317-
SourceFile types.String `tfsdk:"source_file"`
318-
SourceDir types.String `tfsdk:"source_dir"`
319-
Excludes types.Set `tfsdk:"excludes"`
320-
OutputPath types.String `tfsdk:"output_path"`
321-
OutputSize types.Int64 `tfsdk:"output_size"`
322-
OutputFileMode types.String `tfsdk:"output_file_mode"`
323-
OutputMd5 types.String `tfsdk:"output_md5"`
324-
OutputSha types.String `tfsdk:"output_sha"`
325-
OutputSha256 types.String `tfsdk:"output_sha256"`
326-
OutputBase64Sha256 types.String `tfsdk:"output_base64sha256"`
327-
OutputSha512 types.String `tfsdk:"output_sha512"`
328-
OutputBase64Sha512 types.String `tfsdk:"output_base64sha512"`
325+
ID types.String `tfsdk:"id"`
326+
Source types.Set `tfsdk:"source"` // sourceModel
327+
Type types.String `tfsdk:"type"`
328+
SourceContent types.String `tfsdk:"source_content"`
329+
SourceContentFilename types.String `tfsdk:"source_content_filename"`
330+
SourceFile types.String `tfsdk:"source_file"`
331+
SourceDir types.String `tfsdk:"source_dir"`
332+
Excludes types.Set `tfsdk:"excludes"`
333+
ExcludeSymlinkDirectories types.Bool `tfsdk:"exclude_symlink_directories"`
334+
OutputPath types.String `tfsdk:"output_path"`
335+
OutputSize types.Int64 `tfsdk:"output_size"`
336+
OutputFileMode types.String `tfsdk:"output_file_mode"`
337+
OutputMd5 types.String `tfsdk:"output_md5"`
338+
OutputSha types.String `tfsdk:"output_sha"`
339+
OutputSha256 types.String `tfsdk:"output_sha256"`
340+
OutputBase64Sha256 types.String `tfsdk:"output_base64sha256"`
341+
OutputSha512 types.String `tfsdk:"output_sha512"`
342+
OutputBase64Sha512 types.String `tfsdk:"output_base64sha512"`
329343
}
330344

331345
type sourceModel struct {

0 commit comments

Comments
 (0)