summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/gonf/cmd_build.go65
-rw-r--r--cmd/gonf/main.go3
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)