2020-09-01 21:46:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-11-05 12:04:10 +00:00
|
|
|
"os"
|
2020-12-15 21:13:50 +00:00
|
|
|
|
|
|
|
"git.0cd.xyz/michael/mcstatus/client"
|
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()
|
2020-12-15 21:13:50 +00:00
|
|
|
client, err := client.New(cmd.addr, cmd.port, cmd.version)
|
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-12-15 21:13:50 +00:00
|
|
|
defer client.Conn.Close()
|
2020-09-01 21:46:44 +00:00
|
|
|
for {
|
2020-12-15 21:13:50 +00:00
|
|
|
status, err := client.GetStatus()
|
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 {
|
2020-12-15 21:13:50 +00:00
|
|
|
fmt.Printf("Ping: %+v\n", client.PingServer())
|
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 {
|
2020-12-15 21:13:50 +00:00
|
|
|
json, err := json.MarshalIndent(&status, "", " ")
|
2020-11-05 16:51:20 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:04:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-05 16:51:20 +00:00
|
|
|
fmt.Printf("%s\n", string(json))
|
2020-12-15 21:13:50 +00:00
|
|
|
return nil
|
2020-11-05 16:51:20 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("Name: %s\nPlayers: %d/%d\nVersion: %s\n",
|
2020-12-15 21:13:50 +00:00
|
|
|
status.Description.Text,
|
|
|
|
status.Players.Online,
|
|
|
|
status.Players.Max,
|
|
|
|
status.Version.Name)
|
|
|
|
if status.Players.Online >= 1 {
|
2020-11-05 16:51:20 +00:00
|
|
|
fmt.Println("Online:")
|
2020-12-15 21:13:50 +00:00
|
|
|
for _, player := range status.Players.Sample {
|
2020-11-05 16:51:20 +00:00
|
|
|
fmt.Printf("\t%s\n", player.Name)
|
2020-09-01 21:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-15 21:13:50 +00:00
|
|
|
fmt.Printf("Ping: %+v\n", client.PingServer())
|
2020-11-05 12:04:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|