diff --git a/cmd/helm/completion.go b/cmd/helm/completion.go index db50b375a..275483f45 100644 --- a/cmd/helm/completion.go +++ b/cmd/helm/completion.go @@ -52,6 +52,25 @@ To load completions for every new session, execute once: $ helm completion zsh > "${fpath[1]}/_helm" ` +const fishCompDesc = ` +Generate the autocompletion script for Helm for the fish shell. + +To load completions in your current shell session: +$ helm completion fish | source + +To load completions for every new session, execute once: +$ helm completion fish > ~/.config/fish/completions/helm.fish + +You will need to start a new shell for this setup to take effect. +` + +const ( + noDescFlagName = "no-descriptions" + noDescFlagText = "disable completion descriptions" +) + +var disableCompDescriptions bool + func newCompletionCmd(out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "completion", @@ -85,7 +104,20 @@ func newCompletionCmd(out io.Writer) *cobra.Command { }, } - cmd.AddCommand(bash, zsh) + fish := &cobra.Command{ + Use: "fish", + Short: "generate autocompletions script for fish", + Long: fishCompDesc, + Args: require.NoArgs, + DisableFlagsInUseLine: true, + ValidArgsFunction: noCompletions, + RunE: func(cmd *cobra.Command, args []string) error { + return runCompletionFish(out, cmd) + }, + } + fish.Flags().BoolVar(&disableCompDescriptions, noDescFlagName, false, noDescFlagText) + + cmd.AddCommand(bash, zsh, fish) return cmd } @@ -257,6 +289,10 @@ __helm_bash_source <(__helm_convert_bash_to_zsh) return nil } +func runCompletionFish(out io.Writer, cmd *cobra.Command) error { + return cmd.Root().GenFishCompletion(out, !disableCompDescriptions) +} + // Function to disable file completion func noCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveNoFileComp diff --git a/cmd/helm/completion_test.go b/cmd/helm/completion_test.go index 44c74f85a..7eee39832 100644 --- a/cmd/helm/completion_test.go +++ b/cmd/helm/completion_test.go @@ -55,4 +55,5 @@ func TestCompletionFileCompletion(t *testing.T) { checkFileCompletion(t, "completion", false) checkFileCompletion(t, "completion bash", false) checkFileCompletion(t, "completion zsh", false) + checkFileCompletion(t, "completion fish", false) }