Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Add support for completions in fish and powershell
Browse files Browse the repository at this point in the history
Fixes #3158

Note also that with Cobra 1.5, it is no longer necessary
to have zsh >= 5.2 (see spf13/cobra#1665)

Signed-off-by: Marc Khouzam <kmarc@vmware.com>
  • Loading branch information
marckhouzam authored and Guo XiaoShuang committed Oct 31, 2022
1 parent e3e1511 commit dafca78
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
32 changes: 29 additions & 3 deletions cli/core/pkg/command/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
completionShells = []string{
"bash",
"zsh",
"fish",
"powershell",
}

completionLongDesc = dedent.Dedent(`
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down
34 changes: 32 additions & 2 deletions cli/core/pkg/command/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Test_runCompletion_Bash(t *testing.T) {
// Check for a snippet of the bash completion output
// TODO make this test less brittle
if !strings.Contains(out.String(), "if [[ -z \"${BASH_VERSION:-}\" || \"${BASH_VERSINFO[0]:-}\" -gt 3 ]]; then") {
t.Errorf("Unexpected error returned for invalid shell argument: %s", out.String())
t.Errorf("Unexpected output for the bash shell script: %s", out.String())
}
}

Expand All @@ -90,6 +90,36 @@ func Test_runCompletion_Zsh(t *testing.T) {

// Check for a snippet of the zsh completion output
if !strings.Contains(out.String(), "# For zsh, when completing a flag with an = (e.g., completion -n=<TAB>)") {
t.Errorf("Unexpected error returned for invalid shell argument: %s", out.String())
t.Errorf("Unexpected output for the zsh shell script: %s", out.String())
}
}

// Test_runCompletion_Fish validates functionality for fish shell completion.
func Test_runCompletion_Fish(t *testing.T) {
var out bytes.Buffer
args := []string{"fish"}
err := runCompletion(&out, completionCmd, args)
if err != nil {
t.Errorf("Unexpected error for valid shell: %v", err)
}

// Check for a snippet of the fish completion output
if !strings.Contains(out.String(), "# For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)") {
t.Errorf("Unexpected output for the fish shell script: %s", out.String())
}
}

// Test_runCompletion_Pwsh validates functionality for powershell completion.
func Test_runCompletion_Pwsh(t *testing.T) {
var out bytes.Buffer
args := []string{"powershell"}
err := runCompletion(&out, completionCmd, args)
if err != nil {
t.Errorf("Unexpected error for valid shell: %v", err)
}

// Check for a snippet of the powershell completion output
if !strings.Contains(out.String(), "# PowerShell supports three different completion modes") {
t.Errorf("Unexpected output for the powershell script: %s", out.String())
}
}

0 comments on commit dafca78

Please sign in to comment.