diff --git a/cmd/headscale/cli/namespaces.go b/cmd/headscale/cli/namespaces.go index 9f4a66e..1fb2de6 100644 --- a/cmd/headscale/cli/namespaces.go +++ b/cmd/headscale/cli/namespaces.go @@ -30,7 +30,7 @@ var CreateNamespaceCmd = &cobra.Command{ } namespace, err := h.CreateNamespace(args[0]) if strings.HasPrefix(o, "json") { - jsonOutput(namespace, err, o) + JsonOutput(namespace, err, o) return } if err != nil { @@ -52,7 +52,7 @@ var ListNamespacesCmd = &cobra.Command{ } namespaces, err := h.ListNamespaces() if strings.HasPrefix(o, "json") { - jsonOutput(namespaces, err, o) + JsonOutput(namespaces, err, o) return } if err != nil { diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 36bc29d..de76a40 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -30,7 +30,7 @@ var RegisterCmd = &cobra.Command{ } m, err := h.RegisterMachine(args[0], n) if strings.HasPrefix(o, "json") { - jsonOutput(m, err, o) + JsonOutput(m, err, o) return } if err != nil { @@ -57,7 +57,7 @@ var ListNodesCmd = &cobra.Command{ } machines, err := h.ListMachinesInNamespace(n) if strings.HasPrefix(o, "json") { - jsonOutput(machines, err, o) + JsonOutput(machines, err, o) return } diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index ae6c468..3db09f9 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -31,7 +31,7 @@ var ListPreAuthKeys = &cobra.Command{ } keys, err := h.GetPreAuthKeys(n) if strings.HasPrefix(o, "json") { - jsonOutput(keys, err, o) + JsonOutput(keys, err, o) return } @@ -85,7 +85,7 @@ var CreatePreAuthKeyCmd = &cobra.Command{ k, err := h.CreatePreAuthKey(n, reusable, expiration) if strings.HasPrefix(o, "json") { - jsonOutput(k, err, o) + JsonOutput(k, err, o) return } if err != nil { diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index bc043b6..007bc6c 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -36,10 +36,10 @@ var ListRoutesCmd = &cobra.Command{ routes, err := h.GetNodeRoutes(n, args[0]) if strings.HasPrefix(o, "json") { - jsonOutput(routes, err, o) + JsonOutput(routes, err, o) return - } + if err != nil { fmt.Println(err) return @@ -71,7 +71,7 @@ var EnableRouteCmd = &cobra.Command{ } route, err := h.EnableNodeRoute(n, args[0], args[1]) if strings.HasPrefix(o, "json") { - jsonOutput(route, err, o) + JsonOutput(route, err, o) return } diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index 0738e18..a9897fb 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -79,7 +79,7 @@ func loadDerpMap(path string) (*tailcfg.DERPMap, error) { return &derpMap, err } -func jsonOutput(result interface{}, errResult error, outputFormat string) { +func JsonOutput(result interface{}, errResult error, outputFormat string) { var j []byte var err error switch outputFormat { diff --git a/cmd/headscale/headscale.go b/cmd/headscale/headscale.go index 974227e..ccc75e8 100644 --- a/cmd/headscale/headscale.go +++ b/cmd/headscale/headscale.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "errors" "fmt" "log" @@ -20,27 +19,12 @@ var versionCmd = &cobra.Command{ Short: "Print the version.", Long: "The version of headscale.", Run: func(cmd *cobra.Command, args []string) { - o, _ := cmd.Flags().GetString("output") - switch o { - case "": - fmt.Println(version) - case "json": - j, err := json.MarshalIndent(map[string]string{"version": version}, "", "\t") - if err != nil { - log.Fatalln(err) - } - fmt.Println(string(j)) - - case "json-line": - j, err := json.Marshal(map[string]string{"version": version}) - if err != nil { - log.Fatalln(err) - } - fmt.Println(string(j)) - default: - fmt.Printf("Unknown format %s\n", o) + if strings.HasPrefix(o, "json") { + cli.JsonOutput(map[string]string{"version": version}, nil, o) + return } + fmt.Println(version) }, }