Skip to content

Commit

Permalink
Adds zsh completion (#476)
Browse files Browse the repository at this point in the history
* Adds zsh completion

 - Adds back zsh completion after vendoring cobra library with a fix
 - Updates the command definition to accept argument either of ['bash', 'zsh']
 - Moves the completion command definition under `pkg/kn/commands/completion`
 - Updates docs
 - Updates CHANGELOG.adoc

* fix(golint): exported function should have comment

* Fixes PR number in CHANGELOG.adoc

* Updates unit tests
  • Loading branch information
navidshaikh authored and knative-prow-robot committed Nov 5, 2019
1 parent 7ffa49f commit 44dafbd
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 51 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
|===
////

## v0.10.0 (2019-11-05)

[cols="1,10,3", options="header", width="100%"]
|===
| | Description | PR

| 🧽
| Update to Knative serving dependency to 0.10.0
| https://github.com/knative/client/pull/474[#474]

| 🎁
| Adds zsh completion
| https://github.com/knative/client/pull/476[#476]

|===

## v0.9.0 (2019-10-29)

[cols="1,10,3", options="header", width="100%"]
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@ Start with the [user's guide](docs/README.md) to learn more. You can read about
* [User's guide](docs/README.md)
* [Generated documentation](docs/cmd/kn.md)

**Bash auto completion:**
**Shell auto completion:**

Run the following command to enable BASH auto-completion:
Run the following command to enable shell auto-completion:

For Zsh:
```sh
$ source <(kn completion)
$ source <(kn completion zsh)
```

Use TAB to list available sub-commands:

For Bash:
```sh
$ kn <TAB>
completion revision service version

$ kn revision <TAB>
describe get
$ source <(kn completion bash)
```

Use TAB to list available sub-commands or flags.

# Developers

If you would like to contribute, please see
Expand Down
32 changes: 0 additions & 32 deletions pkg/kn/commands/completion.go

This file was deleted.

66 changes: 66 additions & 0 deletions pkg/kn/commands/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package completion

import (
"fmt"
"os"

"knative.dev/client/pkg/kn/commands"

"github.com/spf13/cobra"
)

const (
desc = `
This command prints shell completion code which need to be evaluated
to provide interactive completion
Supported Shells:
- bash
- zsh`
eg = `
# Generate completion code for bash
source <(kn completion bash)
# Generate completion code for zsh
source <(kn completion zsh)`
)

// NewCompletionCommand implements shell auto-completion feature for Bash and Zsh
func NewCompletionCommand(p *commands.KnParams) *cobra.Command {
return &cobra.Command{
Use: "completion [SHELL]",
Short: "Output shell completion code",
Long: desc,
ValidArgs: []string{"bash", "zsh"},
Example: eg,
Hidden: true, // Don't show this in help listing
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
default:
fmt.Println("only supports 'bash' or 'zsh' shell completion")
}
} else {
fmt.Println("accepts one argument either 'bash' or 'zsh'")
}
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package commands
package completion

import (
"testing"

"knative.dev/client/pkg/kn/commands"

"github.com/spf13/cobra"
"gotest.tools/assert"
)

func TestCompletion(t *testing.T) {
var (
fakeRootCmd, completionCmd *cobra.Command
knParams *KnParams
knParams *commands.KnParams
)

setup := func() {
knParams = &KnParams{}
knParams = &commands.KnParams{}
completionCmd = NewCompletionCommand(knParams)

fakeRootCmd = &cobra.Command{}
Expand All @@ -37,17 +39,44 @@ func TestCompletion(t *testing.T) {

t.Run("creates a CompletionCommand", func(t *testing.T) {
setup()
assert.Equal(t, completionCmd.Use, "completion")
assert.Equal(t, completionCmd.Short, "Output shell completion code (Bash)")
assert.Equal(t, completionCmd.Use, "completion [SHELL]")
assert.Equal(t, completionCmd.Short, "Output shell completion code")
assert.Assert(t, completionCmd.RunE == nil)
})

t.Run("returns completion code for BASH", func(t *testing.T) {
setup()
CaptureStdout(t)
defer ReleaseStdout(t)
commands.CaptureStdout(t)
defer commands.ReleaseStdout(t)

completionCmd.Run(fakeRootCmd, []string{"bash"})
assert.Assert(t, commands.ReadStdout(t) != "")
})

t.Run("returns completion code for Zsh", func(t *testing.T) {
setup()
commands.CaptureStdout(t)
defer commands.ReleaseStdout(t)

completionCmd.Run(fakeRootCmd, []string{"zsh"})
assert.Assert(t, commands.ReadStdout(t) != "")
})

t.Run("returns error on command without args", func(t *testing.T) {
setup()
commands.CaptureStdout(t)
defer commands.ReleaseStdout(t)

completionCmd.Run(fakeRootCmd, []string{})
assert.Assert(t, ReadStdout(t) != "")
assert.Assert(t, commands.ReadStdout(t) == "accepts one argument either 'bash' or 'zsh'\n")
})

t.Run("returns error on command with invalid args", func(t *testing.T) {
setup()
commands.CaptureStdout(t)
defer commands.ReleaseStdout(t)

completionCmd.Run(fakeRootCmd, []string{"sh"})
assert.Assert(t, commands.ReadStdout(t) == "only supports 'bash' or 'zsh' shell completion\n")
})
}
3 changes: 2 additions & 1 deletion pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/completion"
"knative.dev/client/pkg/kn/commands/plugin"
"knative.dev/client/pkg/kn/commands/revision"
"knative.dev/client/pkg/kn/commands/route"
Expand Down Expand Up @@ -139,7 +140,7 @@ func NewKnCommand(params ...commands.KnParams) *cobra.Command {
rootCmd.AddCommand(revision.NewRevisionCommand(p))
rootCmd.AddCommand(plugin.NewPluginCommand(p))
rootCmd.AddCommand(route.NewRouteCommand(p))
rootCmd.AddCommand(commands.NewCompletionCommand(p))
rootCmd.AddCommand(completion.NewCompletionCommand(p))
rootCmd.AddCommand(version.NewVersionCommand(p))

// Deal with empty and unknown sub command groups
Expand Down

0 comments on commit 44dafbd

Please sign in to comment.