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 --- cmd/gonf/cmd_build.go | 12 ++++-------- cmd/gonf/cmd_deploy.go | 5 ++--- cmd/gonf/hostflag.go | 7 +++---- cmd/gonf/main.go | 14 ++++++-------- cmd/gonf/ssh.go | 45 ++++++++++++++++----------------------------- 5 files changed, 31 insertions(+), 52 deletions(-) (limited to 'cmd') 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 } -- cgit v1.2.3