diff --git a/docs/quickstart.md b/docs/quickstart.md index 6b31682..b0c0137 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -36,7 +36,7 @@ Using deploy-rs? Skip the file: `sshwarm --flake ./nix`. See [configuration](con ```sh sshwarm # browse -sshwarm status # print the table, exit 1 on any problem +sshwarm status # print the table, exit 1 on any problem, 2 on an error sshwarm status --format json # for scripts ``` diff --git a/main.go b/main.go index dd5d7be..46a971b 100644 --- a/main.go +++ b/main.go @@ -28,8 +28,8 @@ const ( defaultRefresh = 30 * time.Second ) -// errReported means the output already says what's wrong (status's problems): -// sshwarm exits 1 without repeating it. +// errReported means status found problems, which its output already lists: +// sshwarm exits 1 without repeating them. var errReported = errors.New("reported") // version and commit are stamped by the Nix build. @@ -50,8 +50,7 @@ func main() { os.Exit(run(context.Background(), os.Args[1:], os.Stdout, os.Stderr, os.Getenv)) } -// run runs sshwarm with args, and returns its exit status. It writes to stdout, its -// errors to stderr, and reads the environment with getenv. +// run runs sshwarm with args, and returns its exit status: 1 when status found problems, 2 on errors. func run(ctx context.Context, args []string, stdout, stderr io.Writer, getenv func(string) string) int { err := dispatch(ctx, args, stdout, getenv) if errors.Is(err, flag.ErrHelp) { // sshwarm -h @@ -61,11 +60,13 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer, getenv fu switch { case err == nil: return 0 - case !errors.Is(err, errReported): // else the output already says what's wrong - _, _ = fmt.Fprintf(stderr, "sshwarm: %v\n", err) // nowhere left to say it failed + case errors.Is(err, errReported): + return 1 } - return 1 + _, _ = fmt.Fprintf(stderr, "sshwarm: %v\n", err) // nowhere left to say it failed + + return 2 } // dispatch parses the global flags, then runs the command first in the arguments @@ -185,8 +186,9 @@ func envOn(v string) bool { const statusHelp = `Usage: sshwarm [flags] status [--format text|json] [machine...] -Print the machines table and exit 1 on any problem. Machines can -be selected by substrings of their name. All machines by default. +Print the machines table and exit 1 on any problem, 2 on an error. +Machines can be selected by substrings of their name. All machines by +default. --format text|json output format. Text by default. ` @@ -324,7 +326,7 @@ func usage(w io.Writer) error { _, err := fmt.Fprintf(w, `Usage: sshwarm [flags] [command] [machine...] (no command) browse the machines, their services, stacks and logs - status [machine...] print the machines table; exit 1 on any problem + status [machine...] print the machines table; exit 1 on any problem, 2 on an error probe print a machine's raw probe output (debugging) Machine can be selected by substrings of their name. See a command's own flags diff --git a/main_test.go b/main_test.go index 386f116..ea5d0b7 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "flag" + "fmt" "os" "path/filepath" "strings" @@ -29,9 +30,11 @@ func TestHelp(t *testing.T) { "status --help": "Usage: sshwarm [flags] status", "probe -h": "Usage: sshwarm [flags] probe", } { - if code, out, errs := sshwarm(t, nil, strings.Fields(args)...); code != 0 || !strings.HasPrefix(out, want) || errs != "" { - t.Errorf("sshwarm %s: exit %d, stdout %.40q, stderr %q", args, code, out, errs) - } + t.Run(args, func(t *testing.T) { + if code, out, errs := sshwarm(t, nil, strings.Fields(args)...); code != 0 || !strings.HasPrefix(out, want) || errs != "" { + t.Errorf("exit %d, stdout %.40q, stderr %q", code, out, errs) + } + }) } } @@ -58,16 +61,18 @@ func TestHelpListsFlags(t *testing.T) { } } -// A mistake says what's wrong on stderr, and exits 1. +// A mistake says what's wrong on stderr, and exits 2: 1 is status's problems. func TestErrors(t *testing.T) { for args, want := range map[string]string{ "nope": `unknown command "nope"`, "--bogus": "flag provided but not defined: -bogus", "status --format yaml": `unknown format "yaml"`, } { - if code, out, errs := sshwarm(t, nil, strings.Fields(args)...); code != 1 || out != "" || !strings.Contains(errs, want) { - t.Errorf("sshwarm %s: exit %d, stdout %q, stderr %q", args, code, out, errs) - } + t.Run(args, func(t *testing.T) { + if code, out, errs := sshwarm(t, nil, strings.Fields(args)...); code != 2 || out != "" || !strings.Contains(errs, want) { + t.Errorf("exit %d, stdout %q, stderr %q", code, out, errs) + } + }) } } @@ -127,24 +132,27 @@ func TestHostSource(t *testing.T) { t.Fatal(err) } - for _, c := range []struct { + for _, tt := range []struct { + name string env map[string]string args, want string // want is in stdout or stderr }{ - {map[string]string{"HOME": home}, "status", "web disk"}, - {map[string]string{"XDG_CONFIG_HOME": config}, "status", "web disk"}, - {map[string]string{"XDG_CONFIG_HOME": "relative", "HOME": home}, "status", "web disk"}, - {map[string]string{"HOME": empty}, "status", "or write " + filepath.Join(empty, ".config", "sshwarm", "hosts.toml")}, - {nil, "status", "or write $XDG_CONFIG_HOME/sshwarm/hosts.toml"}, - {map[string]string{"SSHWARM_HOSTS": hosts}, "--flake . status", "the flake"}, - {map[string]string{"SSHWARM_FLAKE": "."}, "--hosts " + hosts + " status", "web disk"}, - {map[string]string{"SSHWARM_HOSTS": hosts, "SSHWARM_FLAKE": "."}, "status", "choose one"}, - {nil, "--hosts " + hosts + " --flake . status", "choose one"}, - {map[string]string{"SSHWARM_TIMEOUT": "soon"}, "status", `SSHWARM_TIMEOUT: time: invalid duration "soon"`}, + {"default file in home", map[string]string{"HOME": home}, "status", "web disk"}, + {"default file in xdg config home", map[string]string{"XDG_CONFIG_HOME": config}, "status", "web disk"}, + {"relative xdg config home ignored", map[string]string{"XDG_CONFIG_HOME": "relative", "HOME": home}, "status", "web disk"}, + {"no default file names its path", map[string]string{"HOME": empty}, "status", "or write " + filepath.Join(empty, ".config", "sshwarm", "hosts.toml")}, + {"no home names the variable", nil, "status", "or write $XDG_CONFIG_HOME/sshwarm/hosts.toml"}, + {"flake flag beats hosts variable", map[string]string{"SSHWARM_HOSTS": hosts}, "--flake . status", "the flake"}, + {"hosts flag beats flake variable", map[string]string{"SSHWARM_FLAKE": "."}, "--hosts " + hosts + " status", "web disk"}, + {"both variables", map[string]string{"SSHWARM_HOSTS": hosts, "SSHWARM_FLAKE": "."}, "status", "choose one"}, + {"both flags", nil, "--hosts " + hosts + " --flake . status", "choose one"}, + {"invalid timeout variable", map[string]string{"SSHWARM_TIMEOUT": "soon"}, "status", `SSHWARM_TIMEOUT: time: invalid duration "soon"`}, } { - if _, out, errs := sshwarm(t, c.env, strings.Fields(c.args)...); !strings.Contains(out+errs, c.want) { - t.Errorf("%v sshwarm %s: no %q in stdout %q, stderr %q", c.env, c.args, c.want, out, errs) - } + t.Run(tt.name, func(t *testing.T) { + if _, out, errs := sshwarm(t, tt.env, strings.Fields(tt.args)...); !strings.Contains(out+errs, tt.want) { + t.Errorf("%v sshwarm %s: no %q in stdout %q, stderr %q", tt.env, tt.args, tt.want, out, errs) + } + }) } } @@ -175,8 +183,10 @@ func TestVersion(t *testing.T) { // SSHWARM_READ_ONLY is on when set, but to a false value: a typo errs on the safe side. func TestEnvOn(t *testing.T) { for v, want := range map[string]bool{"": false, "false": false, "true": true, "yes": true} { - if got := envOn(v); got != want { - t.Errorf("SSHWARM_READ_ONLY=%q: on %v, want %v", v, got, want) - } + t.Run(fmt.Sprintf("%q", v), func(t *testing.T) { + if got := envOn(v); got != want { + t.Errorf("SSHWARM_READ_ONLY=%q: on %v, want %v", v, got, want) + } + }) } }