summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-04-29 07:49:57 +0200
committerJulien Dessaux2024-04-29 07:49:57 +0200
commitcae47352762fc0b04766ac77d9a0c34f60f3841a (patch)
treed0e1b66c283e04d0bf60301c0830254f79dc9e16
parentfeat(gonf): implement the gonf build cli command (diff)
downloadgonf-cae47352762fc0b04766ac77d9a0c34f60f3841a.tar.gz
gonf-cae47352762fc0b04766ac77d9a0c34f60f3841a.tar.bz2
gonf-cae47352762fc0b04766ac77d9a0c34f60f3841a.zip
chore(gonf): factorise hostflag management
-rw-r--r--cmd/gonf/cmd_build.go36
-rw-r--r--cmd/gonf/hostflag.go26
-rw-r--r--cmd/gonf/main.go2
3 files changed, 37 insertions, 27 deletions
diff --git a/cmd/gonf/cmd_build.go b/cmd/gonf/cmd_build.go
index 0e7326a..ebcde29 100644
--- a/cmd/gonf/cmd_build.go
+++ b/cmd/gonf/cmd_build.go
@@ -2,19 +2,13 @@ package main
import (
"context"
- "errors"
"flag"
"fmt"
"io"
- "log/slog"
"os"
"os/exec"
)
-var (
- hostFlag string
-)
-
func cmdBuild(ctx context.Context,
f *flag.FlagSet,
args []string,
@@ -23,43 +17,33 @@ func cmdBuild(ctx context.Context,
) error {
f.Init(`gonf build [-FLAG]
where FLAG can be one or more of`, flag.ContinueOnError)
- f.StringVar(&hostFlag, "host", "", "(REQUIRED) a valid $GONF_CONFIG/hosts/ subdirectory inside your gonf configurations repository")
+ hostFlag := addHostFlag(f)
f.SetOutput(stderr)
f.Parse(args)
if helpMode {
f.SetOutput(stdout)
f.Usage()
}
- if hostFlag == "" {
- f.Usage()
- return errors.New("Required -host FLAG is missing")
- }
- hostDir := configDir + "/hosts/" + hostFlag
- if info, err := os.Stat(hostDir); err != nil {
- f.Usage()
- return fmt.Errorf("Invalid host name %s, the %s directory returned error %+v", hostFlag, hostDir, err)
- } else if !info.IsDir() {
+ hostDir, err := hostFlagToHostDir(f, hostFlag)
+ if err != nil {
f.Usage()
- return fmt.Errorf("Invalid host name %s, %s is not a directory", hostFlag, hostDir)
+ return err
}
- return runBuild(ctx, hostDir)
+ return runBuild(ctx, stderr, hostDir)
}
-func runBuild(ctx context.Context, hostDir string) error {
+func runBuild(ctx context.Context, stderr io.Writer, hostDir string) error {
wd, err := os.Getwd()
- defer os.Chdir(wd)
- os.Chdir(hostDir)
if err != nil {
- slog.Error("build", "hostDir", hostDir, "error", err)
return err
}
+ defer os.Chdir(wd)
+ os.Chdir(hostDir)
cmd := exec.CommandContext(ctx, "go", "build", "-ldflags", "-s -w -extldflags \"-static\"", hostDir)
cmd.Env = append(cmd.Environ(), "CGO_ENABLED=0")
- out, err := cmd.CombinedOutput()
- if err != nil {
- slog.Error("build", "hostDir", hostDir, "error", err, "combinedOutput", out)
+ if out, err := cmd.CombinedOutput(); err != nil {
+ fmt.Fprint(stderr, string(out))
return err
}
- slog.Debug("build", "hostDir", hostDir, "combinedOutput", out)
return nil
}
diff --git a/cmd/gonf/hostflag.go b/cmd/gonf/hostflag.go
new file mode 100644
index 0000000..f036545
--- /dev/null
+++ b/cmd/gonf/hostflag.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "errors"
+ "flag"
+ "fmt"
+ "os"
+ "path/filepath"
+)
+
+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) {
+ if hostFlag == nil {
+ return "", errors.New("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)
+ } else if !info.IsDir() {
+ return "", fmt.Errorf("invalid host name %s: %s is not a directory", *hostFlag, hostDir)
+ }
+ return hostDir, nil
+}
diff --git a/cmd/gonf/main.go b/cmd/gonf/main.go
index 548f287..8dae51d 100644
--- a/cmd/gonf/main.go
+++ b/cmd/gonf/main.go
@@ -25,7 +25,7 @@ func main() {
os.Stdout,
os.Stderr,
); err != nil {
- fmt.Fprintf(os.Stderr, "%s\n", err)
+ fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}