From 27fe54cb16ec336846c3f543f9bf3e59049d075b Mon Sep 17 00:00:00 2001 From: cmdoret Date: Thu, 3 Dec 2026 22:43:10 +0100 Subject: [PATCH 1/5] test: add probe test data --- internal/swarm/testdata/argiope.probe | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/swarm/testdata/argiope.probe b/internal/swarm/testdata/argiope.probe index d7c3662..2b93be0 100644 --- a/internal/swarm/testdata/argiope.probe +++ b/internal/swarm/testdata/argiope.probe @@ -8,6 +8,7 @@ reboot=true load=12.5 memory=74.2 disk=93.1 +temp=71.5 docker=sudo -n docker ### temps /sys/class/hwmon/hwmon0/name:acpitz From c22aa3e6d72b5519e0014915ee085ab1e48bfea8 Mon Sep 17 00:00:00 2001 From: cmdoret Date: Thu, 3 Dec 2026 23:08:55 +0100 Subject: [PATCH 2/5] feat(probe): read temperature --- internal/swarm/probe.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/swarm/probe.sh b/internal/swarm/probe.sh index 76083e7..4e752eb 100644 --- a/internal/swarm/probe.sh +++ b/internal/swarm/probe.sh @@ -73,6 +73,9 @@ echo "memory=$(awk '/^MemTotal:/ { total = $2 } /^MemAvailable:/ { free = $2 } E # time the whole probe out. echo "disk=$(timeout 2 df -P -l -x tmpfs -x devtmpfs -x overlay -x squashfs -x efivarfs 2>/dev/null | awk 'NR > 1 && $3 + $4 > 0 { used = 100 * $3 / ($3 + $4); if (used > disk) disk = used } END { print disk }')" +# The hottest sensor in °C (none in VMs). Skip >125°C (maxTemp) as unconnected input. +echo "temp=$(cat /sys/class/hwmon/hwmon*/temp*_input 2>/dev/null | + awk '$1 > t && $1 < 125000 { t = $1 } END { if (t) print t / 1000 }')" docker=$(docker_cmd) echo "docker=$docker" From 08c5b92de87832dab7fc218ac062e99a5468c5e3 Mon Sep 17 00:00:00 2001 From: cmdoret Date: Thu, 3 Dec 2026 23:44:58 +0100 Subject: [PATCH 3/5] feat(swarm): parse temperatures --- internal/swarm/probe.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/swarm/probe.go b/internal/swarm/probe.go index 4996899..299d2da 100644 --- a/internal/swarm/probe.go +++ b/internal/swarm/probe.go @@ -119,6 +119,7 @@ func Parse(out string) (Report, error) { Load: usage(kv["load"]), Memory: usage(kv["memory"]), Disk: usage(kv["disk"]), + Temp: usage(kv["temp"]), Docker: Docker(kv["docker"]), Units: withUnloaded(parseUnits(sec["units"], now, bad), sec["unit-files"], bad), Stacks: parseStacks(sec["containers"], parseStates(sec["states"], now, bad), bad), From 409854913a2041a7208f745cd9e818ca6820e47c Mon Sep 17 00:00:00 2001 From: cmdoret Date: Fri, 4 Dec 2026 00:48:38 +0100 Subject: [PATCH 4/5] refactor: centralize process and sensor filtering --- internal/swarm/probe.go | 1 - internal/swarm/probe.sh | 3 --- internal/swarm/testdata/argiope.probe | 1 - 3 files changed, 5 deletions(-) diff --git a/internal/swarm/probe.go b/internal/swarm/probe.go index 299d2da..4996899 100644 --- a/internal/swarm/probe.go +++ b/internal/swarm/probe.go @@ -119,7 +119,6 @@ func Parse(out string) (Report, error) { Load: usage(kv["load"]), Memory: usage(kv["memory"]), Disk: usage(kv["disk"]), - Temp: usage(kv["temp"]), Docker: Docker(kv["docker"]), Units: withUnloaded(parseUnits(sec["units"], now, bad), sec["unit-files"], bad), Stacks: parseStacks(sec["containers"], parseStates(sec["states"], now, bad), bad), diff --git a/internal/swarm/probe.sh b/internal/swarm/probe.sh index 4e752eb..76083e7 100644 --- a/internal/swarm/probe.sh +++ b/internal/swarm/probe.sh @@ -73,9 +73,6 @@ echo "memory=$(awk '/^MemTotal:/ { total = $2 } /^MemAvailable:/ { free = $2 } E # time the whole probe out. echo "disk=$(timeout 2 df -P -l -x tmpfs -x devtmpfs -x overlay -x squashfs -x efivarfs 2>/dev/null | awk 'NR > 1 && $3 + $4 > 0 { used = 100 * $3 / ($3 + $4); if (used > disk) disk = used } END { print disk }')" -# The hottest sensor in °C (none in VMs). Skip >125°C (maxTemp) as unconnected input. -echo "temp=$(cat /sys/class/hwmon/hwmon*/temp*_input 2>/dev/null | - awk '$1 > t && $1 < 125000 { t = $1 } END { if (t) print t / 1000 }')" docker=$(docker_cmd) echo "docker=$docker" diff --git a/internal/swarm/testdata/argiope.probe b/internal/swarm/testdata/argiope.probe index 2b93be0..d7c3662 100644 --- a/internal/swarm/testdata/argiope.probe +++ b/internal/swarm/testdata/argiope.probe @@ -8,7 +8,6 @@ reboot=true load=12.5 memory=74.2 disk=93.1 -temp=71.5 docker=sudo -n docker ### temps /sys/class/hwmon/hwmon0/name:acpitz From 1ec3ab8b143dd43ffac0b6e45b302dc1c1b6c38e Mon Sep 17 00:00:00 2001 From: cmdoret Date: Fri, 4 Dec 2026 01:10:04 +0100 Subject: [PATCH 5/5] feat(cli): separate exit code for reporting problems --- docs/quickstart.md | 2 +- main.go | 22 ++++++++++-------- main_test.go | 58 +++++++++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 35 deletions(-) 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) + } + }) } }