From 70b942d6638e99484fe9eb11e878d7300dd8d9ae Mon Sep 17 00:00:00 2001 From: Christoph Blecker Date: Sat, 12 Aug 2017 13:36:41 -0700 Subject: [PATCH 1/2] Allow zsh completion to be autoloaded by compinit --- pkg/kubectl/cmd/completion.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/pkg/kubectl/cmd/completion.go b/pkg/kubectl/cmd/completion.go index 20414218c72..10b773c5e93 100644 --- a/pkg/kubectl/cmd/completion.go +++ b/pkg/kubectl/cmd/completion.go @@ -87,7 +87,7 @@ source '$HOME/.kube/completion.bash.inc' ) var ( - completion_shells = map[string]func(out io.Writer, cmd *cobra.Command) error{ + completion_shells = map[string]func(out io.Writer, boilerPlate string, cmd *cobra.Command) error{ "bash": runCompletionBash, "zsh": runCompletionZsh, } @@ -126,20 +126,32 @@ func RunCompletion(out io.Writer, boilerPlate string, cmd *cobra.Command, args [ return cmdutil.UsageErrorf(cmd, "Unsupported shell type %q.", args[0]) } + return run(out, boilerPlate, cmd.Parent()) +} + +func runCompletionBash(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 run(out, cmd.Parent()) -} -func runCompletionBash(out io.Writer, kubectl *cobra.Command) error { return kubectl.GenBashCompletion(out) } -func runCompletionZsh(out io.Writer, kubectl *cobra.Command) error { +func runCompletionZsh(out io.Writer, boilerPlate string, kubectl *cobra.Command) error { + zsh_head := `#compdef kubectl +` + out.Write([]byte(zsh_head)) + + if len(boilerPlate) == 0 { + boilerPlate = defaultBoilerPlate + } + if _, err := out.Write([]byte(boilerPlate)); err != nil { + return err + } + zsh_initialization := ` __kubectl_bash_source() { alias shopt=':' @@ -293,6 +305,7 @@ BASH_COMPLETION_EOF } __kubectl_bash_source <(__kubectl_convert_bash_to_zsh) +_complete kubectl 2>/dev/null ` out.Write([]byte(zsh_tail)) return nil From 0153f8de1a17668cb8e0a38b02c81d77711a8285 Mon Sep 17 00:00:00 2001 From: Christoph Blecker Date: Mon, 14 Aug 2017 11:24:33 -0700 Subject: [PATCH 2/2] Update help/example for kubectl completion --- pkg/kubectl/cmd/completion.go | 53 +++++++++++++++++------------------ 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/pkg/kubectl/cmd/completion.go b/pkg/kubectl/cmd/completion.go index 10b773c5e93..d14ca66329b 100644 --- a/pkg/kubectl/cmd/completion.go +++ b/pkg/kubectl/cmd/completion.go @@ -50,40 +50,37 @@ var ( completion of kubectl commands. This can be done by sourcing it from the .bash_profile. - Note: this requires the bash-completion framework, which is not installed - by default on Mac. This can be installed by using homebrew: - - $ brew install bash-completion - - Once installed, bash_completion must be evaluated. This can be done by adding the - following line to the .bash_profile - - $ source $(brew --prefix)/etc/bash_completion + Detailed instructions on how to do this are available here: + https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2`)) completion_example = templates.Examples(i18n.T(` - # Install bash completion on a Mac using homebrew - brew install bash-completion - printf " -# Bash completion support -source $(brew --prefix)/etc/bash_completion -" >> $HOME/.bash_profile - source $HOME/.bash_profile + # Installing bash completion on macOS using homebrew + ## If running Bash 3.2 included with macOS + brew install bash-completion + ## or, if running Bash 4.1+ + brew install bash-completion@2 + ## If kubectl is installed via homebrew, this should start working immediately. + ## If you've installed via other means, you may need add the completion to your completion directory + kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl - # Load the kubectl completion code for bash into the current shell - source <(kubectl completion bash) - # Write bash completion code to a file and source if from .bash_profile - kubectl completion bash > ~/.kube/completion.bash.inc - printf " -# Kubectl shell completion -source '$HOME/.kube/completion.bash.inc' -" >> $HOME/.bash_profile - source $HOME/.bash_profile + # Installing bash completion on Linux + ## Load the kubectl completion code for bash into the current shell + source <(kubectl completion bash) + ## Write bash completion code to a file and source if from .bash_profile + kubectl completion bash > ~/.kube/completion.bash.inc + printf " + # Kubectl shell completion + source '$HOME/.kube/completion.bash.inc' + " >> $HOME/.bash_profile + source $HOME/.bash_profile # Load the kubectl completion code for zsh[1] into the current shell - source <(kubectl completion zsh)`)) + source <(kubectl completion zsh) + # Set the kubectl completion code for zsh[1] to autoload on startup + kubectl completion zsh > "${fpath[1]}/_kubectl"`)) ) var ( @@ -141,8 +138,8 @@ func runCompletionBash(out io.Writer, boilerPlate string, kubectl *cobra.Command } func runCompletionZsh(out io.Writer, boilerPlate string, kubectl *cobra.Command) error { - zsh_head := `#compdef kubectl -` + zsh_head := "#compdef kubectl\n" + out.Write([]byte(zsh_head)) if len(boilerPlate) == 0 {