Skip to content

Commit

Permalink
Add completion support for fish and powershell (#1929)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhtah committed Mar 27, 2024
1 parent f28ddc0 commit 5c152bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/cmd/kn_completion.md
Expand Up @@ -10,6 +10,8 @@ to provide interactive completion

Supported Shells:
- bash
- fish
- powershell
- zsh

```
Expand All @@ -23,6 +25,12 @@ kn completion SHELL
# Generate completion code for bash
source <(kn completion bash)
# Generate completion code for fish
kn completion fish | source
# Generate completion code for powershell
kn completion powershell | Out-String | Invoke-Expression
# Generate completion code for zsh
source <(kn completion zsh)
compdef _kn kn
Expand Down
18 changes: 15 additions & 3 deletions pkg/kn/commands/completion/completion.go
Expand Up @@ -30,11 +30,19 @@ to provide interactive completion
Supported Shells:
- bash
- fish
- powershell
- zsh`
eg = `
# Generate completion code for bash
source <(kn completion bash)
# Generate completion code for fish
kn completion fish | source
# Generate completion code for powershell
kn completion powershell | Out-String | Invoke-Expression
# Generate completion code for zsh
source <(kn completion zsh)
compdef _kn kn`
Expand All @@ -46,20 +54,24 @@ func NewCompletionCommand(p *commands.KnParams) *cobra.Command {
Use: "completion SHELL",
Short: "Output shell completion code",
Long: desc,
ValidArgs: []string{"bash", "zsh"},
ValidArgs: []string{"bash", "fish", "powershell", "zsh"},
Example: eg,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletion(os.Stdout)
case "fish":
return cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
return cmd.Root().GenPowerShellCompletion(os.Stdout)
case "zsh":
return cmd.Root().GenZshCompletion(os.Stdout)
default:
return errors.New("'bash' or 'zsh' shell completion is supported")
return errors.New("'bash', 'fish', 'powershell' or 'zsh' shell completion is supported")
}
} else {
return errors.New("Only one argument can be provided, either 'bash' or 'zsh'")
return errors.New("Only one argument can be provided, either 'bash', 'fish', 'powershell' or 'zsh'")
}
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kn/commands/completion/completion_test.go
Expand Up @@ -34,7 +34,7 @@ func TestCompletionUsage(t *testing.T) {
}

func TestCompletionGeneration(t *testing.T) {
for _, shell := range []string{"bash", "zsh"} {
for _, shell := range []string{"bash", "fish", "powershell", "zsh"} {
completionCmd := NewCompletionCommand(&commands.KnParams{})
c := test.CaptureOutput(t)
err := completionCmd.RunE(&cobra.Command{}, []string{shell})
Expand All @@ -48,11 +48,11 @@ func TestCompletionGeneration(t *testing.T) {
func TestCompletionNoArg(t *testing.T) {
completionCmd := NewCompletionCommand(&commands.KnParams{})
err := completionCmd.RunE(&cobra.Command{}, []string{})
assert.Assert(t, util.ContainsAll(err.Error(), "bash", "zsh", "one", "argument"))
assert.Assert(t, util.ContainsAll(err.Error(), "bash", "fish", "powershell", "zsh", "one", "argument"))
}

func TestCompletionWrongArg(t *testing.T) {
completionCmd := NewCompletionCommand(&commands.KnParams{})
err := completionCmd.RunE(&cobra.Command{}, []string{"sh"})
assert.Assert(t, util.ContainsAll(err.Error(), "bash", "zsh", "support"))
assert.Assert(t, util.ContainsAll(err.Error(), "bash", "fish", "powershell", "zsh", "support"))
}

0 comments on commit 5c152bd

Please sign in to comment.