diff options
author | Julien Dessaux | 2024-03-12 23:23:10 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-12 23:23:10 +0100 |
commit | 93b22a886a12cde2886616298446fcc3eee1bd4d (patch) | |
tree | e5147ae2968abb7922a4885332f9e8c9601271d5 /pkg/utils.go | |
parent | chore(variables): support integer interpolation for all values (diff) | |
download | gonf-93b22a886a12cde2886616298446fcc3eee1bd4d.tar.gz gonf-93b22a886a12cde2886616298446fcc3eee1bd4d.tar.bz2 gonf-93b22a886a12cde2886616298446fcc3eee1bd4d.zip |
feat(files): support creating intermediate directories
Diffstat (limited to 'pkg/utils.go')
-rw-r--r-- | pkg/utils.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/pkg/utils.go b/pkg/utils.go index f62d1c3..c3301f2 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -2,6 +2,10 @@ package gonf import ( "crypto/sha256" + "errors" + "io/fs" + "os" + "path/filepath" ) var builtinTemplateFunctions = map[string]any{ @@ -20,6 +24,28 @@ func FilterSlice[T any](slice *[]T, predicate func(T) bool) { *slice = (*slice)[:i] // or truncated out of the slice } +// We cannot just use os.MakedirAll because we need to set the user:group on every intermediate directories created +func makeDirectoriesHierarchy(dir string, perms *Permissions) (Status, error) { + if _, err := os.Lstat(dir); err != nil { + if errors.Is(err, fs.ErrNotExist) { + if status, err := makeDirectoriesHierarchy(filepath.Dir(dir), perms); err != nil { + return status, err + } + m, err := perms.mode.Int() + if err != nil { + return BROKEN, err + } + os.Mkdir(dir, fs.FileMode(m)) + perms.resolve(dir) + return REPAIRED, nil + } else { + return BROKEN, err + } + } else { + return KEPT, nil + } +} + func sha256sum(contents []byte) []byte { h := sha256.New() h.Write(contents) |