Use JsonOutput in version too

This commit is contained in:
Juan Font Alonso 2021-05-08 17:06:36 +02:00
parent 9eaac71762
commit 8ad366f977
6 changed files with 14 additions and 30 deletions

View File

@ -30,7 +30,7 @@ var CreateNamespaceCmd = &cobra.Command{
} }
namespace, err := h.CreateNamespace(args[0]) namespace, err := h.CreateNamespace(args[0])
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(namespace, err, o) JsonOutput(namespace, err, o)
return return
} }
if err != nil { if err != nil {
@ -52,7 +52,7 @@ var ListNamespacesCmd = &cobra.Command{
} }
namespaces, err := h.ListNamespaces() namespaces, err := h.ListNamespaces()
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(namespaces, err, o) JsonOutput(namespaces, err, o)
return return
} }
if err != nil { if err != nil {

View File

@ -30,7 +30,7 @@ var RegisterCmd = &cobra.Command{
} }
m, err := h.RegisterMachine(args[0], n) m, err := h.RegisterMachine(args[0], n)
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(m, err, o) JsonOutput(m, err, o)
return return
} }
if err != nil { if err != nil {
@ -57,7 +57,7 @@ var ListNodesCmd = &cobra.Command{
} }
machines, err := h.ListMachinesInNamespace(n) machines, err := h.ListMachinesInNamespace(n)
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(machines, err, o) JsonOutput(machines, err, o)
return return
} }

View File

@ -31,7 +31,7 @@ var ListPreAuthKeys = &cobra.Command{
} }
keys, err := h.GetPreAuthKeys(n) keys, err := h.GetPreAuthKeys(n)
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(keys, err, o) JsonOutput(keys, err, o)
return return
} }
@ -85,7 +85,7 @@ var CreatePreAuthKeyCmd = &cobra.Command{
k, err := h.CreatePreAuthKey(n, reusable, expiration) k, err := h.CreatePreAuthKey(n, reusable, expiration)
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(k, err, o) JsonOutput(k, err, o)
return return
} }
if err != nil { if err != nil {

View File

@ -36,10 +36,10 @@ var ListRoutesCmd = &cobra.Command{
routes, err := h.GetNodeRoutes(n, args[0]) routes, err := h.GetNodeRoutes(n, args[0])
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(routes, err, o) JsonOutput(routes, err, o)
return return
} }
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -71,7 +71,7 @@ var EnableRouteCmd = &cobra.Command{
} }
route, err := h.EnableNodeRoute(n, args[0], args[1]) route, err := h.EnableNodeRoute(n, args[0], args[1])
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
jsonOutput(route, err, o) JsonOutput(route, err, o)
return return
} }

View File

@ -79,7 +79,7 @@ func loadDerpMap(path string) (*tailcfg.DERPMap, error) {
return &derpMap, err return &derpMap, err
} }
func jsonOutput(result interface{}, errResult error, outputFormat string) { func JsonOutput(result interface{}, errResult error, outputFormat string) {
var j []byte var j []byte
var err error var err error
switch outputFormat { switch outputFormat {

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -20,27 +19,12 @@ var versionCmd = &cobra.Command{
Short: "Print the version.", Short: "Print the version.",
Long: "The version of headscale.", Long: "The version of headscale.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
o, _ := cmd.Flags().GetString("output") o, _ := cmd.Flags().GetString("output")
switch o { if strings.HasPrefix(o, "json") {
case "": cli.JsonOutput(map[string]string{"version": version}, nil, o)
fmt.Println(version) return
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)
} }
fmt.Println(version)
}, },
} }