Files
devvm/internal/cli/completion.go
T
w3cjandClaude Opus 5.5 a134e0d8a6 feat(cli): choose a new VM's toolchains, agents and layers in a picker or with --tools, --agents and --layers
devvm takes no IMAGE word any more than any other: in a terminal a new VM's
toolchains, coding agents and layers are checked in one picker, under three
headings, before the network-policy question; --tools, --agents and
--layers (each an exact list, or none) skip it, and a part left out takes
its default: the detected toolchains, devvm setup's agents, no layers (a
devcontainer.json's features still count). Without a terminal, or with
--no-input, the VM gets the defaults and new_vm_gets says so in one line.
devvm setup --agents (and its first-run question) chooses new VMs' agents.
devvm tools in a terminal opens the same picker for the VM, its layers
fixed, and turns the changes into tools add and rm.

An image is base and layers only: tools are the VM's tool_names state key.
build, rebuild and images rm refuse a shared tool (tool_not_layer), and
devvm build builds no tool images. devvm ls gains a TOOLS column and
--json tools. gc's --tools is --tool-images, so --tools has one meaning.

Gone: image.Reserved, checkWord's image half (devvm WORD is no_command or
no_command_like, did-you-mean over commands, msg.Closest), the lacks path
in internal/vm/existing.go, and the messages vm_lacks_tools,
vm_lacks_layer, layer_is_command, layer_name_command, no_image_for,
no_command_or_image*, tools_not_images, tools_not_images_rm, detected,
vm_is_now (vm_tools_now), secret_with_image (secret_with_claude).
New: ui.Pick takes headed sections with fixed rows, ui.CanPick,
vm.PickPart/PickRow/AgentsChoice/CheckPart (internal/vm/parts.go),
state.KeyToolNames, state.ReadAgents/WriteAgents, paths.Dirs.Agents,
project.DetectFiles. Image keys are unchanged in form: images never held
tools; docker.sh's and nix.sh's comments changed, so those layers rebuild.

Docs: the home page's quick start steps, diagram (sandbox.svg), cards and
FAQ; README; getting started; using (images' What a new VM gets, coding
agents' Running an agent, settings' agents); internals; contributing;
the cli skill; AGENTS.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 00:45:09 -07:00

179 lines
6.2 KiB
Go

