diff options
author | Julien Dessaux | 2024-04-16 00:09:25 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-04-16 00:09:25 +0200 |
commit | 345e4df5088b69f718ffab552c44ca1fd5696e34 (patch) | |
tree | 5f00dec1920ca29ed15ba761a4ffd1977c42c60e /cmd | |
parent | feat(gonf): implement config directory flag and env variable (diff) | |
download | gonf-345e4df5088b69f718ffab552c44ca1fd5696e34.tar.gz gonf-345e4df5088b69f718ffab552c44ca1fd5696e34.tar.bz2 gonf-345e4df5088b69f718ffab552c44ca1fd5696e34.zip |
feat(gonf): implement the gonf build cli command
Diffstat (limited to '')
-rw-r--r-- | cmd/gonf/cmd_build.go | 65 | ||||
-rw-r--r-- | cmd/gonf/main.go | 3 |
2 files changed, 68 insertions, 0 deletions
diff --git a/cmd/gonf/cmd_build.go b/cmd/gonf/cmd_build.go new file mode 100644 index 0000000..0e7326a --- /dev/null +++ b/cmd/gonf/cmd_build.go @@ -0,0 +1,65 @@ +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, + getenv func(string) string, + stdout, stderr io.Writer, +) 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") + 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() { + f.Usage() + return fmt.Errorf("Invalid host name %s, %s is not a directory", hostFlag, hostDir) + } + return runBuild(ctx, hostDir) +} + +func runBuild(ctx context.Context, 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 + } + 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) + return err + } + slog.Debug("build", "hostDir", hostDir, "combinedOutput", out) + return nil +} diff --git a/cmd/gonf/main.go b/cmd/gonf/main.go index 1e28368..548f287 100644 --- a/cmd/gonf/main.go +++ b/cmd/gonf/main.go @@ -56,6 +56,7 @@ where FLAG can be one or more of`, flag.ContinueOnError) return errors.New("No command given") } cmd := f.Arg(0) + argsTail := f.Args()[1:] switch cmd { case "help": f.SetOutput(stdout) @@ -71,6 +72,8 @@ where FLAG can be one or more of`, flag.ContinueOnError) } } switch cmd { + case "build": + return cmdBuild(ctx, f, argsTail, getenv, stdout, stderr) default: f.Usage() return fmt.Errorf("Invalid command: %s", cmd) |