Merge pull request #92989 from WLun001/fish-completion

add kubectl fish shell completion
This commit is contained in:
Kubernetes Prow Robot 2021-08-25 12:27:55 -07:00 committed by GitHub
commit cd80d70c3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}