From 69a2d59b231427b6cfc3563cc13d8d235f406133 Mon Sep 17 00:00:00 2001 From: Emil Date: Sun, 2 Aug 2026 22:43:17 +0300 Subject: [PATCH] Add the coordinator token subcommand for joining workers --- coordinator/cmd/coordinator/main.go | 6 +++++ coordinator/cmd/coordinator/token_cmd.go | 33 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 coordinator/cmd/coordinator/token_cmd.go diff --git a/coordinator/cmd/coordinator/main.go b/coordinator/cmd/coordinator/main.go index 6c13e78..c1e6a69 100644 --- a/coordinator/cmd/coordinator/main.go +++ b/coordinator/cmd/coordinator/main.go @@ -46,6 +46,12 @@ func main() { os.Exit(1) } return + case "token": + if err := runToken(args[1:]); err != nil { + fmt.Fprintln(os.Stderr, "token:", err) + os.Exit(1) + } + return } } showVersion := flag.Bool("version", false, "print the build version and exit") diff --git a/coordinator/cmd/coordinator/token_cmd.go b/coordinator/cmd/coordinator/token_cmd.go new file mode 100644 index 0000000..3405e6b --- /dev/null +++ b/coordinator/cmd/coordinator/token_cmd.go @@ -0,0 +1,33 @@ +package main + +import ( + "flag" + "fmt" + "os" + "path/filepath" +) + +// runToken implements `coordinator token`: prints the worker auth token of a +// `coordinator serve` instance, so a scientist can join a worker without +// hunting through the data directory. The file itself is what serve created. +func runToken(args []string) error { + flags := flag.NewFlagSet("token", flag.ContinueOnError) + flags.Usage = func() { + _, _ = fmt.Fprintf(flags.Output(), "usage: coordinator token [options]\n") + _, _ = fmt.Fprintf(flags.Output(), "Prints the WORKER_AUTH_TOKEN of this coordinator's serve instance.\n\n") + flags.PrintDefaults() + } + var dataDir = flags.String("data-dir", defaultDataDir(), "data directory (default: ~/.scimesh)") + if err := flags.Parse(args); err != nil { + return err + } + if flags.NArg() > 0 { + return fmt.Errorf("token takes no positional arguments") + } + token, err := os.ReadFile(filepath.Join(*dataDir, "worker.token")) + if err != nil { + return fmt.Errorf("no worker token found in %s — start the coordinator with `coordinator serve` first", *dataDir) + } + fmt.Print(string(token)) + return nil +}