diff --git a/README.md b/README.md index bf7e203..4d5bbaa 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,10 @@ systemctl enable --now pullgate-check@NAME.path # optional: /check Every step logs to the journal: `journalctl -t pullgate`. +To see the version of a binary, run `pullgate --version` or +`pullgate version`. Both print `pullgate X.Y.Z` and exit 0. A build from +the source without `-ldflags "-X main.version=..."` prints `pullgate dev`. + ## Check now: `/check` The timer runs every 5 minutes. After a push you do not have to wait: send diff --git a/cmd/pullgate/main.go b/cmd/pullgate/main.go index aeec330..02f01c3 100644 --- a/cmd/pullgate/main.go +++ b/cmd/pullgate/main.go @@ -23,6 +23,8 @@ import ( var version = "dev" const usage = `usage: pullgate [--config-dir DIR] [--state-dir DIR] [--spool-dir DIR] COMMAND + pullgate --version | -version print the version (same as: pullgate version) + pullgate --help commands: run NAME fetch, check, and ask (the timer runs it, as root) apply NAME act on approvals (the path unit runs it, as root) @@ -48,6 +50,7 @@ func run(args []string, stdout, stderr io.Writer) int { configDir := fs.String("config-dir", "/etc/pullgate", "") stateDir := fs.String("state-dir", "/var/lib/pullgate", "") spoolDir := fs.String("spool-dir", "/var/spool/pullgate", "") + showVersion := fs.Bool("version", false, "") if err := fs.Parse(args); err != nil { if errors.Is(err, flag.ErrHelp) { _, _ = fmt.Fprintln(stdout, usage) @@ -55,6 +58,10 @@ func run(args []string, stdout, stderr io.Writer) int { } return usageErr(stderr, err.Error()) } + if *showVersion { + _, _ = fmt.Fprintln(stdout, "pullgate", version) + return 0 + } rest := fs.Args() if len(rest) == 0 { return usageErr(stderr, "give a command: run, apply, status, listen or version") diff --git a/cmd/pullgate/main_test.go b/cmd/pullgate/main_test.go new file mode 100644 index 0000000..a10f378 --- /dev/null +++ b/cmd/pullgate/main_test.go @@ -0,0 +1,71 @@ +package main + +import ( + "bytes" + "strings" + "testing" +) + +func runArgs(t *testing.T, args ...string) (int, string, string) { + t.Helper() + var out, errOut bytes.Buffer + code := run(args, &out, &errOut) + return code, out.String(), errOut.String() +} + +func TestVersionForms(t *testing.T) { + old := version + version = "1.2.3" + t.Cleanup(func() { version = old }) + cases := [][]string{ + {"version"}, + {"--version"}, + {"-version"}, + {"--config-dir", "/x", "--version"}, + {"--config-dir=/x", "--state-dir", "/y", "--spool-dir", "/z", "-version"}, + // --version wins over a command: it reads no config. + {"--version", "run", "example"}, + } + for _, args := range cases { + code, out, errOut := runArgs(t, args...) + if code != 0 || out != "pullgate 1.2.3\n" || errOut != "" { + t.Errorf("pullgate %v: exit %d, stdout %q, stderr %q; want exit 0, stdout %q, no stderr", + args, code, out, errOut, "pullgate 1.2.3\n") + } + } +} + +func TestVersionDeny(t *testing.T) { + cases := [][]string{ + {"--version=maybe"}, // not a bool + {"--versions"}, // unknown flag + {"--config-dir", "--version"}, // --version is the value of --config-dir, no command + // --version is a global flag: after a command it is an unknown argument. + {"run", "example", "--version"}, + {"status", "example", "--version"}, + {"listen", "--version"}, + } + for _, args := range cases { + code, out, errOut := runArgs(t, args...) + if code != 64 || out != "" || !strings.Contains(errOut, "pullgate --help") { + t.Errorf("pullgate %v: exit %d, stdout %q, stderr %q; want exit 64, no stdout, a hint", args, code, out, errOut) + } + } + // --version=false is no version request: a command is still needed. + code, out, _ := runArgs(t, "--version=false") + if code != 64 || out != "" { + t.Errorf("pullgate --version=false: exit %d, stdout %q; want exit 64, no stdout", code, out) + } +} + +func TestHelpListsVersion(t *testing.T) { + for _, h := range []string{"--help", "-h"} { + code, out, _ := runArgs(t, h) + if code != 0 { + t.Fatalf("pullgate %s: exit %d, want 0", h, code) + } + if !strings.Contains(out, "--version") || !strings.Contains(out, "\n version") { + t.Errorf("pullgate %s does not list --version and version:\n%s", h, out) + } + } +}