summaryrefslogtreecommitdiff
path: root/pkg/files.go
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-01 16:23:08 +0200
committerJulien Dessaux2024-05-01 16:23:08 +0200
commit8b9195e3e3506d576b39a23ca646c260dfacc808 (patch)
tree1cdbdd2fb368654ef8eab702258eb5d6a90e1447 /pkg/files.go
parentchore(gonf): fix go vet and staticcheck errors (diff)
downloadgonf-8b9195e3e3506d576b39a23ca646c260dfacc808.tar.gz
gonf-8b9195e3e3506d576b39a23ca646c260dfacc808.tar.bz2
gonf-8b9195e3e3506d576b39a23ca646c260dfacc808.zip
chore(gonf): fix errcheck and shadow errors
Diffstat (limited to '')
-rw-r--r--pkg/files.go21
1 files changed, 12 insertions, 9 deletions
diff --git a/pkg/files.go b/pkg/files.go
index eb3fa38..2fe0f6e 100644
--- a/pkg/files.go
+++ b/pkg/files.go
@@ -11,15 +11,12 @@ import (
"path/filepath"
)
-// ----- Globals ---------------------------------------------------------------
var files []*FilePromise
-// ----- Init ------------------------------------------------------------------
func init() {
files = make([]*FilePromise, 0)
}
-// ----- Public ----------------------------------------------------------------
type FilePromise struct {
chain []Promise
contents Value
@@ -62,7 +59,6 @@ func (f *FilePromise) Template(contents any) *FilePromise {
return f
}
-// We want to satisfy the Promise interface
func (f *FilePromise) IfRepaired(p ...Promise) Promise {
f.chain = append(f.chain, p...)
return f
@@ -127,7 +123,6 @@ func (f FilePromise) Status() Status {
return f.status
}
-// ----- Internal --------------------------------------------------------------
func resolveFiles() (status Status) {
status = KEPT
for _, f := range files {
@@ -144,12 +139,16 @@ func resolveFiles() (status Status) {
return
}
-func sha256sumOfFile(filename string) ([]byte, error) {
+func sha256sumOfFile(filename string) (hash []byte, err error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
- defer f.Close()
+ defer func() {
+ if e := f.Close(); err == nil {
+ err = e
+ }
+ }()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return nil, err
@@ -157,12 +156,16 @@ func sha256sumOfFile(filename string) ([]byte, error) {
return h.Sum(nil), nil
}
-func writeFile(filename string, contents []byte) error {
+func writeFile(filename string, contents []byte) (err error) {
f, err := os.Create(filename)
if err != nil {
return err
}
- defer f.Close()
+ defer func() {
+ if e := f.Close(); err == nil {
+ err = e
+ }
+ }()
_, err = f.Write(contents)
return err
}