cmd/headscale/cli: silence cobra error/usage output and centralise error formatting

Set SilenceErrors and SilenceUsage on the root command so that
cobra never prints usage text for runtime errors. A SetFlagErrorFunc
callback re-enables usage output specifically for flag-parsing
errors (the kubectl pattern).

Add printError to utils.go and switch Execute() to ExecuteC() so
the returned error is formatted as JSON/YAML when --output requests
machine-readable output.
This commit is contained in:
Kristoffer Dalby 2026-02-18 13:36:28 +00:00
parent aae2f7de71
commit e6546b2cea
2 changed files with 41 additions and 3 deletions

View file

@ -203,6 +203,34 @@ func ErrorOutput(errResult error, override string, outputFormat string) {
os.Exit(1)
}
// printError writes err to stderr, formatting it as JSON/YAML when the
// --output flag requests machine-readable output. Used exclusively by
// Execute() so that every error surfaces in the format the caller asked for.
func printError(err error, outputFormat string) {
type errOutput struct {
Error string `json:"error"`
}
e := errOutput{Error: err.Error()}
var formatted []byte
switch outputFormat {
case "json":
formatted, _ = json.MarshalIndent(e, "", "\t") //nolint:errchkjson // errOutput contains only a string field
case "json-line":
formatted, _ = json.Marshal(e) //nolint:errchkjson // errOutput contains only a string field
case "yaml":
formatted, _ = yaml.Marshal(e)
default:
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return
}
fmt.Fprintf(os.Stderr, "%s\n", formatted)
}
func HasMachineOutputFlag() bool {
for _, arg := range os.Args {
if arg == "json" || arg == "json-line" || arg == "yaml" {