mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #50561 from cblecker/zsh-compinit
Automatic merge from submit-queue (batch tested with PRs 51471, 50561, 50435, 51473, 51436) Allow zsh completion to be autoloaded by compinit **What this PR does / why we need it**: Allows the kubectl zsh autocompletion to be auto loaded by compinit. Had to move the the boilerplate down into the specific shell functions as the compdef needs to be the first line in the definition file. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #50560 **Special notes for your reviewer**: **Release note**: ```release-note kubectl zsh autocompletion will work with compinit ```
This commit is contained in:
commit
b5c5b4a494
@ -50,44 +50,41 @@ 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 (
|
||||
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 +123,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\n"
|
||||
|
||||
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 +302,7 @@ BASH_COMPLETION_EOF
|
||||
}
|
||||
|
||||
__kubectl_bash_source <(__kubectl_convert_bash_to_zsh)
|
||||
_complete kubectl 2>/dev/null
|
||||
`
|
||||
out.Write([]byte(zsh_tail))
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user