mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-30 02:43:05 +00:00
Adding support for JSON-formatted output 1/n
This commit is contained in:
parent
4b3b48441f
commit
3b34f715ce
4 changed files with 89 additions and 11 deletions
|
@ -3,6 +3,7 @@ package cli
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -22,16 +23,21 @@ var CreateNamespaceCmd = &cobra.Command{
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
o, _ := cmd.Flags().GetString("output")
|
||||||
h, err := getHeadscaleApp()
|
h, err := getHeadscaleApp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
}
|
}
|
||||||
_, err = h.CreateNamespace(args[0])
|
namespace, err := h.CreateNamespace(args[0])
|
||||||
if err != nil {
|
if strings.HasPrefix(o, "json") {
|
||||||
fmt.Println(err)
|
jsonOutput(namespace, err, o)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("Ook.\n")
|
if err != nil {
|
||||||
|
fmt.Printf("Error creating namespace: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("Namespace created\n")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,17 +45,22 @@ var ListNamespacesCmd = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "List all the namespaces",
|
Short: "List all the namespaces",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
o, _ := cmd.Flags().GetString("output")
|
||||||
h, err := getHeadscaleApp()
|
h, err := getHeadscaleApp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
}
|
}
|
||||||
ns, err := h.ListNamespaces()
|
namespaces, err := h.ListNamespaces()
|
||||||
|
if strings.HasPrefix(o, "json") {
|
||||||
|
jsonOutput(namespaces, err, o)
|
||||||
|
return
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("ID\tName\n")
|
fmt.Printf("ID\tName\n")
|
||||||
for _, n := range *ns {
|
for _, n := range *namespaces {
|
||||||
fmt.Printf("%d\t%s\n", n.ID, n.Name)
|
fmt.Printf("%d\t%s\n", n.ID, n.Name)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cli
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -21,17 +22,22 @@ var RegisterCmd = &cobra.Command{
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error getting namespace: %s", err)
|
log.Fatalf("Error getting namespace: %s", err)
|
||||||
}
|
}
|
||||||
|
o, _ := cmd.Flags().GetString("output")
|
||||||
|
|
||||||
h, err := getHeadscaleApp()
|
h, err := getHeadscaleApp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
}
|
}
|
||||||
err = h.RegisterMachine(args[0], n)
|
m, err := h.RegisterMachine(args[0], n)
|
||||||
if err != nil {
|
if strings.HasPrefix(o, "json") {
|
||||||
fmt.Printf("Error: %s", err)
|
jsonOutput(m, err, o)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("Ook.")
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot register machine: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("Machine registered\n")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,6 +15,10 @@ import (
|
||||||
"tailscale.com/tailcfg"
|
"tailscale.com/tailcfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ErrorOutput struct {
|
||||||
|
Error string
|
||||||
|
}
|
||||||
|
|
||||||
func absPath(path string) string {
|
func absPath(path string) string {
|
||||||
// If a relative path is provided, prefix it with the the directory where
|
// If a relative path is provided, prefix it with the the directory where
|
||||||
// the config file was found.
|
// the config file was found.
|
||||||
|
@ -72,3 +78,35 @@ func loadDerpMap(path string) (*tailcfg.DERPMap, error) {
|
||||||
err = yaml.Unmarshal(b, &derpMap)
|
err = yaml.Unmarshal(b, &derpMap)
|
||||||
return &derpMap, err
|
return &derpMap, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func jsonOutput(result interface{}, errResult error, outputFormat string) {
|
||||||
|
var j []byte
|
||||||
|
var err error
|
||||||
|
switch outputFormat {
|
||||||
|
case "json":
|
||||||
|
if errResult != nil {
|
||||||
|
j, err = json.MarshalIndent(ErrorOutput{errResult.Error()}, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
j, err = json.MarshalIndent(result, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "json-line":
|
||||||
|
if errResult != nil {
|
||||||
|
j, err = json.Marshal(ErrorOutput{errResult.Error()})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
j, err = json.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(string(j))
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -19,7 +20,27 @@ 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")
|
||||||
|
switch o {
|
||||||
|
case "":
|
||||||
fmt.Println(version)
|
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)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +144,8 @@ func main() {
|
||||||
cli.CreatePreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable")
|
cli.CreatePreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable")
|
||||||
cli.CreatePreAuthKeyCmd.Flags().StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)")
|
cli.CreatePreAuthKeyCmd.Flags().StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)")
|
||||||
|
|
||||||
|
headscaleCmd.PersistentFlags().StringP("output", "o", "", "Output format. Empty for human-readable, 'json' or 'json-line'")
|
||||||
|
|
||||||
if err := headscaleCmd.Execute(); err != nil {
|
if err := headscaleCmd.Execute(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
|
|
Loading…
Reference in a new issue