Skip to content

Commit

Permalink
keep permissions when running gotpl (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
belitre authored Mar 4, 2019
1 parent 55bf9ad commit b2057a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Version should be updated each time there is a new release
var (
Version = "v0.6"
Version = "v0.7"
GitCommit = ""
)

Expand Down
11 changes: 9 additions & 2 deletions tpl/tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func executeTemplates(values map[string]interface{}, tplFileNames []string, isSt
return "", err
}
if len(tmpDir) > 0 {
err = saveFile(path.Join(tmpDir, f.dest), r)
err = saveFile(path.Join(tmpDir, f.dest), r, f.perm)
if err != nil {
return "", err
}
Expand All @@ -108,7 +108,7 @@ func executeTemplates(values map[string]interface{}, tplFileNames []string, isSt
return result, nil
}

func saveFile(path string, contents string) error {
func saveFile(path string, contents string, perm os.FileMode) error {
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return err
}
Expand All @@ -120,6 +120,10 @@ func saveFile(path string, contents string) error {

defer f.Close()

if err = f.Chmod(perm); err != nil {
return err
}

if _, err = f.WriteString(contents); err != nil {
return err
}
Expand All @@ -130,6 +134,7 @@ func saveFile(path string, contents string) error {
type SrcDest struct {
src string
dest string
perm os.FileMode
}

// ParseTemplate reads YAML or JSON documents from valueFiles, and extra values
Expand Down Expand Up @@ -173,6 +178,7 @@ func getListFiles(tplFileNames []string) ([]*SrcDest, error) {
srcdst := &SrcDest{
src: p,
dest: strings.Replace(p, cleanPath, "", 1),
perm: i.Mode(),
}
listFiles = append(listFiles, srcdst)
}
Expand All @@ -186,6 +192,7 @@ func getListFiles(tplFileNames []string) ([]*SrcDest, error) {
srcdst := &SrcDest{
src: cleanPath,
dest: filepath.Base(cleanPath),
perm: info.Mode(),
}
listFiles = append(listFiles, srcdst)
}
Expand Down

0 comments on commit b2057a0

Please sign in to comment.