From 4a254746de7246ddf8ac131f734f224d49e927b7 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 2 Jun 2024 01:17:36 +0200 Subject: chore(gonf): removed opinionated static checkers --- pkg/files.go | 16 ++++------------ pkg/permissions.go | 12 ++++++------ pkg/utils.go | 10 +++++----- pkg/values.go | 17 ++++++++--------- 4 files changed, 23 insertions(+), 32 deletions(-) (limited to 'pkg') diff --git a/pkg/files.go b/pkg/files.go index 2fe0f6e..b9e7e5c 100644 --- a/pkg/files.go +++ b/pkg/files.go @@ -139,16 +139,12 @@ func resolveFiles() (status Status) { return } -func sha256sumOfFile(filename string) (hash []byte, err error) { +func sha256sumOfFile(filename string) ([]byte, error) { f, err := os.Open(filename) if err != nil { return nil, err } - defer func() { - if e := f.Close(); err == nil { - err = e - } - }() + defer f.Close() h := sha256.New() if _, err := io.Copy(h, f); err != nil { return nil, err @@ -156,16 +152,12 @@ func sha256sumOfFile(filename string) (hash []byte, err error) { return h.Sum(nil), nil } -func writeFile(filename string, contents []byte) (err error) { +func writeFile(filename string, contents []byte) error { f, err := os.Create(filename) if err != nil { return err } - defer func() { - if e := f.Close(); err == nil { - err = e - } - }() + defer f.Close() _, err = f.Write(contents) return err } diff --git a/pkg/permissions.go b/pkg/permissions.go index 9a46b64..d292ae5 100644 --- a/pkg/permissions.go +++ b/pkg/permissions.go @@ -1,7 +1,7 @@ package gonf import ( - "errors" + "fmt" "io/fs" "os" "os/user" @@ -29,10 +29,10 @@ func (p *Permissions) resolve(filename string) (Status, error) { if group, err := user.LookupGroup(p.group.String()); err != nil { return BROKEN, err } else { - if groupID, err := strconv.Atoi(group.Gid); err != nil { + if groupId, err := strconv.Atoi(group.Gid); err != nil { return BROKEN, err } else { - g = &IntValue{groupID} + g = &IntValue{groupId} p.group = g } } @@ -46,10 +46,10 @@ func (p *Permissions) resolve(filename string) (Status, error) { if user, err := user.Lookup(p.user.String()); err != nil { return BROKEN, err } else { - if userID, err := strconv.Atoi(user.Uid); err != nil { + if userId, err := strconv.Atoi(user.Uid); err != nil { return BROKEN, err } else { - u = &IntValue{userID} + u = &IntValue{userId} p.group = u } } @@ -75,7 +75,7 @@ func (p *Permissions) resolve(filename string) (Status, error) { status = REPAIRED } } else { - return BROKEN, errors.New("unsupported operating system") + return BROKEN, fmt.Errorf("unsupported operating system") } } return status, nil diff --git a/pkg/utils.go b/pkg/utils.go index 3920737..2c9e768 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -28,17 +28,17 @@ func FilterSlice[T any](slice *[]T, predicate func(T) bool) { func makeDirectoriesHierarchy(dir string, perms *Permissions) (Status, error) { if _, err := os.Lstat(dir); err != nil { if errors.Is(err, fs.ErrNotExist) { - if _, err = makeDirectoriesHierarchy(filepath.Dir(dir), perms); err != nil { + if _, err := makeDirectoriesHierarchy(filepath.Dir(dir), perms); err != nil { return BROKEN, err } - var m int - if m, err = perms.mode.Int(); err != nil { + m, err := perms.mode.Int() + if err != nil { return BROKEN, err } - if err = os.Mkdir(dir, fs.FileMode(m)); err != nil { + if err := os.Mkdir(dir, fs.FileMode(m)); err != nil { return BROKEN, err } - if _, err = perms.resolve(dir); err != nil { + if _, err := perms.resolve(dir); err != nil { return BROKEN, err } return REPAIRED, nil diff --git a/pkg/values.go b/pkg/values.go index 70fc9fe..c85de01 100644 --- a/pkg/values.go +++ b/pkg/values.go @@ -14,20 +14,19 @@ type Value interface { } func interfaceToValue(v any) Value { - if vv, ok := v.([]byte); ok { + switch vv := v.(type) { + case []byte: return &BytesValue{vv} - } - if vv, ok := v.(int); ok { + case int: return &IntValue{vv} - } - if vv, ok := v.(string); ok { + case string: return &StringValue{vv} - } - if vv, ok := v.(*VariablePromise); ok { + case *VariablePromise: return vv + default: + slog.Error("interfaceToValue", "value", v, "error", "Not Implemented") + panic(fmt.Sprintf("interfaceToValue cannot take type %T as argument. Value was %#v.", v, v)) } - slog.Error("interfaceToValue", "value", v, "error", "Not Implemented") - panic(fmt.Sprintf("interfaceToValue cannot take type %T as argument. Value was %#v.", v, v)) } func interfaceToTemplateValue(v any) Value { -- cgit v1.2.3