Repository navigation
Expand file tree
/
Copy pathcompletion.go
More file actions
62 lines (49 loc) · 1.44 KB
/
Copy pathcompletion.go
File metadata and controls
62 lines (49 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"embed"
"flag"
"fmt"
"maps"
"os"
"slices"
)
//go:embed completions
var completionScripts embed.FS
const completionUsage = `Print the shell completion script for dev-vm.
Usage: devvm completion <bash|zsh|fish>
Load it for the current shell:
bash: source <(devvm completion bash)
zsh: source <(devvm completion zsh)
fish: devvm completion fish | source
Install it permanently:
bash: devvm completion bash > /usr/local/etc/bash_completion.d/dev-vm
zsh: devvm completion zsh > "${fpath[1]}/_dev-vm"
fish: devvm completion fish > ~/.config/fish/completions/dev-vm.fish
The script completes VM names for start, stop, delete, disk-resize and status.
It gets the names from "devvm __names".
`
func cmdCompletion(argv []string) {
fs := flag.NewFlagSet("completion", flag.ExitOnError)
fs.Usage = func() {
fmt.Fprint(os.Stderr, completionUsage)
}
fs.Parse(argv)
if fs.NArg() != 1 {
fmt.Fprint(os.Stderr, completionUsage)
die("completion needs exactly one shell: bash, zsh or fish")
}
shell := fs.Arg(0)
script, err := completionScripts.ReadFile("completions/dev-vm." + shell)
if err != nil {
die("unsupported shell %q; use bash, zsh or fish", shell)
}
os.Stdout.Write(script)
}
// cmdNames prints the recorded VM names, one per line, for the completion
// scripts.
func cmdNames() {
vms, _ := loadState()["vms"].(map[string]any)
for _, name := range slices.Sorted(maps.Keys(vms)) {
fmt.Println(name)
}
}