CTL: JSON output
This commit is contained in:
		
							parent
							
								
									c6cb674053
								
							
						
					
					
						commit
						753aaf8377
					
				
					 2 changed files with 24 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -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))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										10
									
								
								ctl/main.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								ctl/main.go
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -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() {
 | 
			
		|||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if *jsonOutput {
 | 
			
		||||
			printJSON(discoveries)
 | 
			
		||||
		} else {
 | 
			
		||||
			printDiscoveries(discoveries)
 | 
			
		||||
		}
 | 
			
		||||
	case "discovery":
 | 
			
		||||
		discovery, err := client.GetDiscovery()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if *jsonOutput {
 | 
			
		||||
			printJSON(discovery)
 | 
			
		||||
		} else {
 | 
			
		||||
			printDiscovery(discovery)
 | 
			
		||||
		}
 | 
			
		||||
	case "labels":
 | 
			
		||||
		if len(flag.Args()) < 3 {
 | 
			
		||||
			fmt.Println("ERROR: not enough arguments for labels command")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue