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 package main
import ( import (
"encoding/json"
"fmt" "fmt"
"os"
"strconv" "strconv"
"github.com/rosti-cz/server_lobby/server" "github.com/rosti-cz/server_lobby/server"
@ -41,3 +43,13 @@ func printDiscoveries(discoveries []server.Discovery) {
fmt.Println() 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") host := flag.String("host", "", "Hostname or IP address of lobby daemon")
port := flag.Uint("port", 0, "Port 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") 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() flag.Parse()
@ -103,13 +104,22 @@ func main() {
} }
} }
if *jsonOutput {
printJSON(discoveries)
} else {
printDiscoveries(discoveries) printDiscoveries(discoveries)
}
case "discovery": case "discovery":
discovery, err := client.GetDiscovery() discovery, err := client.GetDiscovery()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
if *jsonOutput {
printJSON(discovery)
} else {
printDiscovery(discovery) printDiscovery(discovery)
}
case "labels": case "labels":
if len(flag.Args()) < 3 { if len(flag.Args()) < 3 {
fmt.Println("ERROR: not enough arguments for labels command") fmt.Println("ERROR: not enough arguments for labels command")