chore(gonf): fix errcheck and shadow errors

This commit is contained in:
Julien Dessaux 2024-05-01 16:23:08 +02:00
parent 72be13c3e9
commit 8b9195e3e3
Signed by: adyxax
GPG key ID: F92E51B86E07177E
15 changed files with 65 additions and 80 deletions

View file

@ -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

View file

@ -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