Skip to content

Commit 5325966

Browse files
Merge pull request #82 from ksctl/feature/78
feat(completion): add shell completion support
2 parents 8546f4b + 5c24ed7 commit 5325966

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

cmd/completion.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (k *KsctlCommand) ShellCompletion() *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "completion [bash|zsh|fish]",
13+
Short: "Generate shell completion scripts",
14+
Long: `To load completions:
15+
16+
Bash:
17+
18+
$ source <(ksctl completion bash)
19+
20+
# To load completions for each session, execute once:
21+
# Linux:
22+
$ ksctl completion bash > /etc/bash_completion.d/ksctl
23+
# macOS:
24+
$ ksctl completion bash > /usr/local/etc/bash_completion.d/ksctl
25+
26+
Zsh:
27+
28+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
29+
$ ksctl completion zsh > "${fpath[1]}/_ksctl"
30+
31+
Fish:
32+
33+
$ ksctl completion fish | source
34+
35+
# To load completions for each session, execute once:
36+
$ ksctl completion fish > ~/.config/fish/completions/ksctl.fish
37+
`,
38+
Args: cobra.ExactArgs(1),
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
switch args[0] {
41+
case "bash":
42+
return cmd.Root().GenBashCompletion(os.Stdout)
43+
case "zsh":
44+
return cmd.Root().GenZshCompletion(os.Stdout)
45+
case "fish":
46+
return cmd.Root().GenFishCompletion(os.Stdout, true)
47+
default:
48+
return fmt.Errorf("unsupported shell: %s", args[0])
49+
}
50+
},
51+
}
52+
53+
return cmd
54+
}

cmd/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (k *KsctlCommand) CommandMapping() error {
2626
c,
2727
k.Version(),
2828
k.SelfUpdate(),
29+
k.ShellCompletion(),
2930
cr,
3031
)
3132
cli.RegisterCommand(

0 commit comments

Comments
 (0)