From abde7dddb7485e93d6adc9007f30aebdfbec73ad Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sat, 8 May 2021 13:58:51 +0200 Subject: [PATCH] More json output --- cmd/headscale/cli/nodes.go | 6 ++++++ cmd/headscale/cli/preauthkeys.go | 18 +++++++++++++++--- cmd/headscale/cli/routes.go | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index b449c6da..36bc29d0 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -49,12 +49,18 @@ var ListNodesCmd = &cobra.Command{ if err != nil { log.Fatalf("Error getting namespace: %s", err) } + o, _ := cmd.Flags().GetString("output") h, err := getHeadscaleApp() if err != nil { log.Fatalf("Error initializing: %s", err) } machines, err := h.ListMachinesInNamespace(n) + if strings.HasPrefix(o, "json") { + jsonOutput(machines, err, o) + return + } + if err != nil { log.Fatalf("Error getting nodes: %s", err) } diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index ce189eba..ae6c4682 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "log" + "strings" "time" "github.com/hako/durafmt" @@ -22,14 +23,20 @@ var ListPreAuthKeys = &cobra.Command{ if err != nil { log.Fatalf("Error getting namespace: %s", err) } + o, _ := cmd.Flags().GetString("output") h, err := getHeadscaleApp() if err != nil { log.Fatalf("Error initializing: %s", err) } keys, err := h.GetPreAuthKeys(n) + if strings.HasPrefix(o, "json") { + jsonOutput(keys, err, o) + return + } + if err != nil { - fmt.Println(err) + fmt.Printf("Error getting the list of keys: %s\n", err) return } for _, k := range *keys { @@ -57,6 +64,7 @@ var CreatePreAuthKeyCmd = &cobra.Command{ if err != nil { log.Fatalf("Error getting namespace: %s", err) } + o, _ := cmd.Flags().GetString("output") h, err := getHeadscaleApp() if err != nil { @@ -75,11 +83,15 @@ var CreatePreAuthKeyCmd = &cobra.Command{ expiration = &exp } - _, err = h.CreatePreAuthKey(n, reusable, expiration) + k, err := h.CreatePreAuthKey(n, reusable, expiration) + if strings.HasPrefix(o, "json") { + jsonOutput(k, err, o) + return + } if err != nil { fmt.Println(err) return } - fmt.Printf("Ook.\n") + fmt.Printf("Key: %s\n", k.Key) }, } diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index 067b6c24..bc043b64 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "log" + "strings" "github.com/spf13/cobra" ) @@ -26,16 +27,24 @@ var ListRoutesCmd = &cobra.Command{ if err != nil { log.Fatalf("Error getting namespace: %s", err) } + o, _ := cmd.Flags().GetString("output") h, err := getHeadscaleApp() if err != nil { log.Fatalf("Error initializing: %s", err) } routes, err := h.GetNodeRoutes(n, args[0]) + + if strings.HasPrefix(o, "json") { + jsonOutput(routes, err, o) + return + + } if err != nil { fmt.Println(err) return } + fmt.Println(routes) }, } @@ -54,15 +63,22 @@ var EnableRouteCmd = &cobra.Command{ if err != nil { log.Fatalf("Error getting namespace: %s", err) } + o, _ := cmd.Flags().GetString("output") h, err := getHeadscaleApp() if err != nil { log.Fatalf("Error initializing: %s", err) } - err = h.EnableNodeRoute(n, args[0], args[1]) + route, err := h.EnableNodeRoute(n, args[0], args[1]) + if strings.HasPrefix(o, "json") { + jsonOutput(route, err, o) + return + } + if err != nil { fmt.Println(err) return } + fmt.Printf("Enabled route %s\n", route) }, }