Skip to content

Commit c859384

Browse files
committed
feat(golang-rewrite): self-contained completion code
* Add `asdf completion` command * Move completion files to `cli/completions` * Add completions for Bash, Zsh, Fish, Elvish, and Nushell * Update Zsh completion code to work with new completion install method
1 parent 08ca28f commit c859384

File tree

7 files changed

+455
-3
lines changed

7 files changed

+455
-3
lines changed

cli/cli.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package cli
33

44
import (
5+
_ "embed"
56
"errors"
67
"fmt"
78
"io"
@@ -71,6 +72,13 @@ func Execute(version string) {
7172
return extensionCommand(logger, args)
7273
},
7374
},
75+
{
76+
Name: "completion",
77+
Action: func(cCtx *cli.Context) error {
78+
shell := cCtx.Args().Get(0)
79+
return completionCommand(logger, shell)
80+
},
81+
},
7482
{
7583
Name: "current",
7684
Flags: []cli.Flag{
@@ -307,6 +315,47 @@ func Execute(version string) {
307315
}
308316
}
309317

318+
//go:embed completions/asdf.bash
319+
var bashCompletions string
320+
321+
//go:embed completions/asdf.zsh
322+
var zshCompletions string
323+
324+
//go:embed completions/asdf.fish
325+
var fishCompletions string
326+
327+
//go:embed completions/asdf.nu
328+
var nuCompletions string
329+
330+
//go:embed completions/asdf.elv
331+
var elvishCompletions string
332+
333+
func completionCommand(l *log.Logger, shell string) error {
334+
switch shell {
335+
case "bash":
336+
fmt.Print(bashCompletions)
337+
return nil
338+
case "zsh":
339+
fmt.Print(zshCompletions)
340+
return nil
341+
case "fish":
342+
fmt.Print(fishCompletions)
343+
return nil
344+
case "nushell":
345+
fmt.Print(nuCompletions)
346+
return nil
347+
case "elvish":
348+
fmt.Print(elvishCompletions)
349+
return nil
350+
default:
351+
fmtString := `No completions available for shell with name %s
352+
Completions are available for: bash, zsh, fish, nushell, elvish`
353+
msg := fmt.Sprintf(fmtString, shell)
354+
l.Print(msg)
355+
return errors.New(msg)
356+
}
357+
}
358+
310359
// This function is a whole mess and needs to be refactored
311360
func currentCommand(logger *log.Logger, tool string, noHeader bool) error {
312361
conf, err := config.LoadConfig()
File renamed without changes.

cli/completions/asdf.elv

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Setup argument completions
2+
fn arg-completer {|@argz|
3+
set argz = $argz[1..-1] # strip 'asdf' and trailing empty string
4+
var num = (count $argz)
5+
if (== $num 0) {
6+
# list all subcommands
7+
find $asdf_dir'/lib/commands' -name 'command-*' | each {|cmd|
8+
put (re:replace '.*/command-(.*)\.bash' '${1}' $cmd)
9+
}
10+
put 'plugin'
11+
} else {
12+
if (match $argz 'current') {
13+
# asdf current <name>
14+
asdf plugin-list
15+
} elif (match $argz 'env') {
16+
# asdf env <command>
17+
ls-shims
18+
} elif (match $argz 'env' '.*') {
19+
# asdf env <command> [util]
20+
ls-executables
21+
} elif (match $argz 'exec') {
22+
# asdf exec <command>
23+
ls-shims
24+
} elif (match $argz 'global') {
25+
# asdf global <name>
26+
asdf plugin-list
27+
} elif (match $argz 'global' '.*') {
28+
# asdf global <name> <version>
29+
ls-installed-versions $argz[-1]
30+
} elif (match $argz 'install') {
31+
# asdf install <name>
32+
asdf plugin-list
33+
} elif (match $argz 'install' '.*') {
34+
# asdf install <name> <version>
35+
ls-all-versions $argz[-1]
36+
} elif (match $argz 'install' '.*' '.*') {
37+
# asdf install <name> <version> [--keep-download]
38+
put '--keep-download'
39+
} elif (match $argz 'latest') {
40+
# asdf latest <name>
41+
asdf plugin-list
42+
} elif (match $argz 'latest' '.*') {
43+
# asdf latest <name> [<version>]
44+
ls-all-versions $argz[-1]
45+
} elif (match $argz 'list-all') {
46+
# asdf list all <name>
47+
asdf plugin-list
48+
} elif (match $argz 'list-all' '.*') {
49+
# asdf list all <name> [<version>]
50+
ls-all-versions $argz[-1]
51+
} elif (match $argz 'list') {
52+
# asdf list <name>
53+
asdf plugin-list
54+
} elif (match $argz 'list' '.*') {
55+
# asdf list <name> [<version>]
56+
ls-installed-versions $argz[-1]
57+
} elif (match $argz 'local') {
58+
# asdf local <name> [-p|--parent]
59+
asdf plugin-list
60+
put '-p'
61+
put '--parent'
62+
} elif (match $argz 'local' '(-p|(--parent))') {
63+
# asdf local <name> [-p|--parent] <version>
64+
asdf plugin-list
65+
} elif (match $argz 'local' '.*') {
66+
# asdf local <name> [-p|--parent]
67+
# asdf local <name> <version>
68+
ls-installed-versions $argz[-1]
69+
put '-p'
70+
put '--parent'
71+
} elif (match $argz 'local' '(-p|(--parent))' '.*') {
72+
# asdf local [-p|--parent] <name> <version>
73+
ls-installed-versions $argz[-1]
74+
} elif (match $argz 'local' '.*' '(-p|(--parent))') {
75+
# asdf local <name> [-p|--parent] <version>
76+
ls-installed-versions $argz[-2]
77+
} elif (match $argz 'local' '.*' '.*') {
78+
# asdf local <name> <version> [-p|--parent]
79+
put '-p'
80+
put '--parent'
81+
} elif (or (match $argz 'plugin-add') (match $argz 'plugin' 'add')) {
82+
# asdf plugin add <name>
83+
asdf plugin-list-all | each {|line|
84+
put (re:replace '([^\s]+)\s+.*' '${1}' $line)
85+
}
86+
} elif (or (match $argz 'plugin-list') (match $argz 'plugin' 'list')) {
87+
# asdf plugin list
88+
put '--urls'
89+
put '--refs'
90+
put 'all'
91+
} elif (or (match $argz 'plugin-push') (match $argz 'plugin' 'push')) {
92+
# asdf plugin push <name>
93+
asdf plugin-list
94+
} elif (or (match $argz 'plugin-remove') (match $argz 'plugin' 'remove')) {
95+
# asdf plugin remove <name>
96+
asdf plugin-list
97+
} elif (and (>= (count $argz) 3) (match $argz[..3] 'plugin-test' '.*' '.*')) {
98+
# asdf plugin-test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]
99+
put '--asdf-plugin-gitref'
100+
put '--asdf-tool-version'
101+
ls-executables
102+
ls-shims
103+
} elif (and (>= (count $argz) 4) (match $argz[..4] 'plugin' 'test' '.*' '.*')) {
104+
# asdf plugin test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]
105+
put '--asdf-plugin-gitref'
106+
put '--asdf-tool-version'
107+
ls-executables
108+
ls-shims
109+
} elif (or (match $argz 'plugin-update') (match $argz 'plugin' 'update')) {
110+
# asdf plugin update <name>
111+
asdf plugin-list
112+
put '--all'
113+
} elif (match $argz 'plugin') {
114+
# list plugin-* subcommands
115+
find $asdf_dir'/lib/commands' -name 'command-plugin-*' | each {|cmd|
116+
put (re:replace '.*/command-plugin-(.*)\.bash' '${1}' $cmd)
117+
}
118+
} elif (match $argz 'reshim') {
119+
# asdf reshim <name>
120+
asdf plugin-list
121+
} elif (match $argz 'reshim' '.*') {
122+
# asdf reshim <name> <version>
123+
ls-installed-versions $argz[-1]
124+
} elif (match $argz 'shim-versions') {
125+
# asdf shim-versions <command>
126+
ls-shims
127+
} elif (match $argz 'uninstall') {
128+
# asdf uninstall <name>
129+
asdf plugin-list
130+
} elif (match $argz 'uninstall' '.*') {
131+
# asdf uninstall <name> <version>
132+
ls-installed-versions $argz[-1]
133+
} elif (match $argz 'update') {
134+
if (== $num 1) {
135+
# asdf update
136+
put '--head'
137+
}
138+
} elif (match $argz 'where') {
139+
# asdf where <name>
140+
asdf plugin-list
141+
} elif (match $argz 'where' '.*') {
142+
# asdf where <name> [<version>]
143+
ls-installed-versions $argz[-1]
144+
} elif (match $argz 'which') {
145+
ls-shims
146+
}
147+
}
148+
}
File renamed without changes.

0 commit comments

Comments
 (0)