forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.go
63 lines (53 loc) · 1.38 KB
/
completion.go
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
63
package cli
import (
"embed"
"fmt"
"sort"
)
const (
completionCommandName = "generate-completion"
)
var (
//go:embed autocomplete
autoCompleteFS embed.FS
shellCompletions = map[string]renderCompletion{
"bash": getCompletion("autocomplete/bash_autocomplete"),
"ps": getCompletion("autocomplete/powershell_autocomplete.ps1"),
"zsh": getCompletion("autocomplete/zsh_autocomplete"),
"fish": func(a *App) (string, error) {
return a.ToFishCompletion()
},
}
)
type renderCompletion func(a *App) (string, error)
func getCompletion(s string) renderCompletion {
return func(a *App) (string, error) {
b, err := autoCompleteFS.ReadFile(s)
return string(b), err
}
}
var completionCommand = &Command{
Name: completionCommandName,
Hidden: true,
Action: func(ctx *Context) error {
var shells []string
for k := range shellCompletions {
shells = append(shells, k)
}
sort.Strings(shells)
if ctx.Args().Len() == 0 {
return Exit(fmt.Sprintf("no shell provided for completion command. available shells are %+v", shells), 1)
}
s := ctx.Args().First()
if rc, ok := shellCompletions[s]; !ok {
return Exit(fmt.Sprintf("unknown shell %s, available shells are %+v", s, shells), 1)
} else if c, err := rc(ctx.App); err != nil {
return Exit(err, 1)
} else {
if _, err = ctx.App.Writer.Write([]byte(c)); err != nil {
return Exit(err, 1)
}
}
return nil
},
}