From 8b9195e3e3506d576b39a23ca646c260dfacc808 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 1 May 2024 16:23:08 +0200 Subject: chore(gonf): fix errcheck and shadow errors --- cmd/gonf/cmd_build.go | 16 +++++++++++----- cmd/gonf/main.go | 10 ++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) (limited to 'cmd') 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 -- cgit v1.2.3