CTL: JSON output

This commit is contained in:
Adam Štrauch 2021-09-05 21:35:10 +02:00
parent c6cb674053
commit 753aaf8377
Signed by: cx
GPG Key ID: 018304FFA8988F8D
2 changed files with 24 additions and 2 deletions

View File

@ -1,7 +1,9 @@
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/rosti-cz/server_lobby/server"
@ -41,3 +43,13 @@ func printDiscoveries(discoveries []server.Discovery) {
fmt.Println()
}
}
func printJSON(data interface{}) {
body, err := json.Marshal(data)
if err != nil {
fmt.Println("error occurred while formating the output into JSON:", err.Error())
os.Exit(3)
}
fmt.Println(string(body))
}

View File

@ -30,6 +30,7 @@ func main() {
host := flag.String("host", "", "Hostname or IP address of lobby daemon")
port := flag.Uint("port", 0, "Port of lobby daemon")
token := flag.String("token", "", "Token needed to communicate lobby daemon, if empty auth is disabled")
jsonOutput := flag.Bool("json", false, "set output to JSON, error will be still in plain text")
flag.Parse()
@ -103,13 +104,22 @@ func main() {
}
}
printDiscoveries(discoveries)
if *jsonOutput {
printJSON(discoveries)
} else {
printDiscoveries(discoveries)
}
case "discovery":
discovery, err := client.GetDiscovery()
if err != nil {
fmt.Println(err)
}
printDiscovery(discovery)
if *jsonOutput {
printJSON(discovery)
} else {
printDiscovery(discovery)
}
case "labels":
if len(flag.Args()) < 3 {
fmt.Println("ERROR: not enough arguments for labels command")