diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f5bd757f47..754e402dcb 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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%"] diff --git a/README.md b/README.md index 73db83d555..e2ca3b30b5 100644 --- a/README.md +++ b/README.md @@ -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 -completion revision service version - -$ kn revision -describe get +$ source <(kn completion bash) ``` +Use TAB to list available sub-commands or flags. + # Developers If you would like to contribute, please see diff --git a/pkg/kn/commands/completion.go b/pkg/kn/commands/completion.go deleted file mode 100644 index 0d64452cba..0000000000 --- a/pkg/kn/commands/completion.go +++ /dev/null @@ -1,32 +0,0 @@ -// 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 commands - -import ( - "os" - - "github.com/spf13/cobra" -) - -func NewCompletionCommand(p *KnParams) *cobra.Command { - return &cobra.Command{ - Use: "completion", - Short: "Output shell completion code (Bash)", - Hidden: true, // Don't show this in help listing - Run: func(cmd *cobra.Command, args []string) { - cmd.Root().GenBashCompletion(os.Stdout) - }, - } -} diff --git a/pkg/kn/commands/completion/completion.go b/pkg/kn/commands/completion/completion.go new file mode 100644 index 0000000000..6edcb0d7aa --- /dev/null +++ b/pkg/kn/commands/completion/completion.go @@ -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'") + } + }, + } +} diff --git a/pkg/kn/commands/completion_test.go b/pkg/kn/commands/completion/completion_test.go similarity index 51% rename from pkg/kn/commands/completion_test.go rename to pkg/kn/commands/completion/completion_test.go index 7aa3585d55..84fa791731 100644 --- a/pkg/kn/commands/completion_test.go +++ b/pkg/kn/commands/completion/completion_test.go @@ -12,11 +12,13 @@ // 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" ) @@ -24,11 +26,11 @@ import ( 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{} @@ -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") }) } diff --git a/pkg/kn/core/root.go b/pkg/kn/core/root.go index dba35dd9e7..c1e30e2422 100644 --- a/pkg/kn/core/root.go +++ b/pkg/kn/core/root.go @@ -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" @@ -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