summaryrefslogtreecommitdiff
path: root/golang/cmd
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-06 00:18:22 +0200
committerJulien Dessaux2024-05-06 00:18:22 +0200
commit427cc77fa3633aca7c4c3377fa26b0e70921a7b5 (patch)
treecc8de99037ab539a0786f568cf217b680ac99ec3 /golang/cmd
parent[node] fix shortest path to only hop through refueling points (diff)
downloadspacetraders-427cc77fa3633aca7c4c3377fa26b0e70921a7b5.tar.gz
spacetraders-427cc77fa3633aca7c4c3377fa26b0e70921a7b5.tar.bz2
spacetraders-427cc77fa3633aca7c4c3377fa26b0e70921a7b5.zip
[golang] bootstrapped a client in yet another language
Diffstat (limited to 'golang/cmd')
-rw-r--r--golang/cmd/spacetraders/main.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/golang/cmd/spacetraders/main.go b/golang/cmd/spacetraders/main.go
new file mode 100644
index 0000000..937144c
--- /dev/null
+++ b/golang/cmd/spacetraders/main.go
@@ -0,0 +1,78 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "os"
+ "os/signal"
+
+ "git.adyxax.org/adyxax/spacetraders/v2/pkg/api"
+ "git.adyxax.org/adyxax/spacetraders/v2/pkg/database"
+)
+
+func main() {
+ opts := &slog.HandlerOptions{
+ // //AddSource: true,
+ Level: slog.LevelDebug,
+ }
+ //logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
+ logger := slog.New(slog.NewTextHandler(os.Stdout, opts))
+ slog.SetDefault(logger)
+
+ ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
+ defer cancel()
+ db, err := database.DBInit(ctx, "./spacetraders.db")
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "DbInit error %+v\n", err)
+ os.Exit(1)
+ }
+ client := api.NewClient(ctx)
+ defer client.Close()
+ err = run( //ctx,
+ db,
+ client,
+ //os.Args,
+ //os.Getenv,
+ //os.Getwd,
+ //os.Stdin,
+ //os.Stdout,
+ //os.Stderr,
+ )
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s\n", err)
+ if err = db.Close(); err != nil {
+ fmt.Fprintf(os.Stderr, "%s\n", err)
+ }
+ os.Exit(2)
+ }
+}
+
+func run( // ctx context.Context,
+ db *database.DB,
+ client *api.Client,
+ //args []string,
+ //getenv func(string) string,
+ //getwd func() (string, error),
+ //stdin io.Reader,
+ //stdout, stderr io.Writer,
+) (err error) {
+ // ----- Get token or register ---------------------------------------------
+ token, err := db.GetToken()
+ if err != nil || token == "" {
+ var r api.APIMessage[api.RegisterMessage, any]
+ if r, err = client.Register("COSMIC", "ADYXAX-GO"); err != nil {
+ // TODO handle server reset
+ fmt.Printf("%+v, %+v\n", r, err)
+ return err
+ }
+ if err = db.AddToken(r.Data.Token); err != nil {
+ return err
+ }
+ }
+ client.SetToken(token)
+ // ----- Update agent ------------------------------------------------------
+ agent, err := client.MyAgent()
+ slog.Info("agent", "agent", agent, "err", err)
+ return err
+}