From 404fee83611db28f75fc0621573811ae80958fc9 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sat, 1 May 2021 20:00:25 +0200 Subject: [PATCH 1/3] Added list node functionality --- cmd/headscale/cli/nodes.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 1bf4accc..c6819475 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -35,6 +35,32 @@ var RegisterCmd = &cobra.Command{ }, } +var ListNodesCmd = &cobra.Command{ + Use: "list", + Short: "List the nodes in a given namespace", + Run: func(cmd *cobra.Command, args []string) { + n, err := cmd.Flags().GetString("namespace") + if err != nil { + log.Fatalf("Error getting namespace: %s", err) + } + + h, err := getHeadscaleApp() + if err != nil { + log.Fatalf("Error initializing: %s", err) + } + machines, err := h.ListMachinesInNamespace(n) + if err != nil { + log.Fatalf("Error getting nodes: %s", err) + } + + fmt.Printf("name\tlast seen") + for _, m := range *machines { + fmt.Printf("%s\t%s\n", m.Name, m.LastSeen.Format("2006-01-02 15:04:05")) + } + + }, +} + var NodeCmd = &cobra.Command{ Use: "node", Short: "Manage the nodes of Headscale", From c71c13366f6cac1845f66d2727fcf2b03095ded7 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sat, 1 May 2021 20:04:03 +0200 Subject: [PATCH 2/3] Minor fix --- cmd/headscale/cli/nodes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index c6819475..08b7e4d8 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -53,7 +53,7 @@ var ListNodesCmd = &cobra.Command{ log.Fatalf("Error getting nodes: %s", err) } - fmt.Printf("name\tlast seen") + fmt.Printf("name\t\tlast seen\n") for _, m := range *machines { fmt.Printf("%s\t%s\n", m.Name, m.LastSeen.Format("2006-01-02 15:04:05")) } From 45e900178776924f79288a4ea3dfd902bd9cc7e2 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sat, 1 May 2021 20:05:10 +0200 Subject: [PATCH 3/3] Register cmd under node --- README.md | 2 +- api.go | 2 +- cmd/headscale/headscale.go | 10 +++------- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0fe65805..17016974 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Suggestions/PRs welcomed! 8. In the server, register your machine to a namespace with the CLI ```shell - ./headscale -n myfirstnamespace register YOURMACHINEKEY + ./headscale -n myfirstnamespace node register YOURMACHINEKEY ``` ## Configuration reference diff --git a/api.go b/api.go index 721b65ab..f875259c 100644 --- a/api.go +++ b/api.go @@ -43,7 +43,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {

- headscale register %s + headscale -n NAMESPACE node register %s

diff --git a/cmd/headscale/headscale.go b/cmd/headscale/headscale.go index 3ea3a00a..51df310d 100644 --- a/cmd/headscale/headscale.go +++ b/cmd/headscale/headscale.go @@ -86,7 +86,6 @@ func main() { headscaleCmd.AddCommand(cli.NamespaceCmd) headscaleCmd.AddCommand(cli.NodeCmd) headscaleCmd.AddCommand(cli.PreauthkeysCmd) - headscaleCmd.AddCommand(cli.RegisterCmd) headscaleCmd.AddCommand(cli.RoutesCmd) headscaleCmd.AddCommand(cli.ServeCmd) headscaleCmd.AddCommand(versionCmd) @@ -103,12 +102,6 @@ func main() { log.Fatalf(err.Error()) } - cli.RegisterCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") - err = cli.RegisterCmd.MarkPersistentFlagRequired("namespace") - if err != nil { - log.Fatalf(err.Error()) - } - cli.RoutesCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace") err = cli.RoutesCmd.MarkPersistentFlagRequired("namespace") if err != nil { @@ -118,6 +111,9 @@ func main() { cli.NamespaceCmd.AddCommand(cli.CreateNamespaceCmd) cli.NamespaceCmd.AddCommand(cli.ListNamespacesCmd) + cli.NodeCmd.AddCommand(cli.ListNodesCmd) + cli.NodeCmd.AddCommand(cli.RegisterCmd) + cli.RoutesCmd.AddCommand(cli.ListRoutesCmd) cli.RoutesCmd.AddCommand(cli.EnableRouteCmd)