summaryrefslogtreecommitdiff
path: root/pkg/files.go
diff options
context:
space:
mode:
authorJulien Dessaux2024-03-12 23:23:10 +0100
committerJulien Dessaux2024-03-12 23:23:10 +0100
commit93b22a886a12cde2886616298446fcc3eee1bd4d (patch)
treee5147ae2968abb7922a4885332f9e8c9601271d5 /pkg/files.go
parentchore(variables): support integer interpolation for all values (diff)
downloadgonf-93b22a886a12cde2886616298446fcc3eee1bd4d.tar.gz
gonf-93b22a886a12cde2886616298446fcc3eee1bd4d.tar.bz2
gonf-93b22a886a12cde2886616298446fcc3eee1bd4d.zip
feat(files): support creating intermediate directories
Diffstat (limited to 'pkg/files.go')
-rw-r--r--pkg/files.go37
1 files changed, 25 insertions, 12 deletions
diff --git a/pkg/files.go b/pkg/files.go
index 35420d4..379b437 100644
--- a/pkg/files.go
+++ b/pkg/files.go
@@ -8,6 +8,7 @@ import (
"io/fs"
"log/slog"
"os"
+ "path/filepath"
)
// ----- Globals ---------------------------------------------------------------
@@ -20,22 +21,24 @@ func init() {
// ----- Public ----------------------------------------------------------------
type FilePromise struct {
- chain []Promise
- contents Value
- err error
- filename Value
- permissions *Permissions
- status Status
+ chain []Promise
+ contents Value
+ dirPermissions *Permissions
+ err error
+ filename Value
+ permissions *Permissions
+ status Status
}
func File(filename any) *FilePromise {
return &FilePromise{
- chain: nil,
- contents: nil,
- err: nil,
- filename: interfaceToTemplateValue(filename),
- permissions: nil,
- status: PROMISED,
+ chain: nil,
+ contents: nil,
+ dirPermissions: nil,
+ err: nil,
+ filename: interfaceToTemplateValue(filename),
+ permissions: nil,
+ status: PROMISED,
}
}
@@ -44,6 +47,11 @@ func (f *FilePromise) Contents(contents any) *FilePromise {
return f
}
+func (f *FilePromise) DirectoriesPermissions(p *Permissions) *FilePromise {
+ f.dirPermissions = p
+ return f
+}
+
func (f *FilePromise) Permissions(p *Permissions) *FilePromise {
f.permissions = p
return f
@@ -67,6 +75,11 @@ func (f *FilePromise) Promise() Promise {
func (f *FilePromise) Resolve() {
filename := f.filename.String()
+ if f.dirPermissions != nil {
+ if f.status, f.err = makeDirectoriesHierarchy(filepath.Dir(filename), f.dirPermissions); f.err != nil {
+ return
+ }
+ }
if f.contents != nil {
var sumFile []byte
sumFile, f.err = sha256sumOfFile(filename)