2020-09-01 21:46:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-11-05 12:04:10 +00:00
|
|
|
"os"
|
2020-09-01 21:46:44 +00:00
|
|
|
)
|
|
|
|
|
2020-11-05 16:51:20 +00:00
|
|
|
func main() {
|
|
|
|
if err := run(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(1)
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:51:20 +00:00
|
|
|
func run() error {
|
|
|
|
cmd := ui()
|
|
|
|
conn, err := newConn(cmd)
|
2020-09-01 21:46:44 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:04:10 +00:00
|
|
|
return err
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
|
|
|
for {
|
2020-11-05 16:51:20 +00:00
|
|
|
if err := conn.write(); err != nil {
|
2020-11-05 12:04:10 +00:00
|
|
|
return err
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
2020-11-05 16:51:20 +00:00
|
|
|
resp, err := conn.read()
|
2020-09-01 21:46:44 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:04:10 +00:00
|
|
|
return err
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
2020-11-05 16:51:20 +00:00
|
|
|
if cmd.ping {
|
|
|
|
fmt.Printf("Ping: %+v\n", conn.pingServer())
|
|
|
|
if err = conn.conn.Close(); err != nil {
|
|
|
|
conn.conn.Close()
|
2020-11-05 12:04:10 +00:00
|
|
|
return err
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
2020-11-05 16:51:20 +00:00
|
|
|
return nil
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
2020-11-05 16:51:20 +00:00
|
|
|
if cmd.raw {
|
|
|
|
json, err := json.MarshalIndent(&resp, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
conn.conn.Close()
|
2020-11-05 12:04:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-05 16:51:20 +00:00
|
|
|
fmt.Printf("%s\n", string(json))
|
|
|
|
conn.conn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Name: %s\nPlayers: %d/%d\nVersion: %s\n",
|
|
|
|
resp.Description.Text,
|
|
|
|
resp.Players.Online,
|
|
|
|
resp.Players.Max,
|
|
|
|
resp.Version.Name)
|
|
|
|
if resp.Players.Online >= 1 {
|
|
|
|
fmt.Println("Online:")
|
|
|
|
for _, player := range resp.Players.Sample {
|
|
|
|
fmt.Printf("\t%s\n", player.Name)
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 16:51:20 +00:00
|
|
|
fmt.Printf("Ping: %+v\n", conn.pingServer())
|
|
|
|
if err = conn.conn.Close(); err != nil {
|
|
|
|
conn.conn.Close()
|
2020-11-05 12:04:10 +00:00
|
|
|
return err
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
2020-11-05 12:04:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|