cmd/headscale/cli: switch to RunE with grpcRunE and error returns

Rename grpcRun to grpcRunE: the inner closure now returns error
and the wrapper returns a cobra RunE-compatible function.

Change newHeadscaleCLIWithConfig to return an error instead of
calling log.Fatal/os.Exit, making connection failures propagate
through the normal error path.

Add formatOutput (returns error) and printOutput (writes to stdout)
as non-exiting replacements for the old output/SuccessOutput pair.
Extract output format string literals into package-level constants.
Mark the old ErrorOutput, SuccessOutput and output helpers as
deprecated; they remain temporarily for the unconverted commands.

Convert all 22 grpcRunE commands from Run+ErrorOutput+SuccessOutput
to RunE+fmt.Errorf+printOutput. Change usernameAndIDFromFlag to
return an error instead of calling ErrorOutput directly.

Update backfillNodeIPsCmd and policy.go callers of
newHeadscaleCLIWithConfig for the new 5-return signature while
keeping their Run-based pattern for now.
This commit is contained in:
Kristoffer Dalby 2026-02-18 13:44:35 +00:00
parent e6546b2cea
commit e4fe216e45
8 changed files with 287 additions and 483 deletions

View file

@ -92,7 +92,10 @@ var getPolicy = &cobra.Command{
policy = pol.Data
} else {
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
ctx, client, conn, cancel, err := newHeadscaleCLIWithConfig()
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error connecting: %s", err), output)
}
defer cancel()
defer conn.Close()
@ -179,7 +182,10 @@ var setPolicy = &cobra.Command{
} else {
request := &v1.SetPolicyRequest{Policy: string(policyBytes)}
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
ctx, client, conn, cancel, err := newHeadscaleCLIWithConfig()
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error connecting: %s", err), output)
}
defer cancel()
defer conn.Close()