chore(gonf): factorise hostflag management
This commit is contained in:
parent
345e4df508
commit
cae4735276
3 changed files with 38 additions and 28 deletions
|
@ -2,19 +2,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
hostFlag string
|
|
||||||
)
|
|
||||||
|
|
||||||
func cmdBuild(ctx context.Context,
|
func cmdBuild(ctx context.Context,
|
||||||
f *flag.FlagSet,
|
f *flag.FlagSet,
|
||||||
args []string,
|
args []string,
|
||||||
|
@ -23,43 +17,33 @@ func cmdBuild(ctx context.Context,
|
||||||
) error {
|
) error {
|
||||||
f.Init(`gonf build [-FLAG]
|
f.Init(`gonf build [-FLAG]
|
||||||
where FLAG can be one or more of`, flag.ContinueOnError)
|
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.SetOutput(stderr)
|
||||||
f.Parse(args)
|
f.Parse(args)
|
||||||
if helpMode {
|
if helpMode {
|
||||||
f.SetOutput(stdout)
|
f.SetOutput(stdout)
|
||||||
f.Usage()
|
f.Usage()
|
||||||
}
|
}
|
||||||
if hostFlag == "" {
|
hostDir, err := hostFlagToHostDir(f, hostFlag)
|
||||||
|
if err != nil {
|
||||||
f.Usage()
|
f.Usage()
|
||||||
return errors.New("Required -host FLAG is missing")
|
return err
|
||||||
}
|
}
|
||||||
hostDir := configDir + "/hosts/" + hostFlag
|
return runBuild(ctx, stderr, hostDir)
|
||||||
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 {
|
func runBuild(ctx context.Context, stderr io.Writer, hostDir string) error {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
defer os.Chdir(wd)
|
defer os.Chdir(wd)
|
||||||
os.Chdir(hostDir)
|
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 := exec.CommandContext(ctx, "go", "build", "-ldflags", "-s -w -extldflags \"-static\"", hostDir)
|
||||||
cmd.Env = append(cmd.Environ(), "CGO_ENABLED=0")
|
cmd.Env = append(cmd.Environ(), "CGO_ENABLED=0")
|
||||||
out, err := cmd.CombinedOutput()
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
if err != nil {
|
fmt.Fprint(stderr, string(out))
|
||||||
slog.Error("build", "hostDir", hostDir, "error", err, "combinedOutput", out)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
slog.Debug("build", "hostDir", hostDir, "combinedOutput", out)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
26
cmd/gonf/hostflag.go
Normal file
26
cmd/gonf/hostflag.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -25,7 +25,7 @@ func main() {
|
||||||
os.Stdout,
|
os.Stdout,
|
||||||
os.Stderr,
|
os.Stderr,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
fmt.Fprintf(os.Stderr, "%+v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue