Skip to content

Commit de4c194

Browse files
committed
feat: adding shell completions
1 parent b1a3793 commit de4c194

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

cmd/completion.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var completionCmd = &cobra.Command{
10+
Use: "completion [bash|zsh|fish]",
11+
Short: "Generate shell completion scripts",
12+
Long: `Generate shell completion scripts for Kernel CLI.
13+
14+
To load completions:
15+
16+
Bash:
17+
$ source <(kernel completion bash)
18+
19+
# To load completions for each session, execute once:
20+
# Linux:
21+
$ kernel completion bash > /etc/bash_completion.d/kernel
22+
# macOS:
23+
$ kernel completion bash > $(brew --prefix)/etc/bash_completion.d/kernel
24+
25+
Zsh:
26+
# If shell completion is not already enabled in your environment,
27+
# you will need to enable it. You can execute the following once:
28+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
29+
30+
# To load completions for each session, execute once:
31+
$ kernel completion zsh > "${fpath[1]}/_kernel"
32+
33+
# You will need to start a new shell for this setup to take effect.
34+
35+
Fish:
36+
$ kernel completion fish | source
37+
38+
# To load completions for each session, execute once:
39+
$ kernel completion fish > ~/.config/fish/completions/kernel.fish
40+
`,
41+
DisableFlagsInUseLine: true,
42+
ValidArgs: []string{"bash", "zsh", "fish"},
43+
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
44+
RunE: func(cmd *cobra.Command, args []string) error {
45+
switch args[0] {
46+
case "bash":
47+
return cmd.Root().GenBashCompletion(os.Stdout)
48+
case "zsh":
49+
return cmd.Root().GenZshCompletion(os.Stdout)
50+
case "fish":
51+
return cmd.Root().GenFishCompletion(os.Stdout, true)
52+
}
53+
return nil
54+
},
55+
}
56+
57+
func init() {
58+
rootCmd.AddCommand(completionCmd)
59+
}

0 commit comments

Comments
 (0)