2021-09-04 23:27:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-05 19:35:10 +00:00
|
|
|
"encoding/json"
|
2021-09-04 23:27:39 +00:00
|
|
|
"fmt"
|
2021-09-05 19:35:10 +00:00
|
|
|
"os"
|
2021-09-04 23:27:39 +00:00
|
|
|
"strconv"
|
2021-09-05 19:43:19 +00:00
|
|
|
"strings"
|
2021-09-04 23:27:39 +00:00
|
|
|
|
2021-09-05 19:43:19 +00:00
|
|
|
"github.com/fatih/color"
|
2021-09-04 23:27:39 +00:00
|
|
|
"github.com/rosti-cz/server_lobby/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
func printDiscovery(discovery server.Discovery) {
|
2021-09-05 19:43:19 +00:00
|
|
|
color.Yellow("Hostname:\n %s\n", discovery.Hostname)
|
2021-09-04 23:27:39 +00:00
|
|
|
|
|
|
|
if len(discovery.Labels) > 0 {
|
|
|
|
fmt.Printf("Labels:\n")
|
|
|
|
for _, label := range discovery.Labels {
|
|
|
|
fmt.Printf(" %s\n", label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 19:43:19 +00:00
|
|
|
func colorLabel(label server.Label) string {
|
|
|
|
parts := strings.Split(label.String(), ":")
|
|
|
|
if len(parts) == 1 {
|
|
|
|
return color.GreenString(parts[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return color.GreenString(parts[0]) + ":" + color.MagentaString((strings.Join(parts[1:], ":")))
|
|
|
|
}
|
|
|
|
|
2021-09-04 23:27:39 +00:00
|
|
|
func printDiscoveries(discoveries []server.Discovery) {
|
|
|
|
maxHostnameWidth := 0
|
|
|
|
for _, discovery := range discoveries {
|
|
|
|
if len(discovery.Hostname) > maxHostnameWidth {
|
|
|
|
maxHostnameWidth = len(discovery.Hostname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, discovery := range discoveries {
|
|
|
|
if len(discovery.Labels) == 0 {
|
2021-09-05 19:43:19 +00:00
|
|
|
// fmt.Println(discovery.Hostname)
|
|
|
|
color.Yellow(discovery.Hostname)
|
2021-09-04 23:27:39 +00:00
|
|
|
} else {
|
|
|
|
hostname := fmt.Sprintf("%"+strconv.Itoa(maxHostnameWidth)+"s", discovery.Hostname)
|
2021-09-05 19:43:19 +00:00
|
|
|
|
|
|
|
fmt.Printf("%s %s\n", color.YellowString(hostname), colorLabel(discovery.Labels[0]))
|
|
|
|
|
2021-09-04 23:27:39 +00:00
|
|
|
if len(discovery.Labels) > 1 {
|
|
|
|
for _, label := range discovery.Labels[1:] {
|
2021-09-05 19:43:19 +00:00
|
|
|
fmt.Printf("%"+strconv.Itoa(maxHostnameWidth+4)+"s%s\n", " ", colorLabel(label))
|
2021-09-04 23:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 19:35:10 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|