Files
w3cjandClaude Opus 5.5 3e81767482 refactor(cli): each command carries its own docs link, group and samples
A command's metadata moves from hand-kept tables into the command itself:
an about (docs page, help group, why no --json) set as its Cobra
Annotations. msg.commandDocs, commandGroups and withoutJSON are gone;
TestDocLinks reads the links from the generated CLI reference, and help
output and the reference are byte-identical. AGENTS.md's cli and vm rows
point at new doc.go package comments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-08 10:05:13 -07:00

162 lines
5.8 KiB
Go

package cli
import (
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/devvmhq/devvm/assets"
"github.com/devvmhq/devvm/internal/build"
"github.com/devvmhq/devvm/internal/image"
"github.com/devvmhq/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",
Annotations: about{
docs: "using/command-line#shell-completion", group: groupMachine,
noJSON: "it prints a shell script",
}.annotations(),
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 own flags, the
// toolchains, agents and layers for --tools, --agents and --layers, and
// that it completes no file names, which no command takes. Each other
// command sets its own, as devvm build's IMAGE does with completeImage.
func addCompletions(root *cobra.Command, a *app) {
root.CompletionOptions.DisableDefaultCmd = true
root.CompletionOptions.SetDefaultShellCompDirective(cobra.ShellCompDirectiveNoFileComp)
for _, flag := range []string{"tools", "agents", "layers"} {
_ = root.RegisterFlagCompletionFunc(flag, completeParts(a, flag))
}
_ = root.RegisterFlagCompletionFunc("ports", cobra.FixedCompletions([]string{"all", "none"}, cobra.ShellCompDirectiveNoFileComp))
}
// completeImage returns what completes IMAGE: base and the layers, after
// the layers already given.
func completeImage(a *app) cobra.CompletionFunc {
return func(_ *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return joined(toComplete, layerNames(a, true)), cobra.ShellCompDirectiveNoFileComp
}
}
// layerNames is the layers there are in a's folders, built-in and the
// user's, with base first when withBase.
func layerNames(a *app, withBase bool) []string {
layers := slices.Concat([]string{image.Base}, image.Builtins)
if dirs, err := a.dirs(); 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(a *app, 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(a, 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
}
// completeLayerNames returns what completes devvm layer add's and rm's
// LAYER...: the layers there are, but those given.
func completeLayerNames(a *app) cobra.CompletionFunc {
return func(_ *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
var out []cobra.Completion
for _, n := range layerNames(a, false) {
if strings.HasPrefix(n, toComplete) && !slices.Contains(args, n) {
out = append(out, n)
}
}
return out, cobra.ShellCompDirectiveNoFileComp
}
}
// completeFeatures returns what completes setup's --features: base's
// features, after the ones already given.
func completeFeatures(a *app) cobra.CompletionFunc {
return func(_ *cobra.Command, _ []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
dirs, err := a.dirs()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
features := (&build.Env{Dirs: dirs, Assets: assets.FS}).Features()
return joinedBy(",", toComplete, features), 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
}