-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b6c9b7
commit 156c9b3
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
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 | ||
`, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 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`. | ||
Note that sourcing the completion script in your shell enables Kubebuilder autocompletion. | ||
|
||
<aside class="note"> | ||
<h1>Prerequisites for Bash</h1> | ||
|
||
The completion Bash script depends on [bash-completion](https://github.com/scop/bash-completion), which means that you have to install this software first (you can test if you have bash-completion already installed). Also, ensure that your Bash version is 4.1+. | ||
|
||
</aside> | ||
|
||
|
||
- Once installed, go ahead and add the path `/usr/local/bin/bash` in the `/etc/shells`. | ||
|
||
`echo “/usr/local/bin/bash” > /etc/shells` | ||
|
||
- Make sure to use installed shell by current user. | ||
|
||
`chsh -s /usr/local/bin/bash` | ||
|
||
- Add following content in /.bash_profile or ~/.bashrc | ||
|
||
``` | ||
# kubebuilder autocompletion | ||
if [ -f /usr/local/share/bash-completion/bash_completion ]; then | ||
. /usr/local/share/bash-completion/bash_completion | ||
fi | ||
. <(kubebuilder completion) | ||
``` | ||
- Restart terminal for the changes to be reflected. | ||
|
||
<aside class="note"> | ||
<h1>Zsh</h1> | ||
Follow a similar protocol for `zsh` completion. | ||
</aside> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters