From cb657f63a280b92901cd534a51e086262cfc6398 Mon Sep 17 00:00:00 2001 From: Wei Lun Date: Sat, 11 Jul 2020 22:07:05 +0800 Subject: [PATCH] add kubectl fish shell completion Kubernetes-commit: 3f5176c26512fdb9154a0bd1d34ee61196874251 --- pkg/cmd/completion/completion.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/completion/completion.go b/pkg/cmd/completion/completion.go index 6f89b0d0f..cfe1b4bb3 100644 --- a/pkg/cmd/completion/completion.go +++ b/pkg/cmd/completion/completion.go @@ -44,7 +44,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. @@ -89,13 +89,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, } ) @@ -109,7 +116,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) { @@ -161,3 +168,14 @@ func runCompletionZsh(out io.Writer, boilerPlate string, kubectl *cobra.Command) return kubectl.GenZshCompletion(out) } + +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) +}