diff --git a/cmd/headscale/cli/namespaces.go b/cmd/headscale/cli/namespaces.go index 7fd4c8d..7d3977f 100644 --- a/cmd/headscale/cli/namespaces.go +++ b/cmd/headscale/cli/namespaces.go @@ -8,12 +8,19 @@ import ( "github.com/spf13/cobra" ) -var NamespaceCmd = &cobra.Command{ +func init() { + rootCmd.AddCommand(namespaceCmd) + namespaceCmd.AddCommand(createNamespaceCmd) + namespaceCmd.AddCommand(listNamespacesCmd) + namespaceCmd.AddCommand(destroyNamespaceCmd) +} + +var namespaceCmd = &cobra.Command{ Use: "namespaces", Short: "Manage the namespaces of Headscale", } -var CreateNamespaceCmd = &cobra.Command{ +var createNamespaceCmd = &cobra.Command{ Use: "create NAME", Short: "Creates a new namespace", Args: func(cmd *cobra.Command, args []string) error { @@ -41,7 +48,7 @@ var CreateNamespaceCmd = &cobra.Command{ }, } -var DestroyNamespaceCmd = &cobra.Command{ +var destroyNamespaceCmd = &cobra.Command{ Use: "destroy NAME", Short: "Destroys a namespace", Args: func(cmd *cobra.Command, args []string) error { @@ -69,7 +76,7 @@ var DestroyNamespaceCmd = &cobra.Command{ }, } -var ListNamespacesCmd = &cobra.Command{ +var listNamespacesCmd = &cobra.Command{ Use: "list", Short: "List all the namespaces", Run: func(cmd *cobra.Command, args []string) { diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 92dd669..4be706b 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -11,12 +11,23 @@ import ( "github.com/spf13/cobra" ) -var NodeCmd = &cobra.Command{ +func init () { + nodeCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") + err := nodeCmd.MarkPersistentFlagRequired("namespace") + if err != nil { + log.Fatalf(err.Error()) + } + nodeCmd.AddCommand(listNodesCmd) + nodeCmd.AddCommand(registerNodeCmd) + nodeCmd.AddCommand(deleteNodeCmd) +} + +var nodeCmd = &cobra.Command{ Use: "nodes", Short: "Manage the nodes of Headscale", } -var RegisterCmd = &cobra.Command{ +var registerNodeCmd = &cobra.Command{ Use: "register machineID", Short: "Registers a machine to your network", Args: func(cmd *cobra.Command, args []string) error { @@ -49,7 +60,7 @@ var RegisterCmd = &cobra.Command{ }, } -var ListNodesCmd = &cobra.Command{ +var listNodesCmd = &cobra.Command{ Use: "list", Short: "List the nodes in a given namespace", Run: func(cmd *cobra.Command, args []string) { @@ -89,7 +100,7 @@ var ListNodesCmd = &cobra.Command{ }, } -var DeleteCmd = &cobra.Command{ +var deleteNodeCmd = &cobra.Command{ Use: "delete ID", Short: "Delete a node", Args: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index d59b075..881b5ca 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -10,12 +10,26 @@ import ( "github.com/spf13/cobra" ) -var PreauthkeysCmd = &cobra.Command{ +func init() { + rootCmd.AddCommand(preauthkeysCmd) + preauthkeysCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") + err := preauthkeysCmd.MarkPersistentFlagRequired("namespace") + if err != nil { + log.Fatalf(err.Error()) + } + preauthkeysCmd.AddCommand(listPreAuthKeys) + preauthkeysCmd.AddCommand(createPreAuthKeyCmd) + createPreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable") + createPreAuthKeyCmd.PersistentFlags().Bool("ephemeral", false, "Preauthkey for ephemeral nodes") + createPreAuthKeyCmd.Flags().StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)") +} + +var preauthkeysCmd = &cobra.Command{ Use: "preauthkeys", Short: "Handle the preauthkeys in Headscale", } -var ListPreAuthKeys = &cobra.Command{ +var listPreAuthKeys = &cobra.Command{ Use: "list", Short: "List the preauthkeys for this namespace", Run: func(cmd *cobra.Command, args []string) { @@ -65,7 +79,7 @@ var ListPreAuthKeys = &cobra.Command{ }, } -var CreatePreAuthKeyCmd = &cobra.Command{ +var createPreAuthKeyCmd = &cobra.Command{ Use: "create", Short: "Creates a new preauthkey in the specified namespace", Run: func(cmd *cobra.Command, args []string) { diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go new file mode 100644 index 0000000..0dd6806 --- /dev/null +++ b/cmd/headscale/cli/root.go @@ -0,0 +1,28 @@ +package cli + +import ( + "fmt" + "github.com/spf13/cobra" + "os" +) + +func init() { + rootCmd.PersistentFlags().StringP("output", "o", "", "Output format. Empty for human-readable, 'json' or 'json-line'") +} + +var rootCmd = &cobra.Command{ + Use: "headscale", + Short: "headscale - a Tailscale control server", + Long: ` +headscale is an open source implementation of the Tailscale control server + +Juan Font Alonso - 2021 +https://gitlab.com/juanfont/headscale`, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index 007bc6c..98b653f 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -8,12 +8,23 @@ import ( "github.com/spf13/cobra" ) -var RoutesCmd = &cobra.Command{ +func init() { + rootCmd.AddCommand(routesCmd) + routesCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") + err := routesCmd.MarkPersistentFlagRequired("namespace") + if err != nil { + log.Fatalf(err.Error()) + } + routesCmd.AddCommand(listRoutesCmd) + routesCmd.AddCommand(enableRouteCmd) +} + +var routesCmd = &cobra.Command{ Use: "routes", Short: "Manage the routes of Headscale", } -var ListRoutesCmd = &cobra.Command{ +var listRoutesCmd = &cobra.Command{ Use: "list NODE", Short: "List the routes exposed by this node", Args: func(cmd *cobra.Command, args []string) error { @@ -49,7 +60,7 @@ var ListRoutesCmd = &cobra.Command{ }, } -var EnableRouteCmd = &cobra.Command{ +var enableRouteCmd = &cobra.Command{ Use: "enable node-name route", Short: "Allows exposing a route declared by this node to the rest of the nodes", Args: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/headscale/cli/server.go b/cmd/headscale/cli/server.go index f4423c7..1f8db6a 100644 --- a/cmd/headscale/cli/server.go +++ b/cmd/headscale/cli/server.go @@ -6,7 +6,11 @@ import ( "github.com/spf13/cobra" ) -var ServeCmd = &cobra.Command{ +func init() { + rootCmd.AddCommand(serveCmd) +} + +var serveCmd = &cobra.Command{ Use: "serve", Short: "Launches the headscale server", Args: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/headscale/cli/version.go b/cmd/headscale/cli/version.go index 4bdf92e..5de2c67 100644 --- a/cmd/headscale/cli/version.go +++ b/cmd/headscale/cli/version.go @@ -8,7 +8,11 @@ import ( var version = "dev" -var VersionCmd = &cobra.Command{ +func init() { + rootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version.", Long: "The version of headscale.", diff --git a/cmd/headscale/headscale.go b/cmd/headscale/headscale.go index c6e56fa..c7b834c 100644 --- a/cmd/headscale/headscale.go +++ b/cmd/headscale/headscale.go @@ -1,77 +1,16 @@ package main import ( - "fmt" "log" - "os" "github.com/juanfont/headscale/cmd/headscale/cli" - "github.com/spf13/cobra" ) -var headscaleCmd = &cobra.Command{ - Use: "headscale", - Short: "headscale - a Tailscale control server", - Long: ` -headscale is an open source implementation of the Tailscale control server - -Juan Font Alonso - 2021 -https://gitlab.com/juanfont/headscale`, -} - func main() { err := cli.LoadConfig("") if err != nil { log.Fatalf(err.Error()) } - headscaleCmd.AddCommand(cli.NamespaceCmd) - headscaleCmd.AddCommand(cli.NodeCmd) - headscaleCmd.AddCommand(cli.PreauthkeysCmd) - headscaleCmd.AddCommand(cli.RoutesCmd) - headscaleCmd.AddCommand(cli.ServeCmd) - headscaleCmd.AddCommand(cli.VersionCmd) - - cli.NodeCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") - err = cli.NodeCmd.MarkPersistentFlagRequired("namespace") - if err != nil { - log.Fatalf(err.Error()) - } - - cli.PreauthkeysCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") - err = cli.PreauthkeysCmd.MarkPersistentFlagRequired("namespace") - if err != nil { - log.Fatalf(err.Error()) - } - - cli.RoutesCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") - err = cli.RoutesCmd.MarkPersistentFlagRequired("namespace") - if err != nil { - log.Fatalf(err.Error()) - } - - cli.NamespaceCmd.AddCommand(cli.CreateNamespaceCmd) - cli.NamespaceCmd.AddCommand(cli.ListNamespacesCmd) - cli.NamespaceCmd.AddCommand(cli.DestroyNamespaceCmd) - - cli.NodeCmd.AddCommand(cli.ListNodesCmd) - cli.NodeCmd.AddCommand(cli.RegisterCmd) - cli.NodeCmd.AddCommand(cli.DeleteCmd) - - cli.RoutesCmd.AddCommand(cli.ListRoutesCmd) - cli.RoutesCmd.AddCommand(cli.EnableRouteCmd) - - cli.PreauthkeysCmd.AddCommand(cli.ListPreAuthKeys) - cli.PreauthkeysCmd.AddCommand(cli.CreatePreAuthKeyCmd) - - cli.CreatePreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable") - cli.CreatePreAuthKeyCmd.PersistentFlags().Bool("ephemeral", false, "Preauthkey for ephemeral nodes") - cli.CreatePreAuthKeyCmd.Flags().StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)") - - headscaleCmd.PersistentFlags().StringP("output", "o", "", "Output format. Empty for human-readable, 'json' or 'json-line'") - - if err := headscaleCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(-1) - } + cli.Execute() }