package cli
import (
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/w3cj/devvm/assets"
"github.com/w3cj/devvm/internal/build"
"github.com/w3cj/devvm/internal/image"
"github.com/w3cj/devvm/internal/paths"
"github.com/w3cj/devvm/internal/ui"
)
// shells are the shells devvm completion writes a script for.
var shells = []string{"bash", "zsh", "fish"}
// newCompletionCmd is devvm completion SHELL.
func newCompletionCmd(u *ui.UI) *cobra.Command {
return &cobra.Command{
Use: "completion SHELL",
Short: "Print the shell completion script for bash, zsh or fish",
Long: "Print the script that completes devvm's commands, flags, image names and shared tools in SHELL, which is bash, zsh or fish. " +
"Load it from your shell's startup file.",
Example: examples(
"source <(devvm completion bash)", "in ~/.bashrc, with the bash-completion package installed",
"source <(devvm completion zsh)", "in ~/.zshrc, after compinit",
"devvm completion fish | source", "in ~/.config/fish/config.fish",
),
Args: exactArgs(1),
ValidArgs: shells,
RunE: func(cmd *cobra.Command, args []string) error {
root, out := cmd.Root(), u.Stdout()
switch args[0] {
case "bash":
return root.GenBashCompletionV2(out, true)
case "zsh":
return root.GenZshCompletion(out)
default:
return root.GenFishCompletion(out, true)
}
},
}
}
// addCompletions sets how the shell completes root's commands' arguments
// and flags: image names for IMAGE, the toolchains, agents and layers for
// --tools, --agents and --layers, backend names for --backend, and no
// file names, which none of them take.
func addCompletions(root *cobra.Command, u *ui.UI) {
backendNames := completeBackend(u)
root.CompletionOptions.DisableDefaultCmd = true
root.CompletionOptions.SetDefaultShellCompDirective(cobra.ShellCompDirectiveNoFileComp)
for _, c := range root.Commands() {
switch c.Name() {
case "build", "rebuild":
c.ValidArgsFunction = completeImage
case "setup":
_ = c.RegisterFlagCompletionFunc("features", completeFeatures)
_ = c.RegisterFlagCompletionFunc("agents", completeParts("agents"))
}
}
for _, flag := range []string{"tools", "agents", "layers"} {
_ = root.RegisterFlagCompletionFunc(flag, completeParts(flag))
}
completeBackendFlags(root, backendNames)
_ = root.RegisterFlagCompletionFunc("ports", cobra.FixedCompletions([]string{"all", "none"}, cobra.ShellCompDirectiveNoFileComp))
}
// completeBackendFlags completes --backend with backend names on c and
// every command under it that takes it.
func completeBackendFlags(c *cobra.Command, names cobra.CompletionFunc) {
if c.Flags().Lookup("backend") != nil {
_ = c.RegisterFlagCompletionFunc("backend", names)
}
for _, sub := range c.Commands() {
completeBackendFlags(sub, names)
}
}
// completeImage completes IMAGE: base and the layers, after the layers
// already given.
func completeImage(_ *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return joined(toComplete, layerNames(true)), cobra.ShellCompDirectiveNoFileComp
}
// layerNames is the layers there are, built-in and the user's, with base
// first when withBase.
func layerNames(withBase bool) []string {
layers := slices.Concat([]string{image.Base}, image.Builtins)
if dirs, err := paths.FromEnv(); err == nil {
layers = (&build.Env{Dirs: dirs}).Layers()
}
if withBase {
return layers
}
return layers[1:]
}
// completeParts returns what completes --tools, --agents and --layers
// (flag): a comma-separated list of the part's names, after those already
// given, or none.
func completeParts(flag string) cobra.CompletionFunc {
return func(_ *cobra.Command, _ []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
names := map[string][]string{"tools": image.Toolchains, "agents": image.Agents}[flag]
if flag == "layers" {
names = layerNames(false)
}
out := joinedBy(",", toComplete, names)
if strings.HasPrefix(partsNone, toComplete) {
out = append(out, partsNone)
}
return out, cobra.ShellCompDirectiveNoFileComp
}
}
// completeToolNames completes TOOL...: the toolchains and the agents not
// already given.
func completeToolNames(_ *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
var out []cobra.Completion
for _, n := range slices.Concat(image.Toolchains, image.Agents) {
if strings.HasPrefix(n, toComplete) && !slices.Contains(args, n) {
out = append(out, n)
}
}
return out, cobra.ShellCompDirectiveNoFileComp
}
// completeFeatures completes setup's --features: base's features, after
// the ones already given.
func completeFeatures(_ *cobra.Command, _ []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
dirs, err := paths.FromEnv()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
features, _ := (&build.Env{Dirs: dirs, Assets: assets.FS}).Features()
return joinedBy(",", toComplete, features), cobra.ShellCompDirectiveNoFileComp
}
// completeBackend returns what completes --backend with the backends'
// names.
func completeBackend(u *ui.UI) cobra.CompletionFunc {
return func(*cobra.Command, []string, string) ([]cobra.Completion, cobra.ShellCompDirective) {
dirs, err := paths.FromEnv()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
reg, err := newRegistry(dirs, assets.FS, u)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return reg.Names(), cobra.ShellCompDirectiveNoFileComp
}
}
// joined completes the last of the +-joined parts in toComplete with the
// names it starts, leaving out the parts already given: docker+f gives
// docker+foo and docker+fop.
func joined(toComplete string, names []string) []cobra.Completion {
return joinedBy("+", toComplete, names)
}
// joinedBy is joined for parts joined by sep.
func joinedBy(sep, toComplete string, names []string) []cobra.Completion {
i := strings.LastIndex(toComplete, sep) + 1
given := strings.Split(toComplete[:i], sep)
var out []cobra.Completion
for _, n := range names {
if strings.HasPrefix(n, toComplete[i:]) && !slices.Contains(given, n) {
out = append(out, toComplete[:i]+n)
}
}
return out
}