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 completion for fish shell #9950

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions cmd/clusterctl/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const completionBoilerPlate = `# Copyright 2021 The Kubernetes Authors.

var (
completionLong = LongDesc(`
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
clusterctl commands. This can be done by sourcing it from the
.bash_profile.`)
Expand Down Expand Up @@ -77,12 +77,16 @@ var (
# To load completions for each session, execute once:
clusterctl completion zsh > "${fpath[1]}/_clusterctl"

Fish:
# To load completions in your current shell, execute the following command:
clusterctl completion fish | source

# You will need to start a new shell for this setup to take effect.`)

completionCmd = &cobra.Command{
Use: "completion [bash|zsh]",
Use: "completion [bash|zsh|fish]",
GroupID: groupOther,
Short: "Output shell completion code for the specified shell (bash or zsh)",
Short: "Output shell completion code for the specified shell (bash, zsh or fish)",
Long: LongDesc(completionLong),
Example: completionExample,
Args: func(cmd *cobra.Command, args []string) error {
Expand All @@ -100,6 +104,7 @@ var (
completionShells = map[string]func(out io.Writer, cmd *cobra.Command) error{
"bash": runCompletionBash,
"zsh": runCompletionZsh,
"fish": runCompletionFish,
}
)

Expand Down Expand Up @@ -131,6 +136,12 @@ func runCompletionBash(out io.Writer, cmd *cobra.Command) error {
return cmd.Root().GenBashCompletion(out)
}

func runCompletionFish(out io.Writer, cmd *cobra.Command) error {
fmt.Fprintf(out, "%s\n", completionBoilerPlate)

return cmd.Root().GenFishCompletion(out, true)
}

func runCompletionZsh(out io.Writer, cmd *cobra.Command) error {
var b bytes.Buffer

Expand Down