summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-01 16:23:08 +0200
committerJulien Dessaux2024-05-01 16:23:08 +0200
commit8b9195e3e3506d576b39a23ca646c260dfacc808 (patch)
tree1cdbdd2fb368654ef8eab702258eb5d6a90e1447
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
-rw-r--r--cmd/gonf/cmd_build.go16
-rw-r--r--cmd/gonf/main.go10
-rw-r--r--pkg/commands.go4
-rw-r--r--pkg/files.go21
-rw-r--r--pkg/packages.go11
-rw-r--r--pkg/permissions.go8
-rw-r--r--pkg/services.go5
-rw-r--r--pkg/templates.go3
-rw-r--r--pkg/users.go11
-rw-r--r--pkg/utils.go16
-rw-r--r--pkg/values.go4
-rw-r--r--pkg/variables.go5
-rw-r--r--stdlib/os/debian/apt.go8
-rw-r--r--stdlib/os/debian/debian.go17
-rw-r--r--stdlib/os/systemd/systemd.go4
15 files changed, 64 insertions, 79 deletions
diff --git a/cmd/gonf/cmd_build.go b/cmd/gonf/cmd_build.go
index ebcde29..6a59b4b 100644
--- a/cmd/gonf/cmd_build.go
+++ b/cmd/gonf/cmd_build.go
@@ -19,7 +19,7 @@ func cmdBuild(ctx context.Context,
where FLAG can be one or more of`, flag.ContinueOnError)
hostFlag := addHostFlag(f)
f.SetOutput(stderr)
- f.Parse(args)
+ _ = f.Parse(args)
if helpMode {
f.SetOutput(stdout)
f.Usage()
@@ -32,17 +32,23 @@ where FLAG can be one or more of`, flag.ContinueOnError)
return runBuild(ctx, stderr, hostDir)
}
-func runBuild(ctx context.Context, stderr io.Writer, hostDir string) error {
+func runBuild(ctx context.Context, stderr io.Writer, hostDir string) (err error) {
wd, err := os.Getwd()
if err != nil {
return err
}
- defer os.Chdir(wd)
- os.Chdir(hostDir)
+ defer func() {
+ if e := os.Chdir(wd); err == nil {
+ err = e
+ }
+ }()
+ if err = os.Chdir(hostDir); err != nil {
+ return err
+ }
cmd := exec.CommandContext(ctx, "go", "build", "-ldflags", "-s -w -extldflags \"-static\"", hostDir)
cmd.Env = append(cmd.Environ(), "CGO_ENABLED=0")
if out, err := cmd.CombinedOutput(); err != nil {
- fmt.Fprint(stderr, string(out))
+ _, _ = fmt.Fprint(stderr, string(out))
return err
}
return nil
diff --git a/cmd/gonf/main.go b/cmd/gonf/main.go
index 8dae51d..fe6f065 100644
--- a/cmd/gonf/main.go
+++ b/cmd/gonf/main.go
@@ -49,11 +49,13 @@ where FLAG can be one or more of`, flag.ContinueOnError)
f.BoolVar(&helpMode, "help", false, "show contextual help")
f.StringVar(&configDir, "config", "", "(REQUIRED for most commands) path to a gonf configurations repository (overrides the GONF_CONFIG environment variable)")
f.SetOutput(stderr)
- f.Parse(args[1:])
+ if err := f.Parse(args[1:]); err != nil {
+ return err
+ }
if f.NArg() < 1 {
f.Usage()
- return errors.New("No command given")
+ return errors.New("no command given")
}
cmd := f.Arg(0)
argsTail := f.Args()[1:]
@@ -68,7 +70,7 @@ where FLAG can be one or more of`, flag.ContinueOnError)
configDir = getenv("GONF_CONFIG")
if configDir == "" {
f.Usage()
- return errors.New("The GONF_CONFIG environment variable is unset and the -config FLAG is missing. Please use one or the other.")
+ return errors.New("the GONF_CONFIG environment variable is unset and the -config FLAG is missing. Please use one or the other")
}
}
switch cmd {
@@ -76,7 +78,7 @@ where FLAG can be one or more of`, flag.ContinueOnError)
return cmdBuild(ctx, f, argsTail, getenv, stdout, stderr)
default:
f.Usage()
- return fmt.Errorf("Invalid command: %s", cmd)
+ return fmt.Errorf("invalid command: %s", cmd)
}
}
return nil
diff --git a/pkg/commands.go b/pkg/commands.go
index 5403e5f..096da11 100644
--- a/pkg/commands.go
+++ b/pkg/commands.go
@@ -6,15 +6,12 @@ import (
"os/exec"
)
-// ----- Globals ---------------------------------------------------------------
var commands []*CommandPromise
-// ----- Init ------------------------------------------------------------------
func init() {
commands = make([]*CommandPromise, 0)
}
-// ----- Public ----------------------------------------------------------------
func Command(cmd string, args ...string) *CommandPromise {
return CommandWithEnv([]string{}, cmd, args...)
}
@@ -81,7 +78,6 @@ func (c CommandPromise) Status() Status {
return c.status
}
-// ----- Internal --------------------------------------------------------------
func resolveCommands() (status Status) {
status = KEPT
for _, c := range commands {
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
}
diff --git a/pkg/packages.go b/pkg/packages.go
index bf9322c..87db647 100644
--- a/pkg/packages.go
+++ b/pkg/packages.go
@@ -2,20 +2,16 @@ package gonf
import "log/slog"
-// ----- Globals ---------------------------------------------------------------
var packages []*PackagePromise
-// packages management functions
-var packages_install_function func([]string) (Status, []string)
+var packagesInstallFunction func([]string) (Status, []string)
-// ----- Init ------------------------------------------------------------------
func init() {
packages = make([]*PackagePromise, 0)
}
-// ----- Public ----------------------------------------------------------------
func SetPackagesConfiguration(install func([]string) (Status, []string), update *CommandPromise) {
- packages_install_function = install
+ packagesInstallFunction = install
}
func Package(names ...string) *PackagePromise {
@@ -45,7 +41,7 @@ func (p *PackagePromise) Promise() Promise {
}
func (p *PackagePromise) Resolve() {
- status, affected := packages_install_function(p.names)
+ status, affected := packagesInstallFunction(p.names)
switch status {
case BROKEN:
slog.Error("package", "names", p.names, "status", status, "broke", affected)
@@ -63,7 +59,6 @@ func (p PackagePromise) Status() Status {
return p.status
}
-// ----- Internal --------------------------------------------------------------
func resolvePackages() (status Status) {
status = KEPT
for _, c := range packages {
diff --git a/pkg/permissions.go b/pkg/permissions.go
index 91287c0..9a46b64 100644
--- a/pkg/permissions.go
+++ b/pkg/permissions.go
@@ -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
}
}
diff --git a/pkg/services.go b/pkg/services.go
index 7e4f114..319599e 100644
--- a/pkg/services.go
+++ b/pkg/services.go
@@ -2,18 +2,14 @@ package gonf
import "log/slog"
-// ----- Globals ---------------------------------------------------------------
var services []*ServicePromise
-// service management function
var serviceFunction func(string, string) (Status, error)
-// ----- Init ------------------------------------------------------------------
func init() {
services = make([]*ServicePromise, 0)
}
-// ----- Public ----------------------------------------------------------------
func SetServiceFunction(f func(string, string) (Status, error)) {
serviceFunction = f
}
@@ -82,7 +78,6 @@ func (s ServicePromise) Status() Status {
return s.status
}
-// ----- Internal --------------------------------------------------------------
func resolveServices() (status Status) {
status = KEPT
for _, c := range services {
diff --git a/pkg/templates.go b/pkg/templates.go
index 736bbe0..1339306 100644
--- a/pkg/templates.go
+++ b/pkg/templates.go
@@ -7,17 +7,14 @@ import (
"text/template"
)
-// ----- Globals ---------------------------------------------------------------
var templates *template.Template
-// ----- Init ------------------------------------------------------------------
func init() {
templates = template.New("")
templates.Option("missingkey=error")
templates.Funcs(builtinTemplateFunctions)
}
-// ----- Public ----------------------------------------------------------------
type TemplateValue struct {
contents []byte
data string
diff --git a/pkg/users.go b/pkg/users.go
index ca7913b..2577207 100644
--- a/pkg/users.go
+++ b/pkg/users.go
@@ -4,20 +4,16 @@ import (
"log/slog"
)
-// ----- Globals ---------------------------------------------------------------
var users []*UserPromise
-// users management functions
-var user_add_function func(data UserData) (Status, error)
+var userAddFunction func(data UserData) (Status, error)
-// ----- Init ------------------------------------------------------------------
func init() {
users = make([]*UserPromise, 0)
}
-// ----- Public ----------------------------------------------------------------
func SetUsersConfiguration(useradd func(data UserData) (Status, error)) {
- user_add_function = useradd
+ userAddFunction = useradd
}
func User(data UserData) *UserPromise {
@@ -57,7 +53,7 @@ func (u *UserPromise) Promise() Promise {
func (u *UserPromise) Resolve() {
var err error
- u.status, err = user_add_function(u.data)
+ u.status, err = userAddFunction(u.data)
switch u.status {
case BROKEN:
slog.Error("user", "name", u.data.Name, "status", u.status, "error", err)
@@ -77,7 +73,6 @@ func (u UserPromise) Status() Status {
return u.status
}
-// ----- Internal --------------------------------------------------------------
func resolveUsers() (status Status) {
status = KEPT
for _, c := range users {
diff --git a/pkg/utils.go b/pkg/utils.go
index c3301f2..3920737 100644
--- a/pkg/utils.go
+++ b/pkg/utils.go
@@ -28,15 +28,19 @@ 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 status, err := makeDirectoriesHierarchy(filepath.Dir(dir), perms); err != nil {
- return status, err
+ if _, err = makeDirectoriesHierarchy(filepath.Dir(dir), perms); err != nil {
+ return BROKEN, err
+ }
+ var m int
+ if m, err = perms.mode.Int(); err != nil {
+ return BROKEN, err
+ }
+ if err = os.Mkdir(dir, fs.FileMode(m)); err != nil {
+ return BROKEN, err
}
- m, err := perms.mode.Int()
- if err != nil {
+ if _, err = perms.resolve(dir); err != nil {
return BROKEN, err
}
- os.Mkdir(dir, fs.FileMode(m))
- perms.resolve(dir)
return REPAIRED, nil
} else {
return BROKEN, err
diff --git a/pkg/values.go b/pkg/values.go
index 25f31e4..70fc9fe 100644
--- a/pkg/values.go
+++ b/pkg/values.go
@@ -47,7 +47,6 @@ func interfaceToTemplateValue(v any) Value {
panic(fmt.Sprintf("interfaceToTemplateValue cannot take type %T as argument. Value was %#v.", v, v))
}
-// ----- BytesValue ------------------------------------------------------------
type BytesValue struct {
value []byte
}
@@ -62,7 +61,6 @@ func (b BytesValue) String() string {
return string(b.value[:])
}
-// ----- IntValue --------------------------------------------------------------
type IntValue struct {
value int
}
@@ -77,7 +75,6 @@ func (i IntValue) String() string {
return fmt.Sprint(i.value)
}
-// ----- StringsListValue ------------------------------------------------------
type StringsListValue struct {
value []string
}
@@ -95,7 +92,6 @@ func (s StringsListValue) String() string {
return strings.Join(s.value, "\n")
}
-// ----- StringValue -----------------------------------------------------------
type StringValue struct {
value string
}
diff --git a/pkg/variables.go b/pkg/variables.go
index bb3096d..4c9bb56 100644
--- a/pkg/variables.go
+++ b/pkg/variables.go
@@ -5,15 +5,12 @@ import (
"log/slog"
)
-// ----- Globals ---------------------------------------------------------------
var variables map[string]*VariablePromise
-// ----- Init ------------------------------------------------------------------
func init() {
variables = make(map[string]*VariablePromise)
}
-// ----- Public ----------------------------------------------------------------
func AppendVariable(name string, values ...string) *VariablePromise {
if v, ok := variables[name]; ok {
if l, ok := v.value.(*StringsListValue); ok {
@@ -66,7 +63,6 @@ type VariablePromise struct {
value Value
}
-// We want VariablePromise to satisfy the Value interface
func (s VariablePromise) Bytes() []byte {
return s.value.Bytes()
}
@@ -77,7 +73,6 @@ func (s VariablePromise) String() string {
return s.value.String()
}
-// ----- Internal --------------------------------------------------------------
func getVariable(name string) string {
if v, ok := variables[name]; ok {
return v.value.String()
diff --git a/stdlib/os/debian/apt.go b/stdlib/os/debian/apt.go
index 3386f20..3b72b9f 100644
--- a/stdlib/os/debian/apt.go
+++ b/stdlib/os/debian/apt.go
@@ -14,10 +14,10 @@ import (
var packages map[string]string
func init() {
- packages_list()
+ packagesList()
}
-func packages_install(names []string) (gonf.Status, []string) {
+func packagesInstall(names []string) (gonf.Status, []string) {
gonf.FilterSlice(&names, func(n string) bool {
_, ok := packages[n]
return !ok
@@ -28,11 +28,11 @@ func packages_install(names []string) (gonf.Status, []string) {
args := append([]string{"install", "-y", "--no-install-recommends"}, names...)
cmd := gonf.CommandWithEnv([]string{"DEBIAN_FRONTEND=noninteractive", "LC_ALL=C"}, "apt-get", args...)
cmd.Resolve()
- packages_list()
+ packagesList()
return cmd.Status(), names
}
-func packages_list() {
+func packagesList() {
packages = make(map[string]string)
ecmd := exec.Command("dpkg-query", "-W")
out, err := ecmd.CombinedOutput()
diff --git a/stdlib/os/debian/debian.go b/stdlib/os/debian/debian.go
index 9954088..95af85d 100644
--- a/stdlib/os/debian/debian.go
+++ b/stdlib/os/debian/debian.go
@@ -2,21 +2,22 @@ package debian
import (
_ "embed"
- "git.adyxax.org/adyxax/gonf/v2/pkg"
+
+ gonf "git.adyxax.org/adyxax/gonf/v2/pkg"
"git.adyxax.org/adyxax/gonf/v2/stdlib/os/linux"
"git.adyxax.org/adyxax/gonf/v2/stdlib/os/systemd"
)
//go:embed apt-norecommends
-var apt_norecommends []byte
+var aptNoRecommends []byte
//go:embed sources.list
-var sources_list []byte
+var sourcesList []byte
func Promise() {
// ----- gonf --------------------------------------------------------------
- apt_update := gonf.Command("apt-get", "update", "-qq")
- gonf.SetPackagesConfiguration(packages_install, apt_update)
+ aptUpdate := gonf.Command("apt-get", "update", "-qq")
+ gonf.SetPackagesConfiguration(packagesInstall, aptUpdate)
gonf.SetUsersConfiguration(linux.Useradd)
// ----- systemd -----------------------------------------------------------
systemd.Promise()
@@ -27,12 +28,12 @@ func Promise() {
gonf.AppendVariable("debian-extra-sources", "# Extra sources")
gonf.File("/etc/apt/sources.list").
Permissions(rootRO).
- Template(sources_list).
+ Template(sourcesList).
Promise().
- IfRepaired(apt_update)
+ IfRepaired(aptUpdate)
gonf.File("/etc/apt/apt.conf.d/99_norecommends").
DirectoriesPermissions(rootDir).
Permissions(rootRO).
- Contents(apt_norecommends).
+ Contents(aptNoRecommends).
Promise()
}
diff --git a/stdlib/os/systemd/systemd.go b/stdlib/os/systemd/systemd.go
index 8d5ec77..81e6a6b 100644
--- a/stdlib/os/systemd/systemd.go
+++ b/stdlib/os/systemd/systemd.go
@@ -8,7 +8,7 @@ import (
)
func Promise() {
- gonf.SetServiceFunction(systemd_service)
+ gonf.SetServiceFunction(systemdService)
}
func isEnabled(name string) bool {
@@ -33,7 +33,7 @@ func systemctlShow(name, field string) string {
return string(out[:len(out)-1]) // remove trailing '\n' and convert to string
}
-func systemd_service(name, state string) (gonf.Status, error) {
+func systemdService(name, state string) (gonf.Status, error) {
switch state {
case "disabled":
if isEnabled(name) {