allow full json output for best server using flag -o json

This commit is contained in:
Bastian Doetsch 2022-03-06 17:36:21 +01:00
parent 20e1092604
commit 230e89e2b7
2 changed files with 14 additions and 2 deletions

View File

@ -1,5 +1,5 @@
# mullvad-best-server
Determines the mullvat.net server with the lowest latency.
Determines the mullvat.net wireguard server with the lowest latency.
## Installation

14
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/go-ping/ping"
"github.com/rs/zerolog"
@ -17,11 +18,22 @@ var pings = make(map[string]time.Duration)
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
var outputFlag = flag.String("o", "short", "Output format. 'json' outputs server json")
flag.Parse()
servers := getServers()
bestIndex := selectBestServerIndex(servers)
log.Debug().Interface("server", servers[bestIndex]).Msg("Best latency server found.")
hostname := strings.Split(servers[bestIndex].Hostname, "-")[0]
fmt.Println(hostname)
if *outputFlag != "json" {
fmt.Println(hostname)
} else {
serverJson, err := json.Marshal(servers[bestIndex])
if err != nil {
log.Fatal().Err(err)
}
fmt.Println(string(serverJson))
}
}
func selectBestServerIndex(servers []server) int {