1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
}
|