From 88087e0338680a40fbf923c05b93da4f45de66ae Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Fri, 5 Jun 2020 19:08:36 +0100 Subject: [PATCH] Implement most rove commands Need to finish up /commands Need to pretify output of others --- cmd/rove/main.go | 159 ++++++++++++++++++++++++++++++++++++++++++++++- script/build.sh | 1 + 2 files changed, 159 insertions(+), 1 deletion(-) diff --git a/cmd/rove/main.go b/cmd/rove/main.go index 9a12136..c429ee7 100644 --- a/cmd/rove/main.go +++ b/cmd/rove/main.go @@ -1,7 +1,164 @@ package main -import "flag" +import ( + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "os" + "path" + + "github.com/mdiluz/rove/pkg/rove" +) + +var USAGE = "" + +// Command usage +func Usage() { + fmt.Printf("Usage: %s [OPTIONS]... COMMAND\n", os.Args[0]) + fmt.Println("\nCommands:") + fmt.Println("\tstatus \tprints the server status") + fmt.Println("\tregister\tregisters an account and stores it (use with -name)") + fmt.Println("\tspawn \tspawns a rover for the current account") + fmt.Println("\tcommands\tissues commands to the rover") + fmt.Println("\tradar \tgathers radar data for the current rover") + fmt.Println("\nOptions:") + flag.PrintDefaults() +} + +var home = os.Getenv("HOME") +var filepath = path.Join(home, ".local/share/rove.json") + +var host = flag.String("host", "api.rove-game.com", "path to game host server") +var dataPath = flag.String("data", filepath, "data file for storage") + +// Data is used to store internal data +type Data struct { + Account string `json:"account,omitempty"` + Host string `json:"host,omitempty"` +} + +var name = flag.String("name", "", "used with status command for the account name") + +func verifyId(d Data) { + if len(d.Account) == 0 { + fmt.Fprintf(os.Stderr, "No account ID set, must register first or set \"account\" value in %s\n", *dataPath) + os.Exit(1) + } +} func main() { + flag.Usage = Usage flag.Parse() + + // Verify we have a single command line arg + args := flag.Args() + if len(args) != 1 { + Usage() + os.Exit(1) + } + + // Load in the persistent file + var data = Data{} + _, err := os.Stat(*dataPath) + if !os.IsNotExist(err) { + // Read and unmarshal the json + if b, err := ioutil.ReadFile(*dataPath); err != nil { + fmt.Fprintf(os.Stderr, "failed to read file %s error: %s\n", *dataPath, err) + os.Exit(1) + } else if len(b) == 0 { + fmt.Fprintf(os.Stderr, "file %s was empty, assumin fresh data\n", *dataPath) + + } else if err := json.Unmarshal(b, &data); err != nil { + fmt.Fprintf(os.Stderr, "failed to unmarshal file %s error: %s\n", *dataPath, err) + os.Exit(1) + } + } + + var server = rove.Server(*host) + + command := args[0] + switch command { + case "status": + if response, err := server.Status(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + + } else { + fmt.Printf("Ready: %t\n", response.Ready) + fmt.Printf("Version: %s\n", response.Version) + } + case "register": + d := rove.RegisterData{ + Name: *name, + } + if response, err := server.Register(d); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + + } else if !response.Success { + fmt.Fprintf(os.Stderr, "Server returned failure: %s\n", response.Error) + os.Exit(1) + + } else { + fmt.Printf("Registered account with id: %s\n", response.Id) + data.Account = response.Id + } + case "spawn": + verifyId(data) + d := rove.SpawnData{Id: data.Account} + if response, err := server.Spawn(d); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + + } else if !response.Success { + fmt.Fprintf(os.Stderr, "Server returned failure: %s\n", response.Error) + os.Exit(1) + + } else { + fmt.Printf("Spawned at position %+v\n", response.Position) + } + case "commands": + verifyId(data) + d := rove.CommandsData{Id: data.Account} + // TODO: Send real commands in + if response, err := server.Commands(d); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + + } else if !response.Success { + fmt.Fprintf(os.Stderr, "Server returned failure: %s\n", response.Error) + os.Exit(1) + + } else { + // TODO: Pretify the response + fmt.Printf("%+v\n", response) + } + case "radar": + verifyId(data) + d := rove.RadarData{Id: data.Account} + if response, err := server.Radar(d); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + + } else if !response.Success { + fmt.Fprintf(os.Stderr, "Server returned failure: %s\n", response.Error) + os.Exit(1) + + } else { + // TODO: Pretify the response + fmt.Printf("%+v\n", response) + } + } + + // Save out the persistent file + if b, err := json.MarshalIndent(data, "", "\t"); err != nil { + fmt.Fprintf(os.Stderr, "failed to marshal data error: %s\n", err) + os.Exit(1) + } else { + if err := ioutil.WriteFile(*dataPath, b, os.ModePerm); err != nil { + fmt.Fprintf(os.Stderr, "failed to save file %s error: %s\n", *dataPath, err) + os.Exit(1) + } + } } diff --git a/script/build.sh b/script/build.sh index b0dbdbb..e375620 100755 --- a/script/build.sh +++ b/script/build.sh @@ -8,4 +8,5 @@ set -x export VERSION=$(git describe --always --long --dirty --tags) # Build and tag as latest and version +docker build -t "rove:latest" -t "rove:${VERSION}" -f "cmd/rove/Dockerfile" . docker build -t "rove-server:latest" -t "rove-server:${VERSION}" -f "cmd/rove-server/Dockerfile" . \ No newline at end of file