Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added 'mode' argument #80

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions internal/provider/data_source_archive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())

Expand All @@ -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)
}
Expand Down Expand Up @@ -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
})
}
2 changes: 2 additions & 0 deletions website/docs/d/archive_file.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down