summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-03-08 00:41:29 +0100
committerJulien Dessaux2024-03-08 00:56:10 +0100
commitacbccb0a93756958b0f37fef12199271a4f4db3b (patch)
treef9776091d706483effc4346965b26fb15c57754f
parentchore(repo): renamed gonf subfolder to something more traditional in go land (diff)
downloadgonf-acbccb0a93756958b0f37fef12199271a4f4db3b.tar.gz
gonf-acbccb0a93756958b0f37fef12199271a4f4db3b.tar.bz2
gonf-acbccb0a93756958b0f37fef12199271a4f4db3b.zip
feat(gonf): bootstrapped the gonf cli
-rw-r--r--cmd/gonf/cmd_version.go31
-rw-r--r--cmd/gonf/main.go68
-rw-r--r--go.mod2
3 files changed, 100 insertions, 1 deletions
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