feat: optional first-run setup wizard 🧭

Bare 'devbox' on a fresh, unjoined machine offers a step-by-step wizard:
  [Y] yes please   [n] no, I'm a devbox expert — don't ask again
'n' writes a setup-skipped marker in the config dir and never asks again;
'devbox setup' runs it explicitly anytime. The wizard owns the one fiddly
step (join: hub URL + token, with a hint where to get the token) then
points at mount/publish/start — mount stays a printed next-step, not a
reimplementation (ponytail). Gated on a real TTY so pipes/scripts are
never blocked; extracted a shared joinHub helper so the wizard and
'devbox join' don't diverge. Stdlib ModeCharDevice (no go-isatty).
Test: opt-out gate + don't-ask-again persistence.
This commit is contained in:
shoemoney 2026-06-23 12:21:56 -05:00
parent 05ebdeea77
commit 4fa2ef1369
3 changed files with 184 additions and 20 deletions

View file

@ -59,8 +59,16 @@ func main() {
resumeCmd(),
statusCmd(),
doctorCmd(),
setupCmd(),
versionCmd(),
)
// First run: on a bare `devbox` (no subcommand) on a terminal, offer the setup
// wizard if this machine isn't joined yet and the user hasn't opted out.
if len(os.Args) == 1 {
if dir, err := config.Dir(); err == nil && maybeOfferSetup(dir) {
return
}
}
if err := root.Execute(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
@ -83,41 +91,46 @@ func joinCmd() *cobra.Command {
Short: "🎟️ enroll this device against a hub",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
hubURL, token := args[0], args[1]
dir, err := config.Dir()
if err != nil {
return err
}
id, err := identity.LoadOrCreate(dir)
resp, err := joinHub(dir, args[0], args[1])
if err != nil {
return err
}
name, _ := os.Hostname()
c := transport.New(hubURL)
resp, err := c.Join(token, name, id.Pub, id.Priv)
if err != nil {
return err
}
d, err := config.LoadDaemon(dir)
if err != nil {
return err
}
d.Hub, d.DeviceID, d.Bearer = hubURL, resp.DeviceID, resp.Bearer
if err := config.SaveDaemon(dir, d); err != nil {
return err
}
out := cmd.OutOrStdout()
fmt.Fprintf(out, "device: %s\n", resp.DeviceID)
fmt.Fprintf(out, "hub: %s\n", hubURL)
fmt.Fprintf(out, "hub: %s\n", args[0])
fmt.Fprintln(out, "✅ joined")
return nil
},
}
}
// joinHub enrolls this device against hubURL with token and persists the bearer
// to the daemon config. Shared by `devbox join` and the first-run setup wizard.
func joinHub(dir, hubURL, token string) (proto.JoinResponse, error) {
id, err := identity.LoadOrCreate(dir)
if err != nil {
return proto.JoinResponse{}, err
}
name, _ := os.Hostname()
resp, err := transport.New(hubURL).Join(token, name, id.Pub, id.Priv)
if err != nil {
return proto.JoinResponse{}, err
}
d, err := config.LoadDaemon(dir)
if err != nil {
return proto.JoinResponse{}, err
}
d.Hub, d.DeviceID, d.Bearer = hubURL, resp.DeviceID, resp.Bearer
if err := config.SaveDaemon(dir, d); err != nil {
return proto.JoinResponse{}, err
}
return resp, nil
}
func publishCmd() *cobra.Command {
return &cobra.Command{
Use: "publish <localdir> <share>",

122
cmd/devbox/wizard.go Normal file
View file

@ -0,0 +1,122 @@
package main
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"git.shoemoney.ai/shoemoney/devbox/internal/config"
)
// setupSkippedFile, when present in the config dir, means the user chose "don't
// ask again" — the first-run wizard is never auto-offered after that.
const setupSkippedFile = "setup-skipped"
func setupCmd() *cobra.Command {
return &cobra.Command{
Use: "setup",
Short: "🧭 step-by-step first-time setup wizard",
RunE: func(cmd *cobra.Command, _ []string) error {
dir, err := config.Dir()
if err != nil {
return err
}
return runWizard(os.Stdin, cmd.OutOrStdout(), dir)
},
}
}
// maybeOfferSetup offers the wizard on a bare `devbox` when the machine isn't set
// up and the user hasn't opted out — and ONLY on a real terminal, so scripts and
// pipes are never blocked on a prompt. Returns true if it ran the wizard (so the
// caller skips the usage dump).
func maybeOfferSetup(dir string) bool {
if !stdinIsTTY() || !shouldOfferSetup(dir) {
return false
}
ran, _ := offerSetup(os.Stdin, os.Stdout, dir)
return ran
}
// shouldOfferSetup is true only before the first join and before any opt-out.
func shouldOfferSetup(dir string) bool {
if _, err := os.Stat(filepath.Join(dir, setupSkippedFile)); err == nil {
return false // opted out
}
d, err := config.LoadDaemon(dir)
return err == nil && d.Hub == "" // not joined yet
}
// offerSetup asks the one question. "n…" records the opt-out (never ask again);
// anything else launches the wizard. ranWizard reports whether the wizard ran.
func offerSetup(in io.Reader, out io.Writer, dir string) (ranWizard bool, err error) {
r := bufio.NewReader(in)
fmt.Fprintln(out, "👋 devbox isn't set up on this machine yet.")
fmt.Fprintln(out, "Would you like a step-by-step setup wizard?")
fmt.Fprintln(out, " [Y] yes please")
fmt.Fprintln(out, " [n] no, I'm a devbox expert — don't ask again")
fmt.Fprint(out, "> ")
line, _ := r.ReadString('\n')
if strings.HasPrefix(strings.ToLower(strings.TrimSpace(line)), "n") {
if err := markSetupSkipped(dir); err != nil {
return false, err
}
fmt.Fprintln(out, "👍 You're flying solo — run `devbox setup` anytime to change your mind.")
return false, nil
}
return true, runWizardReader(r, out, dir)
}
func markSetupSkipped(dir string) error {
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
return os.WriteFile(filepath.Join(dir, setupSkippedFile), []byte("skipped\n"), 0o644)
}
// runWizard walks a new user through the one genuinely fiddly step — enrolling
// against a hub — then points at the next commands. Mount/publish are left as
// printed next-steps: they're self-explanatory once joined (ponytail: the wizard
// owns the hard part, not a reimplementation of every command).
func runWizard(in io.Reader, out io.Writer, dir string) error {
return runWizardReader(bufio.NewReader(in), out, dir)
}
func runWizardReader(r *bufio.Reader, out io.Writer, dir string) error {
ask := func(prompt string) string {
fmt.Fprint(out, prompt)
s, _ := r.ReadString('\n')
return strings.TrimSpace(s)
}
fmt.Fprintln(out, "\n🧭 Let's get this machine syncing.")
hub := ask("\n1) Your hub URL (e.g. http://nas.local:8088):\n> ")
if hub == "" {
return fmt.Errorf("setup cancelled (no hub given)")
}
fmt.Fprintln(out, "\n2) A join token — get one on the hub with: devbox-hub token")
token := ask(" Paste it here:\n> ")
if token == "" {
return fmt.Errorf("setup cancelled (no token given)")
}
resp, err := joinHub(dir, hub, token)
if err != nil {
return fmt.Errorf("join failed: %w", err)
}
fmt.Fprintf(out, "\n✅ Joined as device %s.\n\nNext steps:\n", resp.DeviceID)
fmt.Fprintln(out, " • sync a hub folder here: devbox mount <share> <dir>")
fmt.Fprintln(out, " • share a local folder: devbox publish <dir> <share>")
fmt.Fprintln(out, " • then start syncing: devbox start")
return nil
}
// stdinIsTTY reports whether stdin is an interactive terminal (so we never prompt
// in a pipe or script). ponytail: stdlib ModeCharDevice, no go-isatty dep.
func stdinIsTTY() bool {
fi, err := os.Stdin.Stat()
return err == nil && fi.Mode()&os.ModeCharDevice != 0
}

29
cmd/devbox/wizard_test.go Normal file
View file

@ -0,0 +1,29 @@
package main
import (
"strings"
"testing"
)
// TestSetupOptOut covers the gate + the "don't ask again" persistence: a fresh
// machine is offered the wizard, answering "n" records the opt-out, and after
// that the wizard is never auto-offered again.
func TestSetupOptOut(t *testing.T) {
dir := t.TempDir()
if !shouldOfferSetup(dir) {
t.Fatal("a fresh (unjoined) machine should be offered the wizard")
}
var out strings.Builder
ran, err := offerSetup(strings.NewReader("n\n"), &out, dir)
if err != nil {
t.Fatalf("offerSetup: %v", err)
}
if ran {
t.Fatal(`answering "n" must NOT run the wizard`)
}
if shouldOfferSetup(dir) {
t.Fatal("after opt-out, the wizard must never be auto-offered again")
}
}