41 lines
936 B
Go
41 lines
936 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/mcstatus-io/mcutil/v4/status"
|
|
)
|
|
|
|
func main() {
|
|
// Define flags
|
|
hostname := flag.String("host", "my-mc.link", "Minecraft server hostname")
|
|
port := flag.Int("port", 25565, "Minecraft server port")
|
|
flag.Parse()
|
|
|
|
// Create context with timeout
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
|
defer cancel()
|
|
|
|
// Query server status
|
|
response, err := status.Bedrock(ctx, *hostname, uint16(*port))
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Convert response to formatted JSON
|
|
jsonOutput, err := json.MarshalIndent(response, "", " ")
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error marshaling JSON: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Print JSON
|
|
fmt.Println(string(jsonOutput))
|
|
}
|