mirror of
https://github.com/juanfont/headscale.git
synced 2026-03-18 23:44:07 +01:00
163 lines
4.5 KiB
Go
163 lines
4.5 KiB
Go
package cli
|
|
|
|
// This file demonstrates how the new flag infrastructure simplifies command creation
|
|
// It shows a before/after comparison for the registerNodeCmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
"github.com/spf13/cobra"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// BEFORE: Current registerNodeCmd with lots of duplication (from nodes.go:114-158)
|
|
var originalRegisterNodeCmd = &cobra.Command{
|
|
Use: "register",
|
|
Short: "Registers a node to your network",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
output, _ := cmd.Flags().GetString("output") // Manual flag parsing
|
|
user, err := cmd.Flags().GetString("user") // Manual flag parsing with error handling
|
|
if err != nil {
|
|
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)
|
|
}
|
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig() // gRPC client setup
|
|
defer cancel()
|
|
defer conn.Close()
|
|
|
|
registrationID, err := cmd.Flags().GetString("key") // More manual flag parsing
|
|
if err != nil {
|
|
ErrorOutput(
|
|
err,
|
|
fmt.Sprintf("Error getting node key from flag: %s", err),
|
|
output,
|
|
)
|
|
}
|
|
|
|
request := &v1.RegisterNodeRequest{
|
|
Key: registrationID,
|
|
User: user,
|
|
}
|
|
|
|
response, err := client.RegisterNode(ctx, request) // gRPC call with manual error handling
|
|
if err != nil {
|
|
ErrorOutput(
|
|
err,
|
|
fmt.Sprintf(
|
|
"Cannot register node: %s\n",
|
|
status.Convert(err).Message(),
|
|
),
|
|
output,
|
|
)
|
|
}
|
|
|
|
SuccessOutput(
|
|
response.GetNode(),
|
|
fmt.Sprintf("Node %s registered", response.GetNode().GetGivenName()), output)
|
|
},
|
|
}
|
|
|
|
// AFTER: Refactored registerNodeCmd using new flag infrastructure
|
|
var refactoredRegisterNodeCmd = &cobra.Command{
|
|
Use: "register",
|
|
Short: "Registers a node to your network",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
// Clean flag parsing with standardized error handling
|
|
output := GetOutputFormat(cmd)
|
|
user, err := GetUserWithDeprecatedNamespace(cmd) // Handles both --user and deprecated --namespace
|
|
if err != nil {
|
|
ErrorOutput(err, "Error getting user", output)
|
|
return
|
|
}
|
|
|
|
key, err := GetKey(cmd)
|
|
if err != nil {
|
|
ErrorOutput(err, "Error getting key", output)
|
|
return
|
|
}
|
|
|
|
// gRPC client setup (will be further simplified in Checkpoint 2)
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
|
defer cancel()
|
|
defer conn.Close()
|
|
|
|
request := &v1.RegisterNodeRequest{
|
|
Key: key,
|
|
User: user,
|
|
}
|
|
|
|
response, err := client.RegisterNode(ctx, request)
|
|
if err != nil {
|
|
ErrorOutput(
|
|
err,
|
|
fmt.Sprintf("Cannot register node: %s", status.Convert(err).Message()),
|
|
output,
|
|
)
|
|
return
|
|
}
|
|
|
|
SuccessOutput(
|
|
response.GetNode(),
|
|
fmt.Sprintf("Node %s registered", response.GetNode().GetGivenName()),
|
|
output)
|
|
},
|
|
}
|
|
|
|
// BEFORE: Current flag setup in init() function (from nodes.go:36-52)
|
|
func originalFlagSetup() {
|
|
registerNodeCmd.Flags().StringP("user", "u", "", "User")
|
|
|
|
registerNodeCmd.Flags().StringP("namespace", "n", "", "User")
|
|
registerNodeNamespaceFlag := registerNodeCmd.Flags().Lookup("namespace")
|
|
registerNodeNamespaceFlag.Deprecated = deprecateNamespaceMessage
|
|
registerNodeNamespaceFlag.Hidden = true
|
|
|
|
err := registerNodeCmd.MarkFlagRequired("user")
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
registerNodeCmd.Flags().StringP("key", "k", "", "Key")
|
|
err = registerNodeCmd.MarkFlagRequired("key")
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
}
|
|
|
|
// AFTER: Simplified flag setup using new infrastructure
|
|
func refactoredFlagSetup() {
|
|
AddRequiredUserFlag(refactoredRegisterNodeCmd)
|
|
AddDeprecatedNamespaceFlag(refactoredRegisterNodeCmd)
|
|
AddRequiredKeyFlag(refactoredRegisterNodeCmd)
|
|
}
|
|
|
|
/*
|
|
IMPROVEMENT SUMMARY:
|
|
|
|
1. FLAG PARSING REDUCTION:
|
|
Before: 6 lines of manual flag parsing + error handling
|
|
After: 3 lines with standardized helpers
|
|
|
|
2. ERROR HANDLING CONSISTENCY:
|
|
Before: Inconsistent error message formatting
|
|
After: Standardized error handling with consistent format
|
|
|
|
3. DEPRECATED FLAG SUPPORT:
|
|
Before: 4 lines of deprecation setup
|
|
After: 1 line with GetUserWithDeprecatedNamespace()
|
|
|
|
4. FLAG REGISTRATION:
|
|
Before: 12 lines in init() with manual error handling
|
|
After: 3 lines with standardized helpers
|
|
|
|
5. CODE READABILITY:
|
|
Before: Business logic mixed with flag parsing boilerplate
|
|
After: Clear separation, focus on business logic
|
|
|
|
6. MAINTAINABILITY:
|
|
Before: Changes to flag patterns require updating every command
|
|
After: Changes can be made in one place (flags.go)
|
|
|
|
TOTAL REDUCTION: ~40% fewer lines, much cleaner code
|
|
*/ |