chore(files): updated FilePromise API
This commit is contained in:
parent
e4eccafed7
commit
d7aa6c514b
1 changed files with 38 additions and 29 deletions
|
@ -3,7 +3,9 @@ package gonf
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
@ -25,26 +27,27 @@ type FilePromise struct {
|
||||||
status Status
|
status Status
|
||||||
}
|
}
|
||||||
|
|
||||||
func File(filename any, contents string) *FilePromise {
|
func File(filename any) *FilePromise {
|
||||||
return &FilePromise{
|
return &FilePromise{
|
||||||
chain: nil,
|
chain: nil,
|
||||||
contents: interfaceToValue(contents),
|
contents: nil,
|
||||||
err: nil,
|
err: nil,
|
||||||
filename: interfaceToTemplateValue(filename),
|
filename: interfaceToTemplateValue(filename),
|
||||||
status: PROMISED,
|
status: PROMISED,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Template(filename any, contents any) *FilePromise {
|
func (f *FilePromise) Contents(contents any) *FilePromise {
|
||||||
return &FilePromise{
|
f.contents = interfaceToValue(contents)
|
||||||
chain: nil,
|
return f
|
||||||
contents: interfaceToTemplateValue(contents),
|
|
||||||
err: nil,
|
|
||||||
filename: interfaceToTemplateValue(filename),
|
|
||||||
status: PROMISED,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FilePromise) Template(contents any) *FilePromise {
|
||||||
|
f.contents = interfaceToTemplateValue(contents)
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// We want to satisfy the Promise interface
|
||||||
func (f *FilePromise) IfRepaired(p ...Promise) Promise {
|
func (f *FilePromise) IfRepaired(p ...Promise) Promise {
|
||||||
f.chain = p
|
f.chain = p
|
||||||
return f
|
return f
|
||||||
|
@ -57,28 +60,34 @@ func (f *FilePromise) Promise() Promise {
|
||||||
|
|
||||||
func (f *FilePromise) Resolve() {
|
func (f *FilePromise) Resolve() {
|
||||||
filename := f.filename.String()
|
filename := f.filename.String()
|
||||||
var sumFile []byte
|
if f.contents != nil {
|
||||||
sumFile, f.err = sha256sumOfFile(filename)
|
var sumFile []byte
|
||||||
if f.err != nil {
|
sumFile, f.err = sha256sumOfFile(filename)
|
||||||
f.status = BROKEN
|
if f.err != nil {
|
||||||
return
|
if errors.Is(f.err, fs.ErrNotExist) {
|
||||||
}
|
slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err)
|
||||||
contents := f.contents.Bytes()
|
f.status = BROKEN
|
||||||
sumContents := sha256sum(contents)
|
return
|
||||||
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
|
contents := f.contents.Bytes()
|
||||||
slog.Info("file", "filename", f.filename, "status", f.status)
|
sumContents := sha256sum(contents)
|
||||||
for _, p := range f.chain {
|
if !bytes.Equal(sumFile, sumContents) {
|
||||||
p.Resolve()
|
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
|
||||||
|
slog.Info("file", "filename", f.filename, "status", f.status)
|
||||||
|
for _, p := range f.chain {
|
||||||
|
p.Resolve()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
f.status = KEPT
|
if f.status == PROMISED {
|
||||||
|
f.status = KEPT
|
||||||
|
}
|
||||||
slog.Debug("file", "filename", f.filename, "status", f.status)
|
slog.Debug("file", "filename", f.filename, "status", f.status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue