diff --git a/internal/provider/data_source_archive_file.go b/internal/provider/data_source_archive_file.go index fb95166b..b9e9cad1 100644 --- a/internal/provider/data_source_archive_file.go +++ b/internal/provider/data_source_archive_file.go @@ -11,6 +11,8 @@ import ( "io/ioutil" "os" "path" + "path/filepath" + "strconv" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-archive/internal/hashcode" @@ -77,6 +79,12 @@ func dataSourceFile() *schema.Resource { ForceNew: true, ConflictsWith: []string{"source_content", "source_content_filename", "source_file"}, }, + "mode": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ConflictsWith: []string{"source_content", "source_content_filename"}, + }, "excludes": { Type: schema.TypeSet, Optional: true, @@ -172,6 +180,9 @@ func archive(d *schema.ResourceData) error { } if dir, ok := d.GetOk("source_dir"); ok { + if mode, ok := d.GetOk("mode"); ok { + chmod(dir.(string), mode.(int)) + } if excludes, ok := d.GetOk("excludes"); ok { excludeList := expandStringList(excludes.(*schema.Set).List()) @@ -184,6 +195,9 @@ func archive(d *schema.ResourceData) error { } } } else if file, ok := d.GetOk("source_file"); ok { + if mode, ok := d.GetOk("mode"); ok { + chmod(file.(string), mode.(int)) + } if err := archiver.ArchiveFile(file.(string)); err != nil { return fmt.Errorf("error archiving file: %s", err) } @@ -228,3 +242,23 @@ func genFileShas(filename string) (string, string, string, error) { return sha1, sha256base64, md5Sum, nil } + +func chmod(path string, mode int) error { + modeDecimal, err := strconv.ParseInt(strconv.Itoa(mode), 8, 64) + if err != nil { + return err + } + + return filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + err = os.Chmod(path, os.FileMode(modeDecimal)) + if err != nil { + return err + } + + return nil + }) +} diff --git a/website/docs/d/archive_file.html.markdown b/website/docs/d/archive_file.html.markdown index 48c9c503..13153e59 100644 --- a/website/docs/d/archive_file.html.markdown +++ b/website/docs/d/archive_file.html.markdown @@ -66,6 +66,8 @@ NOTE: One of `source`, `source_content_filename` (with `source_content`), `sourc * `source_dir` - (Optional) Package entire contents of this directory into the archive. +* `mode` - (Optional) Recursively set `source_file` or `source_dir` permissions to this before packaging. + * `source` - (Optional) Specifies attributes of a single source file to include into the archive. * `excludes` - (Optional) Specify files to ignore when reading the `source_dir`.