@@ -44,9 +44,9 @@ const defaultBoilerPlate = `
44
44
45
45
var (
46
46
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 ).
48
48
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
50
50
the .bash_profile.
51
51
52
52
Note for zsh users: zsh completions are only supported in versions of zsh >= 5.2.` ))
@@ -66,14 +66,19 @@ var (
66
66
# Load the %[1]s completion code for zsh into the current shell
67
67
source <(%[1]s completion zsh)
68
68
# 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` ))
70
75
)
71
76
72
77
var (
73
- // TODO: support output shell completion code for more specified shell, like `fish` and `powershell`.
74
78
completionShells = map [string ]func (out io.Writer , boilerPlate string , cmd * cobra.Command ) error {
75
79
"bash" : runCompletionBash ,
76
80
"zsh" : runCompletionZsh ,
81
+ "fish" : runCompletionFish ,
77
82
}
78
83
)
79
84
@@ -87,8 +92,8 @@ func NewCmdCompletion(parentCommand string, out io.Writer, boilerPlate string) *
87
92
cmd := & cobra.Command {
88
93
Use : "completion SHELL" ,
89
94
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 ) ,
92
97
Example : fmt .Sprintf (completionExample , parentCommand ),
93
98
Run : func (cmd * cobra.Command , args []string ) {
94
99
cmdutil .CheckErr (RunCompletion (out , boilerPlate , cmd , args ))
@@ -141,3 +146,13 @@ func runCompletionZsh(out io.Writer, boilerPlate string, cmd *cobra.Command) err
141
146
142
147
return cmd .GenZshCompletion (out )
143
148
}
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