Skip to content

Commit 71f72fb

Browse files
committed
add shell completion code for fish shell
Signed-off-by: tiansuo114 <[email protected]>
1 parent 88c3e44 commit 71f72fb

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

pkg/karmadactl/completion/completion.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ const defaultBoilerPlate = `
4444

4545
var (
4646
completionLong = templates.LongDesc(i18n.T(`
47-
Output shell completion code for the specified shell (bash, zsh).
47+
Output shell completion code for the specified shell (bash, zsh, fish).
4848
The shell code must be evaluated to provide interactive
49-
completion of kubectl commands. This can be done by sourcing it from
49+
completion of %[1]s commands This can be done by sourcing it from
5050
the .bash_profile.
5151
5252
Note for zsh users: zsh completions are only supported in versions of zsh >= 5.2.`))
@@ -66,14 +66,19 @@ var (
6666
# Load the %[1]s completion code for zsh into the current shell
6767
source <(%[1]s completion zsh)
6868
# Set the %[1]s completion code for zsh to autoload on startup
69-
%[1]s completion zsh > "${fpath[1]}/%[1]s"`))
69+
%[1]s completion zsh > "${fpath[1]}/%[1]s"
70+
71+
# Load the %[1]s completion code for fish into the current shell
72+
%[1]s completion fish | source
73+
# To load completions for each session, execute once:
74+
%[1]s completion fish > ~/.config/fish/completions/%[1]s.fish`))
7075
)
7176

7277
var (
73-
// TODO: support output shell completion code for more specified shell, like `fish` and `powershell`.
7478
completionShells = map[string]func(out io.Writer, boilerPlate string, cmd *cobra.Command) error{
7579
"bash": runCompletionBash,
7680
"zsh": runCompletionZsh,
81+
"fish": runCompletionFish,
7782
}
7883
)
7984

@@ -87,8 +92,8 @@ func NewCmdCompletion(parentCommand string, out io.Writer, boilerPlate string) *
8792
cmd := &cobra.Command{
8893
Use: "completion SHELL",
8994
DisableFlagsInUseLine: true,
90-
Short: "Output shell completion code for the specified shell (bash, zsh)",
91-
Long: completionLong,
95+
Short: "Output shell completion code for the specified shell (bash, zsh, fish)",
96+
Long: fmt.Sprintf(completionLong, parentCommand),
9297
Example: fmt.Sprintf(completionExample, parentCommand),
9398
Run: func(cmd *cobra.Command, args []string) {
9499
cmdutil.CheckErr(RunCompletion(out, boilerPlate, cmd, args))
@@ -141,3 +146,13 @@ func runCompletionZsh(out io.Writer, boilerPlate string, cmd *cobra.Command) err
141146

142147
return cmd.GenZshCompletion(out)
143148
}
149+
150+
func runCompletionFish(out io.Writer, boilerPlate string, cmd *cobra.Command) error {
151+
if len(boilerPlate) == 0 {
152+
boilerPlate = defaultBoilerPlate
153+
}
154+
if _, err := out.Write([]byte(boilerPlate)); err != nil {
155+
return err
156+
}
157+
return cmd.GenFishCompletion(out, true)
158+
}

0 commit comments

Comments
 (0)