-
Notifications
You must be signed in to change notification settings - Fork 469
/
completioncmd.go
65 lines (58 loc) · 1.61 KB
/
completioncmd.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
64
65
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
)
type completionCmdConfig struct {
Custom bool `mapstructure:"custom"`
}
func (c *Config) newCompletionCmd() *cobra.Command {
completionCmd := &cobra.Command{
Use: "completion shell",
Short: "Generate shell completion code",
Args: cobra.ExactArgs(1),
ValidArgs: []string{"bash", "fish", "powershell", "zsh"},
Long: mustLongHelp("completion"),
Example: example("completion"),
RunE: c.runCompletionCmd,
Annotations: map[string]string{
doesNotRequireValidConfig: "true",
},
}
return completionCmd
}
func (c *Config) runCompletionCmd(cmd *cobra.Command, args []string) error {
builder := strings.Builder{}
builder.Grow(16384)
var output string
switch args[0] {
case "bash":
includeDesc := true
if err := cmd.Root().GenBashCompletionV2(&builder, includeDesc); err != nil {
return err
}
output = builder.String()
case "fish":
includeDesc := true
if err := cmd.Root().GenFishCompletion(&builder, includeDesc); err != nil {
return err
}
output = builder.String()
case "powershell":
if err := cmd.Root().GenPowerShellCompletion(&builder); err != nil {
return err
}
output = builder.String()
case "zsh":
if err := cmd.Root().GenZshCompletion(&builder); err != nil {
return err
}
// FIXME remove the following when
// https://github.com/spf13/cobra/pull/1690 is merged and released
output = "#compdef chezmoi\n" + strings.TrimPrefix(builder.String(), "#compdef _chezmoi chezmoi\n")
default:
return fmt.Errorf("%s: unsupported shell", args[0])
}
return c.writeOutputString(output)
}