From b0f4399d7e991c951264cd50a312a09b5d76bf1f Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Mon, 19 Sep 2022 09:33:00 -0400 Subject: [PATCH] Add support for completions in fish and powershell Fixes #3158 Note also that with Cobra 1.5, it is no longer necessary to have zsh >= 5.2 (see https://github.com/spf13/cobra/pull/1665) Signed-off-by: Marc Khouzam --- cli/core/pkg/command/completion.go | 32 +++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/cli/core/pkg/command/completion.go b/cli/core/pkg/command/completion.go index 90be65b16f7..ebe3deca2f6 100644 --- a/cli/core/pkg/command/completion.go +++ b/cli/core/pkg/command/completion.go @@ -20,6 +20,8 @@ var ( completionShells = []string{ "bash", "zsh", + "fish", + "powershell", } completionLongDesc = dedent.Dedent(` @@ -28,8 +30,7 @@ var ( The shell completion code must be evaluated to provide completion. See Examples for how to perform this for your given shell. - Note for bash users: make sure the bash-completions package has been installed. - Note for zsh users: zsh >= 5.2 is required for command completion.`) + Note for bash users: make sure the bash-completions package has been installed.`) completionExamples = dedent.Dedent(` # Bash instructions: @@ -45,9 +46,30 @@ var ( # Zsh instructions: + ## Load only for current session: + autoload -U compinit; compinit + source <(tanzu completion zsh) + compdef _tanzu tanzu + ## Load for all new sessions: echo "autoload -U compinit; compinit" >> ~/.zshrc - tanzu completion zsh > "${fpath[1]}/_tanzu"`) + tanzu completion zsh > "${fpath[1]}/_tanzu" + + # Fish instructions: + + ## Load only for current session: + tanzu completion fish | source + + ## Load for all new sessions: + tanzu completion fish > ~/.config/fish/completions/tanzu.fish + + # Powershell instructions: + + ## Load only for current session: + tanzu completion powershell | Out-String | Invoke-Expression + + ## Load for all new sessions: + Add the output of the above command to your powershell profile.`) ) // completionCmd represents the completion command @@ -78,6 +100,10 @@ func runCompletion(out io.Writer, cmd *cobra.Command, args []string) error { return cmd.Root().GenBashCompletion(out) case "zsh": return cmd.Root().GenZshCompletion(out) + case "fish": + return cmd.Root().GenFishCompletion(out, true) + case "powershell", "pwsh": + return cmd.Root().GenPowerShellCompletionWithDesc(out) default: return errors.New("unrecognized shell type specified") }