Allow zsh completion to be autoloaded by compinit

This commit is contained in:
Christoph Blecker 2017-08-12 13:36:41 -07:00
parent 683f09c65e
commit 70b942d663
No known key found for this signature in database
GPG Key ID: B34A59A9D39F838B

View File

@ -87,7 +87,7 @@ source '$HOME/.kube/completion.bash.inc'
) )
var ( 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, "bash": runCompletionBash,
"zsh": runCompletionZsh, "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 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 { if len(boilerPlate) == 0 {
boilerPlate = defaultBoilerPlate boilerPlate = defaultBoilerPlate
} }
if _, err := out.Write([]byte(boilerPlate)); err != nil { if _, err := out.Write([]byte(boilerPlate)); err != nil {
return err return err
} }
return run(out, cmd.Parent())
}
func runCompletionBash(out io.Writer, kubectl *cobra.Command) error {
return kubectl.GenBashCompletion(out) 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 := ` zsh_initialization := `
__kubectl_bash_source() { __kubectl_bash_source() {
alias shopt=':' alias shopt=':'
@ -293,6 +305,7 @@ BASH_COMPLETION_EOF
} }
__kubectl_bash_source <(__kubectl_convert_bash_to_zsh) __kubectl_bash_source <(__kubectl_convert_bash_to_zsh)
_complete kubectl 2>/dev/null
` `
out.Write([]byte(zsh_tail)) out.Write([]byte(zsh_tail))
return nil return nil