From 15aa5adabf063b9cfe53d6a30039aea21d054af9 Mon Sep 17 00:00:00 2001 From: Adrian Orive Date: Tue, 3 Nov 2020 10:36:29 +0100 Subject: [PATCH] Provide a cli option to enable and disable completion command Signed-off-by: Adrian Orive --- cmd/completion.go | 72 ----------------- cmd/main.go | 2 +- docs/book/src/reference/completion.md | 2 +- pkg/cli/cli.go | 15 ++++ pkg/cli/cli_test.go | 17 ++++ pkg/cli/completion.go | 108 ++++++++++++++++++++++++++ 6 files changed, 142 insertions(+), 74 deletions(-) delete mode 100644 cmd/completion.go create mode 100644 pkg/cli/completion.go diff --git a/cmd/completion.go b/cmd/completion.go deleted file mode 100644 index ca671a75b09..00000000000 --- a/cmd/completion.go +++ /dev/null @@ -1,72 +0,0 @@ -/* -Copyright 2020 The Kubernetes 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 main - -import ( - "os" - - "github.com/spf13/cobra" -) - -func newCompletionCmd() *cobra.Command { - completionCmd := &cobra.Command{ - Use: "completion", - Long: `Output shell completion code for the specified shell (bash or zsh). -The shell code must be evaluated to provide interactive completion of kubebuilder commands. -This can be done by sourcing ~/.bash_profile or ~/.bashrc. -Detailed instructions on how to do this are available at docs/book/src/reference/completion.md -`, - Example: `To load all completions run: -$ . <(kubebuilder completion) -To configure your shell to load completions for each session add to your .bashrc: -$ echo -e "\n. <(kubebuilder completion)" >> ~/.bashrc -`, - } - completionCmd.AddCommand(newZshCmd()) - completionCmd.AddCommand(newBashCmd()) - return completionCmd -} - -func newBashCmd() *cobra.Command { - return &cobra.Command{ - Use: "bash", - Short: "Generate bash completions", - RunE: func(cmd *cobra.Command, cmdArgs []string) error { - return cmd.Root().GenBashCompletion(os.Stdout) - }, - Example: `To load completion run: -$ . <(kubebuilder completion bash) -To configure your bash shell to load completions for each session add to your bashrc: -$ echo -e "\n. <(kubebuilder completion bash)" >> ~/.bashrc -`, - } -} - -func newZshCmd() *cobra.Command { - return &cobra.Command{ - Use: "zsh", - Short: "Generate zsh completions", - RunE: func(cmd *cobra.Command, cmdArgs []string) error { - return cmd.Root().GenZshCompletion(os.Stdout) - }, - Example: `To load completion run: -$ . <(kubebuilder completion zsh) -To configure your zsh shell to load completions for each session add to your bashrc: -$ echo -e "\n. <(kubebuilder completion zsh)" >> ~/.bashrc -`, - } -} diff --git a/cmd/main.go b/cmd/main.go index 0839bcd2ba4..d49adcadc38 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -35,9 +35,9 @@ func main() { &pluginv2.Plugin{}, ), cli.WithExtraCommands( - newCompletionCmd(), version.NewCmd(), ), + cli.WithCompletion, ) if err != nil { log.Fatal(err) diff --git a/docs/book/src/reference/completion.md b/docs/book/src/reference/completion.md index 44e5ecdae98..a30635c7e0e 100644 --- a/docs/book/src/reference/completion.md +++ b/docs/book/src/reference/completion.md @@ -1,5 +1,5 @@ # Enabling shell autocompletion -The Kubebuilder completion script for Bash can be generated with the command `kubebuilder completion bash` as the Kubebuilder completion script for Zsh can be generated with the command `kubebuilder completion zsh`. +The Kubebuilder completion script can be generated with the command `kubebuilder completion [bash|zsh|powershell]`. Note that sourcing the completion script in your shell enables Kubebuilder autocompletion.