summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-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
10 files changed, 32 insertions, 56 deletions
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()