mirror of
https://github.com/juanfont/headscale.git
synced 2026-02-07 20:04:00 +01:00
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.
103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Error is used to compare errors as per https://dave.cheney.net/2016/04/07/constant-errors
|
|
type Error string
|
|
|
|
func (e Error) Error() string { return string(e) }
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(debugCmd)
|
|
|
|
createNodeCmd.Flags().StringP("name", "", "", "Name")
|
|
|
|
err := createNodeCmd.MarkFlagRequired("name")
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("")
|
|
}
|
|
|
|
createNodeCmd.Flags().StringP("user", "u", "", "User")
|
|
|
|
createNodeCmd.Flags().StringP("namespace", "n", "", "User")
|
|
createNodeNamespaceFlag := createNodeCmd.Flags().Lookup("namespace")
|
|
createNodeNamespaceFlag.Deprecated = deprecateNamespaceMessage
|
|
createNodeNamespaceFlag.Hidden = true
|
|
|
|
err = createNodeCmd.MarkFlagRequired("user")
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("")
|
|
}
|
|
|
|
createNodeCmd.Flags().StringP("key", "k", "", "Key")
|
|
|
|
err = createNodeCmd.MarkFlagRequired("key")
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("")
|
|
}
|
|
|
|
createNodeCmd.Flags().
|
|
StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to advertise")
|
|
|
|
debugCmd.AddCommand(createNodeCmd)
|
|
}
|
|
|
|
var debugCmd = &cobra.Command{
|
|
Use: "debug",
|
|
Short: "debug and testing commands",
|
|
Long: "debug contains extra commands used for debugging and testing headscale",
|
|
}
|
|
|
|
var createNodeCmd = &cobra.Command{
|
|
Use: "create-node",
|
|
Short: "Create a node that can be registered with `nodes register <>` command",
|
|
RunE: grpcRunE(func(ctx context.Context, client v1.HeadscaleServiceClient, cmd *cobra.Command, args []string) error {
|
|
user, err := cmd.Flags().GetString("user")
|
|
if err != nil {
|
|
return fmt.Errorf("getting user flag: %w", err)
|
|
}
|
|
|
|
name, err := cmd.Flags().GetString("name")
|
|
if err != nil {
|
|
return fmt.Errorf("getting name flag: %w", err)
|
|
}
|
|
|
|
registrationID, err := cmd.Flags().GetString("key")
|
|
if err != nil {
|
|
return fmt.Errorf("getting key flag: %w", err)
|
|
}
|
|
|
|
_, err = types.RegistrationIDFromString(registrationID)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing machine key: %w", err)
|
|
}
|
|
|
|
routes, err := cmd.Flags().GetStringSlice("route")
|
|
if err != nil {
|
|
return fmt.Errorf("getting routes flag: %w", err)
|
|
}
|
|
|
|
request := &v1.DebugCreateNodeRequest{
|
|
Key: registrationID,
|
|
Name: name,
|
|
User: user,
|
|
Routes: routes,
|
|
}
|
|
|
|
response, err := client.DebugCreateNode(ctx, request)
|
|
if err != nil {
|
|
return fmt.Errorf("creating node: %w", err)
|
|
}
|
|
|
|
return printOutput(cmd, response.GetNode(), "Node created")
|
|
}),
|
|
}
|