carlos / platform Public

cli: named-units put and show return errors to main

The code-health ratchet holds log.Fatal and os.Exit in the root package,
and new CLI commands return errors (AGENTS.md). The tree's root gets an
OnError, commandFailed, that keeps a leaf's error for main; main exits
through its one os.Exit with exitStatus's code. exitError carries a
status other than 1, or text that prints as it is (a usage).

put and show register with clireg.RawArgsErr. Exit codes and text are
unchanged: -h exits 0, a flag error 2, any other error prints
"carlos system named-units <verb>: <err>" and exits 1. The fallback's
bare (2) and unknown-word (1) cases go through commandFailed too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 201922cae2814fa1c75cf52d5741e2f552e35cad parent 0272e82
3 files changed, +91 −23
  • commands.go +3 −0
  • main.go +67 −6
  • system_namedunits.go +21 −17
diff --git a/commands.go b/commands.go
index 538b8ce32..33c49cd86 100644
--- a/commands.go
+++ b/commands.go
@@ -179,6 +179,9 @@ func buildCommandRoot() *clireg.Node {
usage(os.Stderr)
os.Exit(2)
},
+ // A leaf's error that no nearer OnError takes: main prints it and
+ // exits (runCommand).
+ OnError: commandFailed,
}
}
diff --git a/main.go b/main.go
index a2c62efe8..4477f9e62 100644
--- a/main.go
+++ b/main.go
@@ -3,6 +3,7 @@
package main
import (
+ "errors"
"fmt"
"io"
"log"
@@ -28,16 +29,76 @@ func main() {
// than at a few hundred call sites. Off unless stderr is a terminal, so
// journald and CI logs see exactly what they saw before.
log.SetOutput(newErrorWriter(os.Stderr))
+ code := 2
if len(os.Args) < 2 {
usage(os.Stderr)
- os.Exit(2)
+ } else if code = runCommand(os.Args[1:]); code == 0 {
+ return
+ }
+ os.Exit(code)
+}
+
+// runCommand walks the command tree with args and returns main's exit
+// status. After a verb that ran to success it is 0, following the
+// once-a-day newer-release hint (#161). A verb that fails never gets the
+// hint, so a broken command is never followed by an upsell: most exit
+// through log.Fatal before Dispatch returns, and one that returns its
+// error (commandFailed) ends with exitStatus's. Paths through a
+// NoUpdateHint node skip the hint (maybeUpdateHint).
+func runCommand(args []string) int {
+ path := clireg.Dispatch(commandRoot(), args)
+ if failed.err != nil {
+ return exitStatus(failed.path, failed.err)
}
- path := clireg.Dispatch(commandRoot(), os.Args[1:])
- // After a verb that ran to success: the once-a-day newer-release hint
- // (#161). Verbs that fail never reach here (log.Fatal exits), so a
- // broken command is never followed by an upsell. Paths through a
- // NoUpdateHint node skip it (maybeUpdateHint).
maybeUpdateHint(path)
+ return 0
+}
+
+// failed is the error a command handed to commandFailed, with the words
+// it ran under; runCommand ends the binary with it.
+var failed struct {
+ path []string
+ err error
+}
+
+// commandFailed is the command tree root's OnError: the error path for
+// commands that return their errors rather than exit (the code-health
+// rule, AGENTS.md "Writing Go here"). It keeps the error for runCommand.
+// A branch's Fallback, which returns nothing, may call it too.
+func commandFailed(path []string, err error) {
+ failed.path, failed.err = path, err
+}
+
+// exitError ends the binary with its code, printing err as log.Print
+// would, or nothing for a nil err: for an error whose text is its own
+// (a usage), or one the command has already printed.
+type exitError struct {
+ code int
+ err error
+}
+
+func (e *exitError) Error() string {
+ if e.err == nil {
+ return fmt.Sprintf("exit status %d", e.code)
+ }
+ return e.err.Error()
+}
+
+func (e *exitError) Unwrap() error { return e.err }
+
+// exitStatus prints a failed command's error and returns its exit status:
+// an exitError's own code, or 1 after "carlos <words>: <err>", which is
+// what log.Fatalf with that text prints.
+func exitStatus(path []string, err error) int {
+ var ee *exitError
+ if errors.As(err, &ee) {
+ if ee.err != nil {
+ log.Print(ee.err)
+ }
+ return ee.code
+ }
+ log.Printf("carlos %s: %v", strings.Join(path, " "), err)
+ return 1
}
// usage prints the top-level command list, grouped by the role you are in
diff --git a/system_namedunits.go b/system_namedunits.go
index d31d6da84..8553698a8 100644
--- a/system_namedunits.go
+++ b/system_namedunits.go
@@ -7,7 +7,6 @@ import (
"flag"
"fmt"
"io"
- "log"
"net/http"
"os"
"strings"
@@ -60,7 +59,9 @@ var namedUnitsOutcomeText = map[string]string{
// systemNamedUnitsCommand is `carlos system named-units <install|put|show>`.
// Bare is the usage on stderr, exit 2; a help word the usage on stdout,
-// exit 0; any other word log.Fatal with the usage, exit 1.
+// exit 0; any other word the usage through log, exit 1. put and show
+// return their errors to the tree's root (commandFailed); main prints them
+// and exits.
func systemNamedUnitsCommand() *clireg.Node {
return &clireg.Node{
Name: "named-units",
@@ -68,23 +69,24 @@ func systemNamedUnitsCommand() *clireg.Node {
Children: []*clireg.Node{
// Root's, on the box; refuses to run as anyone else.
raw("install", clireg.BoxLocal, host.RunSystemNamedUnitsInstall),
- raw("put", clireg.Operator, func(args []string) {
- exitNamedUnits("put", runNamedUnitsPut(context.Background(), args, stdout, hostenv.OpenNamedUnitsStore))
- }),
- raw("show", clireg.Operator, func(args []string) {
- exitNamedUnits("show", runNamedUnitsShow(context.Background(), args, stdout, hostenv.OpenNamedUnitsStore, consoleNamedUnitsBoxes))
- }),
+ {Name: "put", Audience: clireg.Operator, Run: clireg.RawArgsErr(func(args []string) error {
+ return namedUnitsErr(runNamedUnitsPut(context.Background(), args, stdout, hostenv.OpenNamedUnitsStore))
+ })},
+ {Name: "show", Audience: clireg.Operator, Run: clireg.RawArgsErr(func(args []string) error {
+ return namedUnitsErr(runNamedUnitsShow(context.Background(), args, stdout, hostenv.OpenNamedUnitsStore, consoleNamedUnitsBoxes))
+ })},
},
- Fallback: func(args, _ []string) {
+ Fallback: func(args, consumed []string) {
if len(args) == 0 {
fmt.Fprint(os.Stderr, namedUnitsUsage)
- os.Exit(2)
+ commandFailed(consumed, &exitError{code: 2})
+ return
}
if args[0] == "help" || args[0] == "--help" || args[0] == "-h" {
fmt.Fprint(stdout, namedUnitsUsage)
return
}
- log.Fatal(namedUnitsUsage)
+ commandFailed(consumed, &exitError{code: 1, err: errors.New(namedUnitsUsage)})
},
}
}
@@ -96,16 +98,18 @@ var namedUnitsNow = time.Now
// printed the problem and the usage.
var errNamedUnitsUsage = errors.New("usage")
-func exitNamedUnits(verb string, err error) {
+// namedUnitsErr maps put's and show's errors for the root's error path:
+// -h's ErrHelp is no error (the flag set printed the usage), a usage error
+// exits 2 with nothing more (the flag set printed it), and any other error
+// prints as "carlos system named-units <verb>: <err>", exit 1.
+func namedUnitsErr(err error) error {
switch {
- case err == nil:
case errors.Is(err, flag.ErrHelp):
- os.Exit(0)
+ return nil
case errors.Is(err, errNamedUnitsUsage):
- os.Exit(2)
- default:
- log.Fatalf("carlos system named-units %s: %v", verb, err)
+ return &exitError{code: 2}
}
+ return err
}
// namedUnitsFlags parses --instance and --aws-region (and --console for