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])
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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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 "":
if strings.HasPrefix(o, "json") {
cli.JsonOutput(map[string]string{"version": version}, nil, o)
return
}
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)
}
},
}