From e7572a13826001f9867143759b2bb3a3894ec51c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 27 Aug 2024 08:40:51 +0200 Subject: feat(files): support creating directories --- pkg/files.go | 70 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/pkg/files.go b/pkg/files.go index b9e7e5c..23c27d9 100644 --- a/pkg/files.go +++ b/pkg/files.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/sha256" "errors" + "fmt" "io" "io/fs" "log/slog" @@ -17,16 +18,37 @@ func init() { files = make([]*FilePromise, 0) } +type FileType int + +const ( + FILE = iota + DIRECTORY +) + type FilePromise struct { chain []Promise contents Value dirPermissions *Permissions err error filename Value + fileType FileType permissions *Permissions status Status } +func Directory(filename any) *FilePromise { + return &FilePromise{ + chain: nil, + contents: nil, + dirPermissions: nil, + err: nil, + filename: interfaceToTemplateValue(filename), + fileType: DIRECTORY, + permissions: nil, + status: PROMISED, + } +} + func File(filename any) *FilePromise { return &FilePromise{ chain: nil, @@ -34,6 +56,7 @@ func File(filename any) *FilePromise { dirPermissions: nil, err: nil, filename: interfaceToTemplateValue(filename), + fileType: FILE, permissions: nil, status: PROMISED, } @@ -71,31 +94,38 @@ 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 { + if f.status, f.err = makeDirectoriesHierarchy(filepath.Dir(filename), f.dirPermissions); f.err != nil { + return + } + switch f.fileType { + case DIRECTORY: + if f.status, f.err = makeDirectoriesHierarchy(filepath.Clean(filename), f.dirPermissions); f.err != nil { return } - } - if f.contents != nil { - var sumFile []byte - sumFile, f.err = sha256sumOfFile(filename) - if f.err != nil { - if !errors.Is(f.err, fs.ErrNotExist) { - slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err) - f.status = BROKEN - return + case FILE: + if f.contents != nil { + var sumFile []byte + sumFile, f.err = sha256sumOfFile(filename) + if f.err != nil { + if !errors.Is(f.err, fs.ErrNotExist) { + slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err) + f.status = BROKEN + return + } } - } - contents := f.contents.Bytes() - sumContents := sha256sum(contents) - if !bytes.Equal(sumFile, sumContents) { - if f.err = writeFile(filename, contents); f.err != nil { - f.status = BROKEN - slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err) - return + contents := f.contents.Bytes() + sumContents := sha256sum(contents) + if !bytes.Equal(sumFile, sumContents) { + if f.err = writeFile(filename, contents); f.err != nil { + f.status = BROKEN + slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err) + return + } + f.status = REPAIRED } - f.status = REPAIRED } + default: + panic(fmt.Errorf("unknown File type enum value %d", f.fileType)) } if f.permissions != nil { var status Status -- cgit v1.2.3