Files
devvm/internal/cli/root.go
T
w3cjandClaude Opus 5.5 4bbd35a24b refactor(ui): make the UI an instance only cli holds
internal/ui has no package state: ui.UI holds the streams, terminal
flags, run log, status line, cleanup stack and interrupt flag, and its
functions are methods. ui.Main makes the run's UI on devvm's own
streams, owns signals, recovers Die and runs the cleanups; ui.New makes
one on other streams. cmd/devvm passes it to cli.Execute, and every cli
command gets it through its constructor.

Only cmd/devvm and internal/cli import internal/ui (a depguard rule).
Each other package defines the small UI interface it needs, which its
constructor takes, filling fields tests set:
- gitprotect.New, secrets.NewStore, from.NewLookup: a warn func
- ubuntu.New: LoggedCmd, Note
- netpolicy.NewProxy: Note, Warn, Ask, Confirm, StdinIsTerminal; its
  log lines drop control characters themselves (oneLine)
- sshagent.New: Info, Note, Warn, LogWriter
- tools.New: Info, Warn, LogNote, Status, LoggedCmd, AddCleanup
- lima.New, smolvm.New: Info, Warn, LoggedCmd (their Runner's Run),
  WithSteps, AddCleanup, Interrupted; lima LogNote and ubuntu's too
- backends.Registry: lima's and smolvm's
- vm.NewEnv: all of the above, Say, Stdout, Stderr; Env has Stdout,
  LogNote, LogWriter, LoggedCmd, AddCleanup, Interrupted fields
- build.Builder: Terminal and LogNote fields
- sshconfig.New: its caller sets what it prints and asks

ui's tests build their own UI over buffers and run in parallel; other
packages' tests use small fakes; cli's unit tests a UI over buffers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-25 16:23:51 -06:00

65 lines
2.6 KiB
Go

// Package cli holds devvm's commands.
package cli
import (
"context"
"errors"
"github.com/spf13/cobra"
"github.com/w3cj/devvm/internal/msg"
"github.com/w3cj/devvm/internal/ui"
)
// NewRoot returns the devvm command with every subcommand registered, each
// printing and asking through u. devvm itself is up: a shell in this
// folder's VM.
func NewRoot(u *ui.UI) *cobra.Command {
g := new(globals)
uf := new(upFlags)
root := &cobra.Command{
Use: "devvm [IMAGE]",
Short: "Open a shell in this folder's VM, creating it first",
Long: "Open a shell in this folder's VM, at the same path, creating the VM first. A new VM " +
"gets IMAGE (base, your layers and the shared tools joined with +: node+python+claude), " +
"else the toolchains the folder's files call for, with any agents IMAGE names on top, built " +
"if it isn't. Only the folder is mounted, read-write, with the shared tools read-only, and " +
"its .git hooks and config are locked against changes from the VM (macOS); with --clone, " +
"nothing is mounted but the shared tools, and the VM gets its own clone of the repo. A VM that exists " +
"is started if it's stopped and opened as it is; an IMAGE that names what it lacks adds that " +
"in place, after asking.",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return up(cmd.Context(), u, g, uf, args)
},
}
root.SetOut(u.Stdout())
addGlobals(root, g)
addUpFlags(root, uf)
addVersion(root)
root.AddCommand(newBuildCmd(u, g), newRebuildCmd(u, g), newImagesCmd(u, g), newGCCmd(u, g), newLayerCmd(u), newSetupCmd(u), newMsgCmd(u),
newRestartCmd(u, g), newStopCmd(u), newRemoveCmd(u, g), newUpdateCmd(u, g), newAdoptCmd(u), newExportCmd(u), newRmCmd(u), newLsCmd(u), newLogCmd(u), newPolicyCmd(u), newNetworkCmd(u), newSSHCmd(u), newCodeCmd(u), newSSHAgentCmd(u), newGitProtectCmd(u), newPortsCmd(u), newSizeCmd(u), newDoctorCmd(u, g), newLimactlCmd(u), newQEMUCmd(u), newProxyCmd(u), newRefreshToolsCmd(u), newSecretCmd(u),
newAuthCmd(u), newVersionCmd(u), newCompletionCmd(u))
addCompletions(root, u)
return root
}
// Execute runs devvm with args on u and returns its exit code. A command's
// error is printed like Warn: a message as it is, the command line
// parser's own ("unknown command") in usage_error.
func Execute(ctx context.Context, u *ui.UI, args []string) int {
root := NewRoot(u)
root.SetArgs(args)
if err := root.ExecuteContext(ctx); err != nil {
m, ok := errors.AsType[msg.M](err)
if !ok {
m = msg.UsageError(err.Error())
}
u.Warn(m)
return 1
}
return 0
}