allow full json output for best server using flag -o json
This commit is contained in:
parent
20e1092604
commit
230e89e2b7
@ -1,5 +1,5 @@
|
|||||||
# mullvad-best-server
|
# mullvad-best-server
|
||||||
Determines the mullvat.net server with the lowest latency.
|
Determines the mullvat.net wireguard server with the lowest latency.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
12
main.go
12
main.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-ping/ping"
|
"github.com/go-ping/ping"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
@ -17,11 +18,22 @@ var pings = make(map[string]time.Duration)
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||||
|
var outputFlag = flag.String("o", "short", "Output format. 'json' outputs server json")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
servers := getServers()
|
servers := getServers()
|
||||||
bestIndex := selectBestServerIndex(servers)
|
bestIndex := selectBestServerIndex(servers)
|
||||||
log.Debug().Interface("server", servers[bestIndex]).Msg("Best latency server found.")
|
log.Debug().Interface("server", servers[bestIndex]).Msg("Best latency server found.")
|
||||||
hostname := strings.Split(servers[bestIndex].Hostname, "-")[0]
|
hostname := strings.Split(servers[bestIndex].Hostname, "-")[0]
|
||||||
|
if *outputFlag != "json" {
|
||||||
fmt.Println(hostname)
|
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 {
|
func selectBestServerIndex(servers []server) int {
|
||||||
|
Reference in New Issue
Block a user