cmd/headscale/cli: remove nil resp guards and unexport HasMachineOutputFlag

Remove dead if-resp-nil checks in tagCmd and approveRoutesCmd; gRPC
returns either an error or a valid response, never (nil, nil).

Rename HasMachineOutputFlag to hasMachineOutputFlag since it has a
single internal caller in root.go.
This commit is contained in:
Kristoffer Dalby
2026-02-18 15:28:09 +00:00
parent af777f44f4
commit 13ebea192c
3 changed files with 12 additions and 21 deletions

View File

@@ -368,27 +368,26 @@ func nodesToPtables(
tagsBuilder.WriteString("\n" + tag) tagsBuilder.WriteString("\n" + tag)
} }
tags := tagsBuilder.String() tags := strings.TrimLeft(tagsBuilder.String(), "\n")
var user string var user string
if node.GetUser() != nil { if node.GetUser() != nil {
user = node.GetUser().GetName() user = node.GetUser().GetName()
} }
var ( var ipBuilder strings.Builder
ipAddresses string
ipAddressesSb485 strings.Builder
)
for _, addr := range node.GetIpAddresses() { for _, addr := range node.GetIpAddresses() {
ip, err := netip.ParseAddr(addr) ip, err := netip.ParseAddr(addr)
if err == nil { if err == nil {
ipAddressesSb485.WriteString(ip.String() + "\n") if ipBuilder.Len() > 0 {
ipBuilder.WriteString("\n")
}
ipBuilder.WriteString(ip.String())
} }
} }
ipAddresses += ipAddressesSb485.String() ipAddresses := ipBuilder.String()
ipAddresses = strings.TrimRight(ipAddresses, "\n")
nodeData := []string{ nodeData := []string{
strconv.FormatUint(node.GetId(), util.Base10), strconv.FormatUint(node.GetId(), util.Base10),
@@ -462,11 +461,7 @@ var tagCmd = &cobra.Command{
return fmt.Errorf("setting tags: %w", err) return fmt.Errorf("setting tags: %w", err)
} }
if resp != nil { return printOutput(cmd, resp.GetNode(), "Node updated")
return printOutput(cmd, resp.GetNode(), "Node updated")
}
return nil
}), }),
} }
@@ -488,10 +483,6 @@ var approveRoutesCmd = &cobra.Command{
return fmt.Errorf("setting approved routes: %w", err) return fmt.Errorf("setting approved routes: %w", err)
} }
if resp != nil { return printOutput(cmd, resp.GetNode(), "Node updated")
return printOutput(cmd, resp.GetNode(), "Node updated")
}
return nil
}), }),
} }

View File

@@ -61,7 +61,7 @@ func initConfig() {
} }
} }
machineOutput := HasMachineOutputFlag() machineOutput := hasMachineOutputFlag()
// If the user has requested a "node" readable format, // If the user has requested a "node" readable format,
// then disable login so the output remains valid. // then disable login so the output remains valid.

View File

@@ -295,7 +295,7 @@ func printError(err error, outputFormat string) {
fmt.Fprintf(os.Stderr, "%s\n", formatted) fmt.Fprintf(os.Stderr, "%s\n", formatted)
} }
func HasMachineOutputFlag() bool { func hasMachineOutputFlag() bool {
for _, arg := range os.Args { for _, arg := range os.Args {
if arg == outputFormatJSON || arg == outputFormatJSONLine || arg == outputFormatYAML { if arg == outputFormatJSON || arg == outputFormatJSONLine || arg == outputFormatYAML {
return true return true