add kubectl fish shell completion

This commit is contained in:
Wei Lun 2020-07-11 22:07:05 +08:00
parent 88512be213
commit 3f5176c265

View File

@ -45,7 +45,7 @@ const defaultBoilerPlate = `
var (
completionLong = templates.LongDesc(i18n.T(`
Output shell completion code for the specified shell (bash or zsh).
Output shell completion code for the specified shell (bash, zsh or fish).
The shell code must be evaluated to provide interactive
completion of kubectl commands. This can be done by sourcing it from
the .bash_profile.
@ -82,13 +82,20 @@ var (
# Load the kubectl completion code for zsh[1] into the current shell
source <(kubectl completion zsh)
# Set the kubectl completion code for zsh[1] to autoload on startup
kubectl completion zsh > "${fpath[1]}/_kubectl"`))
kubectl completion zsh > "${fpath[1]}/_kubectl"
# Load the kubectl completion code for fish[2] into the current shell
kubectl completion fish | source
# To load completions for each session, execute once:
kubectl completion fish > ~/.config/fish/completions/kubectl.fish`))
)
var (
completionShells = map[string]func(out io.Writer, boilerPlate string, cmd *cobra.Command) error{
"bash": runCompletionBash,
"zsh": runCompletionZsh,
"fish": runCompletionFish,
}
)
@ -102,7 +109,7 @@ func NewCmdCompletion(out io.Writer, boilerPlate string) *cobra.Command {
cmd := &cobra.Command{
Use: "completion SHELL",
DisableFlagsInUseLine: true,
Short: i18n.T("Output shell completion code for the specified shell (bash or zsh)"),
Short: i18n.T("Output shell completion code for the specified shell (bash, zsh or fish)"),
Long: completionLong,
Example: completionExample,
Run: func(cmd *cobra.Command, args []string) {
@ -269,3 +276,14 @@ __kubectl_bash_source <(__kubectl_convert_bash_to_zsh)
out.Write([]byte(zshTail))
return nil
}
func runCompletionFish(out io.Writer, boilerPlate string, kubectl *cobra.Command) error {
if len(boilerPlate) == 0 {
boilerPlate = defaultBoilerPlate
}
if _, err := out.Write([]byte(boilerPlate)); err != nil {
return err
}
return kubectl.GenFishCompletion(out, true)
}