Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kubectl fish shell completion #92989

Merged
merged 2 commits into from
Aug 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
)

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}