Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions cmd/pullgate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -48,13 +50,18 @@ 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)
return 0
}
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")
Expand Down
71 changes: 71 additions & 0 deletions cmd/pullgate/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading