diff options
-rw-r--r-- | cmd/gonf/cmd_build.go | 12 | ||||
-rw-r--r-- | cmd/gonf/cmd_deploy.go | 5 | ||||
-rw-r--r-- | cmd/gonf/hostflag.go | 7 | ||||
-rw-r--r-- | cmd/gonf/main.go | 14 | ||||
-rw-r--r-- | cmd/gonf/ssh.go | 45 | ||||
-rw-r--r-- | pkg/files.go | 16 | ||||
-rw-r--r-- | pkg/permissions.go | 12 | ||||
-rw-r--r-- | pkg/utils.go | 10 | ||||
-rw-r--r-- | pkg/values.go | 17 | ||||
-rw-r--r-- | stdlib/os/systemd/systemd.go | 4 |
10 files changed, 56 insertions, 86 deletions
diff --git a/cmd/gonf/cmd_build.go b/cmd/gonf/cmd_build.go index 6a59b4b..566edfd 100644 --- a/cmd/gonf/cmd_build.go +++ b/cmd/gonf/cmd_build.go @@ -19,12 +19,12 @@ 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() } - hostDir, err := hostFlagToHostDir(f, hostFlag) + hostDir, err := hostFlagToHostDir(hostFlag) if err != nil { f.Usage() return err @@ -32,16 +32,12 @@ 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) (err error) { +func runBuild(ctx context.Context, stderr io.Writer, hostDir string) error { wd, err := os.Getwd() if err != nil { return err } - defer func() { - if e := os.Chdir(wd); err == nil { - err = e - } - }() + defer os.Chdir(wd) if err = os.Chdir(hostDir); err != nil { return err } diff --git a/cmd/gonf/cmd_deploy.go b/cmd/gonf/cmd_deploy.go index c541a25..0d48b2b 100644 --- a/cmd/gonf/cmd_deploy.go +++ b/cmd/gonf/cmd_deploy.go @@ -18,17 +18,16 @@ func cmdDeploy(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() } - hostDir, err := hostFlagToHostDir(f, hostFlag) + hostDir, err := hostFlagToHostDir(hostFlag) if err != nil { f.Usage() return err } - _ = hostDir return runDeploy(ctx, getenv, stdout, stderr, *hostFlag, hostDir) } diff --git a/cmd/gonf/hostflag.go b/cmd/gonf/hostflag.go index a8627e8..2c2c877 100644 --- a/cmd/gonf/hostflag.go +++ b/cmd/gonf/hostflag.go @@ -1,7 +1,6 @@ package main import ( - "errors" "flag" "fmt" "os" @@ -12,13 +11,13 @@ func addHostFlag(f *flag.FlagSet) *string { return f.String("host", "", "(REQUIRED) a valid $GONF_CONFIG/hosts/ subdirectory") } -func hostFlagToHostDir(f *flag.FlagSet, hostFlag *string) (string, error) { - return "", errors.New("required -host FLAG is missing") +func hostFlagToHostDir(hostFlag *string) (string, error) { if *hostFlag == "" { + return "", fmt.Errorf("required -host FLAG is missing") } hostDir := filepath.Join(configDir, "hosts", *hostFlag) if info, err := os.Stat(hostDir); err != nil { - return "", fmt.Errorf("invalid host name %s: %+v", *hostFlag, err) + return "", fmt.Errorf("invalid host name %s: %w", *hostFlag, err) } else if !info.IsDir() { return "", fmt.Errorf("invalid host name %s: %s is not a directory", *hostFlag, hostDir) } diff --git a/cmd/gonf/main.go b/cmd/gonf/main.go index 99817c4..aefc088 100644 --- a/cmd/gonf/main.go +++ b/cmd/gonf/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "errors" "flag" "fmt" "io" @@ -25,7 +24,7 @@ func main() { os.Stdout, os.Stderr, ); err != nil { - fmt.Fprintf(os.Stderr, "%+v\n", err) + fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } } @@ -41,7 +40,8 @@ func run(ctx context.Context, defer cancel() f := flag.NewFlagSet(`gonf COMMAND [-FLAG] where COMMAND is one of: - * build: build configurations for one or more hosts + * build: build configuration for a host + * deploy: deploy configuration for a host * help: show contextual help * version: show build version and time where FLAG can be one or more of`, flag.ContinueOnError) @@ -49,13 +49,11 @@ 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) - if err := f.Parse(args[1:]); err != nil { - return err - } + f.Parse(args[1:]) if f.NArg() < 1 { f.Usage() - return errors.New("no command given") + return fmt.Errorf("no command given") } cmd := f.Arg(0) argsTail := f.Args()[1:] @@ -70,7 +68,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 fmt.Errorf("the GONF_CONFIG environment variable is unset and the -config FLAG is missing. Please use one or the other") } } switch cmd { diff --git a/cmd/gonf/ssh.go b/cmd/gonf/ssh.go index 47bf2c5..e113d9f 100644 --- a/cmd/gonf/ssh.go +++ b/cmd/gonf/ssh.go @@ -29,13 +29,13 @@ func newSSHClient(context context.Context, socket := getenv("SSH_AUTH_SOCK") if sshc.agentConn, err = net.Dial("unix", socket); err != nil { - return nil, fmt.Errorf("failed to open SSH_AUTH_SOCK: %+v", err) + return nil, fmt.Errorf("failed to open SSH_AUTH_SOCK: %w", err) } agentClient := agent.NewClient(sshc.agentConn) hostKeyCallback, err := knownhosts.New(filepath.Join(getenv("HOME"), ".ssh/known_hosts")) if err != nil { - return nil, fmt.Errorf("could not create hostkeycallback function: %+v", err) + return nil, fmt.Errorf("failed to create hostkeycallback function: %w", err) } config := &ssh.ClientConfig{ @@ -47,7 +47,7 @@ func newSSHClient(context context.Context, } sshc.client, err = ssh.Dial("tcp", destination, config) if err != nil { - return nil, fmt.Errorf("failed to dial: %+v", err) + return nil, fmt.Errorf("failed to dial: %w", err) } return &sshc, nil } @@ -62,38 +62,31 @@ func (sshc *sshClient) Close() error { return nil } -func (sshc *sshClient) SendFile(ctx context.Context, +func (sshc *sshClient) SendFile( + ctx context.Context, stdout, stderr io.Writer, filename string, -) (err error) { +) error { session, err := sshc.client.NewSession() if err != nil { - return fmt.Errorf("sshClient failed to create session: %+v", err) + return fmt.Errorf("failed to create ssh client session: %w", err) } - defer func() { - if e := session.Close(); err == nil && e != io.EOF { - err = e - } - }() + defer session.Close() file, err := os.Open(filename) if err != nil { - return fmt.Errorf("sshClient failed to open file: %+v", err) + return fmt.Errorf("failed to open file: %w", err) } - defer func() { - if e := file.Close(); err == nil { - err = e - } - }() + defer file.Close() fi, err := file.Stat() if err != nil { - return fmt.Errorf("sshClient failed to stat file: %+v", err) + return fmt.Errorf("failed to stat file: %w", err) } w, err := session.StdinPipe() if err != nil { - return fmt.Errorf("sshClient failed to open session stdin pipe: %+v", err) + return fmt.Errorf("sshClient failed to open session stdin pipe: %w", err) } wg := sync.WaitGroup{} @@ -102,8 +95,8 @@ func (sshc *sshClient) SendFile(ctx context.Context, session.Stdout = stdout session.Stderr = stderr - if err = session.Start("scp -t /usr/local/bin/gonf-run"); err != nil { - return fmt.Errorf("sshClient failed to run scp: %+v", err) + if err := session.Start("scp -t /usr/local/bin/gonf-run"); err != nil { + return fmt.Errorf("failed to run scp: %w", err) } go func() { defer wg.Done() @@ -114,11 +107,7 @@ func (sshc *sshClient) SendFile(ctx context.Context, go func() { defer wg.Done() - defer func() { - if e := w.Close(); e != nil { - errCh <- e - } - }() + defer w.Close() // Write "C{mode} {size} {filename}\n" if _, e := fmt.Fprintf(w, "C%#o %d %s\n", 0700, fi.Size(), "gonf-run"); e != nil { errCh <- e @@ -135,8 +124,7 @@ func (sshc *sshClient) SendFile(ctx context.Context, } }() - var cancel context.CancelFunc - ctx, cancel = context.WithTimeout(ctx, 60*time.Second) + ctx, cancel := context.WithTimeout(ctx, 60*time.Second) defer cancel() // wait for all waitgroup.Done() or the timeout @@ -157,6 +145,5 @@ func (sshc *sshClient) SendFile(ctx context.Context, return err } } - return nil } 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 { diff --git a/stdlib/os/systemd/systemd.go b/stdlib/os/systemd/systemd.go index 81e6a6b..298998e 100644 --- a/stdlib/os/systemd/systemd.go +++ b/stdlib/os/systemd/systemd.go @@ -1,7 +1,7 @@ package systemd import ( - "errors" + "fmt" "os/exec" gonf "git.adyxax.org/adyxax/gonf/v2/pkg" @@ -64,6 +64,6 @@ func systemdService(name, state string) (gonf.Status, error) { return gonf.KEPT, nil } default: - return gonf.BROKEN, errors.New("unsupported systemctl operation " + state) + return gonf.BROKEN, fmt.Errorf("unsupported systemctl operation " + state) } } |