Files
devvm/internal/cli/tools.go
T
w3cjandClaude Opus 5.5 0b03f334fb feat(cli): help in groups, with short texts, examples and each command's docs page
devvm --help lists the commands in groups: Your VM, Change your VM,
Images, Disk space and This machine, with help under Cobra's Additional
Commands. A group whose commands don't exist yet, such as Links between
VMs, stays out of the help.

Each command's Long is one to three sentences, and the rules it held are
on the command's docs page. Every visible command has an Example, with
the most common invocations first, each with a comment. Flag
descriptions are shorter. Each help ends with the address of the
command's docs page, and the root's also ends with the issue tracker.

- internal/cli/help.go: commandGroups, addGroups, addDocs (Cobra's usage
  template with Docs and Issues sections), examples
- internal/msg/docs.go: commandDocs, CommandDoc, CommandDocURL,
  IssuesURL; TestDocLinks checks commandDocs (user pages only)
- docgen: each reference page has a Docs section before See also
- docs: commands.md in the help's groups, with new sections on
  restarting and stopping and on deleting a VM; install.md has doctor's
  engine, feature values, Docker and CA notes; Contributing describes a
  command's help
- tests: TestExamples, TestHelpGroupsAndDocs, TestAddGroups,
  TestWithDocs, TestCheckCommandDocs

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-01 04:39:39 -08:00

167 lines
5.3 KiB
Go

package cli
import (
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/w3cj/devvm/internal/image"
"github.com/w3cj/devvm/internal/msg"
"github.com/w3cj/devvm/internal/tools"
"github.com/w3cj/devvm/internal/ui"
"github.com/w3cj/devvm/internal/vm"
)
// newToolsCmd is devvm tools: this folder's VM's shared tools, and add,
// rm and update, which change them.
func newToolsCmd(u *ui.UI) *cobra.Command {
var asJSON bool
cmd := &cobra.Command{
Use: "tools",
Short: "Show this folder's VM's shared tools",
Long: "Show the shared tools of this folder's VM, each with its version and any newer version in devvm's tools lock. " +
"It changes nothing, starts no VM and doesn't go online.",
Example: examples(
"devvm tools", "show the VM's shared tools",
"devvm tools add python claude", "add python and Claude Code to the VM",
"devvm tools update", "move the VM to the newest versions",
),
Args: noArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
var ts []tools.VMTool
withEnv(u, func(env *vm.Env) error {
var err error
ts, err = vm.Tools{Env: env}.List(cmd.Context())
return err
}, msg.ToolsFailed)
if asJSON {
return printJSON(u, toolsJSON(ts))
}
return writeTools(u.Table(), ts)
},
}
jsonFlag(cmd, &asJSON)
cmd.AddCommand(newToolsAddCmd(u), newToolsRmCmd(u), newToolsUpdateCmd(u))
return cmd
}
// toolsOutput is devvm tools --json: the VM's shared tools, each with the
// newer version the tools lock has, when it has one, and whether that one
// is built.
type toolsOutput struct {
Tools []vmToolJSON `json:"tools"`
}
type vmToolJSON struct {
Tool string `json:"tool"`
Version string `json:"version"`
Newer string `json:"newer,omitempty"`
Built *bool `json:"newer_built,omitempty"`
}
func toolsJSON(ts []tools.VMTool) toolsOutput {
out := toolsOutput{Tools: make([]vmToolJSON, 0, len(ts))}
for _, t := range ts {
j := vmToolJSON{Tool: t.Tool, Version: t.Version, Newer: t.Newer}
if t.Newer != "" {
j.Built = &t.Built
}
out.Tools = append(out.Tools, j)
}
return out
}
// writeTools writes a VM's tools as a table, a header first.
func writeTools(tab *ui.Table, ts []tools.VMTool) error {
tab.Header("TOOL", "VERSION", "NEWER", "BUILT")
for _, t := range ts {
built := ""
if t.Newer != "" {
built = "no"
if t.Built {
built = "yes"
}
}
tab.Row(cell(t.Tool), cell(t.Version), cell(t.Newer), cell(built))
}
return tab.Flush()
}
func newToolsAddCmd(u *ui.UI) *cobra.Command {
return &cobra.Command{
Use: "add TOOL...",
Short: "Add shared tools to this folder's VM, then open a shell in it",
Long: "Add shared tools to this folder's VM, then open a shell in it. " +
"TOOL is node, python, rust, go, claude, codex or opencode, each a separate word. " +
"devvm builds any of them that aren't built, and for a new agent in a running VM it asks before restarting the VM.",
Example: examples(
"devvm tools add claude", "add Claude Code to the VM",
"devvm tools add python codex", "add python and Codex",
),
Args: toolNames("add"),
RunE: func(cmd *cobra.Command, args []string) error {
withEnv(u, func(env *vm.Env) error { return vm.Tools{Env: env}.Add(cmd.Context(), vm.Options{}, args) }, msg.UpFailed)
return nil
},
ValidArgsFunction: completeToolNames,
}
}
func newToolsRmCmd(u *ui.UI) *cobra.Command {
return &cobra.Command{
Use: "rm TOOL...",
Short: "Take shared tools out of this folder's VM, then open a shell in it",
Long: "Take shared tools out of this folder's VM, each TOOL a separate word, then open a shell in it. " +
"What the VM installed itself stays.",
Example: examples(
"devvm tools rm python", "take python out of the VM",
"devvm tools rm python claude", "take out python and Claude Code",
),
Args: toolNames("rm"),
RunE: func(cmd *cobra.Command, args []string) error {
withEnv(u, func(env *vm.Env) error { return vm.Tools{Env: env}.Remove(cmd.Context(), vm.Options{}, args) }, msg.UpFailed)
return nil
},
ValidArgsFunction: completeToolNames,
}
}
func newToolsUpdateCmd(u *ui.UI) *cobra.Command {
return &cobra.Command{
Use: "update",
Short: "Move this folder's VM to the newest shared tools, then open a shell in it",
Long: "Check for newer versions of the shared tools, build them, and move this folder's VM to them, " +
"then open a shell in it. Nothing in the VM's disk changes, because its tools are mounted.",
Example: examples(
"devvm tools update", "move the VM to the newest shared tools",
),
Args: noArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
withEnv(u, func(env *vm.Env) error { return vm.Tools{Env: env}.Update(cmd.Context(), vm.Options{}) }, msg.UpFailed)
return nil
},
}
}
// toolNames takes one or more TOOLs, each a word of its own: one joined
// with + is refused, naming sub, the devvm tools command (add, rm). add
// takes only shared tools' names.
func toolNames(sub string) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
if err := minArgs(1)(cmd, args); err != nil {
return err
}
names := slices.Concat(image.Toolchains, image.Agents)
for _, a := range args {
if strings.Contains(a, "+") {
return msg.ToolsJoined(sub, a)
}
if sub == "add" && !slices.Contains(names, a) {
return msg.UnknownTool(a, strings.Join(names, " "))
}
}
return nil
}
}