From acbccb0a93756958b0f37fef12199271a4f4db3b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 8 Mar 2024 00:41:29 +0100 Subject: feat(gonf): bootstrapped the gonf cli --- cmd/gonf/cmd_version.go | 31 ++++++++++++++++++++++ cmd/gonf/main.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 cmd/gonf/cmd_version.go create mode 100644 cmd/gonf/main.go diff --git a/cmd/gonf/cmd_version.go b/cmd/gonf/cmd_version.go new file mode 100644 index 0000000..5cf7fb8 --- /dev/null +++ b/cmd/gonf/cmd_version.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "runtime/debug" +) + +var buildRevision, buildTime, buildModified string + +func init() { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + buildRevision = setting.Value + case "vcs.modified": + buildModified = setting.Value + case "vcs.time": + buildTime = setting.Value + } + } + } +} + +func cmdVersion() { + modified := "clean" + if buildModified == "true" { + modified = "dirty" + } + fmt.Printf("gonf - %s %s %s\n", buildRevision, buildTime, modified) +} diff --git a/cmd/gonf/main.go b/cmd/gonf/main.go new file mode 100644 index 0000000..882636a --- /dev/null +++ b/cmd/gonf/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "context" + "errors" + "flag" + "fmt" + "io" + "os" + "os/signal" +) + +var ( + batchMode bool + helpMode bool +) + +func main() { + if err := run(context.Background(), + os.Args, + //os.Getenv, + //os.Getwd, + //os.Stdin, + os.Stdout, + os.Stderr, + ); err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } +} + +func run(ctx context.Context, + args []string, + //getenv func(string) string, + //getwd func() (string, error), + //stdin io.Reader, + stdout, stderr io.Writer, +) error { + ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) + defer cancel() + f := flag.NewFlagSet(`gonf COMMAND [-FLAG] +where COMMAND is one of: + * build: build configurations for one or more hosts + * help: show contextual help + * version: show build version and time +where FLAG can be one or more of`, flag.ContinueOnError) + f.BoolVar(&batchMode, "batch", false, "skips all questions and confirmations, using the default (safe) choices each time") + f.BoolVar(&helpMode, "help", false, "show contextual help") + f.SetOutput(stderr) + f.Parse(args[1:]) + + if f.NArg() < 1 { + f.Usage() + return errors.New("No command given") + } + cmd := f.Arg(0) + switch cmd { + case "help": + f.SetOutput(stdout) + f.Usage() + case "version": + cmdVersion() + default: + f.Usage() + return fmt.Errorf("Invalid command: %s", cmd) + } + return nil +} diff --git a/go.mod b/go.mod index 5163144..d220e11 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module git.adyxax.org/adyxax/gonf/v2 -go 1.22.0 +go 1.22.1 -- cgit v1.2.3