From 3f5176c26512fdb9154a0bd1d34ee61196874251 Mon Sep 17 00:00:00 2001 From: Wei Lun Date: Sat, 11 Jul 2020 22:07:05 +0800 Subject: [PATCH 1/2] add kubectl fish shell completion --- .../kubectl/pkg/cmd/completion/completion.go | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go b/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go index 6b357cd5110..482d4c5e45d 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go @@ -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) +} From 8a1987f0da1d6d881272f9609cadc03fa32c6f11 Mon Sep 17 00:00:00 2001 From: Wei Lun Date: Tue, 10 Aug 2021 16:31:32 +0800 Subject: [PATCH 2/2] fix indentation --- staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go b/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go index 482d4c5e45d..4d7e524a0e0 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go @@ -86,9 +86,9 @@ var ( # Load the kubectl completion code for fish[2] into the current shell - kubectl completion fish | source + kubectl completion fish | source # To load completions for each session, execute once: - kubectl completion fish > ~/.config/fish/completions/kubectl.fish`)) + kubectl completion fish > ~/.config/fish/completions/kubectl.fish`)) ) var (