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..223b6dc3067 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,6 +27,7 @@ import ( func main() { c, err := cli.New( + cli.WithCommandName("kubebuilder"), cli.WithPlugins( &pluginv2.Plugin{}, &pluginv3.Plugin{}, @@ -35,9 +36,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..10b4cd2259d 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|fish|powershell]`. Note that sourcing the completion script in your shell enables Kubebuilder autocompletion.