feat(maglevc): JSON output for all commands (-json)
Enable App.JSON and add -json on golang-cli v1.4.0, mirroring evpnc so
the two CLIs' JSON contract is identical: show/query -> data, set/action
-> {}, failure -> {"error": "..."}.
- show/query commands branch on cli.IsJSON() -> emit the protobuf via
cli.EmitJSON; text keeps the tabwriter painters (which robot tests
parse via show, not via setter output).
- action commands (pause/resume/enable/disable, set weight, sync,
config reload) are now silent on success in text too — "we did what
you asked" needs no confirmation — and print "{}" in JSON via wrapJSON.
- config check stays informative (it is a query): text "config ok" /
error, JSON the CheckConfig report.
- errors: formatError returns {"error": "..."} in JSON mode.
- watch streams its own JSON events (no trailing {}).
Robot tests assert backend state via `show`, not setter stdout, so the
dropped confirmations don't affect them. Builds on linux and openbsd.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+9
-4
@@ -3,6 +3,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
cli "git.ipng.ch/ipng/golang-cli"
|
||||
@@ -10,10 +11,10 @@ import (
|
||||
|
||||
// formatError returns a user-friendly error string. gRPC status errors are
|
||||
// unwrapped to show only the server's message (no "rpc error: code = ..."
|
||||
// boilerplate). The result is wrapped in red when color is enabled. The label()
|
||||
// helper and the ANSI palette now live in the golang-cli library (cli.Label,
|
||||
// cli.Red, ...); only this gRPC-specific unwrap stays here, wired in as
|
||||
// App.FormatError.
|
||||
// boilerplate). In JSON mode it returns the message as a {"error": "..."}
|
||||
// document (so failures are machine-readable, matching the data/`{}` a success
|
||||
// prints); otherwise it wraps the message in red when color is enabled.
|
||||
// App.FormatError prints the result.
|
||||
func formatError(err error) string {
|
||||
msg := err.Error()
|
||||
// google.golang.org/grpc/status errors format as:
|
||||
@@ -21,6 +22,10 @@ func formatError(err error) string {
|
||||
if i := strings.Index(msg, " desc = "); i >= 0 {
|
||||
msg = msg[i+len(" desc = "):]
|
||||
}
|
||||
if cli.IsJSON() {
|
||||
b, _ := json.Marshal(map[string]string{"error": msg})
|
||||
return string(b)
|
||||
}
|
||||
if cli.ColorEnabled() {
|
||||
return cli.Red + msg + cli.Reset
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user