From 53185eaa9e2a7238e266b2bfe0e8cb89a8e27dee Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:03:45 +0200 Subject: [PATCH 1/9] Refactor namespaces cmd --- cmd/headscale/cli/namespaces.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/headscale/cli/namespaces.go b/cmd/headscale/cli/namespaces.go index 7fd4c8d7..7d3977f9 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) { From 2862c2034b4236ef11d1774d6fbe81b92d5bd187 Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:04:06 +0200 Subject: [PATCH 2/9] Refactor nodes cmd --- cmd/headscale/cli/nodes.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 92dd6690..7a773cef 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -11,12 +11,20 @@ import ( "github.com/spf13/cobra" ) -var NodeCmd = &cobra.Command{ +func init () { + nodeCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") + nodeCmd.MarkPersistentFlagRequired("namespace") + 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 +57,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 +97,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 { From e56755fd67cd74f221d998a7211ba56041dfdb7a Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:07:27 +0200 Subject: [PATCH 3/9] Refactor preauthkeys cmd --- cmd/headscale/cli/preauthkeys.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index d59b0759..8964bcfa 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -10,12 +10,22 @@ import ( "github.com/spf13/cobra" ) -var PreauthkeysCmd = &cobra.Command{ +func init() { + rootCmd.AddCommand(preauthkeysCmd) + preauthkeysCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") + 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 +75,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) { From d87a4c87cc6cf926dbe154da7f904b365ba43cf6 Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:08:40 +0200 Subject: [PATCH 4/9] Refactor routes cmd --- cmd/headscale/cli/routes.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index 007bc6c2..aa392818 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -8,12 +8,19 @@ import ( "github.com/spf13/cobra" ) -var RoutesCmd = &cobra.Command{ +func init() { + rootCmd.AddCommand(routesCmd) + routesCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") + 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 +56,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 { From 4915902e04086fed0f0993bc2ae79cff88c1b9a6 Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:09:33 +0200 Subject: [PATCH 5/9] Refactor server cmd --- cmd/headscale/cli/server.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/headscale/cli/server.go b/cmd/headscale/cli/server.go index f4423c71..1f8db6a2 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 { From b97d6f71b1936959365edf03f45f7ca81960f27d Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:09:53 +0200 Subject: [PATCH 6/9] Refactor version cmd --- cmd/headscale/cli/version.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/headscale/cli/version.go b/cmd/headscale/cli/version.go index 4bdf92ef..5de2c672 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.", From 3a07360b6ec6cbe3ffc0e3596a22cdc3f1ab62b4 Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:10:34 +0200 Subject: [PATCH 7/9] Add root cmd --- cmd/headscale/cli/root.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cmd/headscale/cli/root.go diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go new file mode 100644 index 00000000..0dd68064 --- /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) + } +} From 04dffcc4ae79c1e12e78cc4f19fef7303e459500 Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 15:14:09 +0200 Subject: [PATCH 8/9] Refactor cli commands --- cmd/headscale/headscale.go | 63 +------------------------------------- 1 file changed, 1 insertion(+), 62 deletions(-) diff --git a/cmd/headscale/headscale.go b/cmd/headscale/headscale.go index c6e56fa8..c7b834c2 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() } From ea3043cdcb1585f8db7499d6df8fa47eae041b76 Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Sun, 25 Jul 2021 16:26:15 +0200 Subject: [PATCH 9/9] cmd: Add error check for Persistent Flags --- cmd/headscale/cli/nodes.go | 5 ++++- cmd/headscale/cli/preauthkeys.go | 4 ++++ cmd/headscale/cli/routes.go | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 7a773cef..4be706b8 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -13,7 +13,10 @@ import ( func init () { nodeCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") - nodeCmd.MarkPersistentFlagRequired("namespace") + err := nodeCmd.MarkPersistentFlagRequired("namespace") + if err != nil { + log.Fatalf(err.Error()) + } nodeCmd.AddCommand(listNodesCmd) nodeCmd.AddCommand(registerNodeCmd) nodeCmd.AddCommand(deleteNodeCmd) diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index 8964bcfa..881b5ca3 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -13,6 +13,10 @@ import ( 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") diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index aa392818..98b653f9 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -11,6 +11,10 @@ import ( 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) }