From 427cc77fa3633aca7c4c3377fa26b0e70921a7b5 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 6 May 2024 00:18:22 +0200 Subject: [golang] bootstrapped a client in yet another language --- golang/cmd/spacetraders/main.go | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 golang/cmd/spacetraders/main.go (limited to 'golang/cmd') 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 +} -- cgit v1.2.3