diff --git a/README.md b/README.md index e474dd1..69e9187 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index 7d86e36..43594bb 100644 --- a/main.go +++ b/main.go @@ -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 {