Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bash/zsh completion command #1437

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2020 The Kubernetes Authors.
bharathi-tenneti marked this conversation as resolved.
Show resolved Hide resolved
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)
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we need to add the Examples:

Example: `# Run the command to generate the bash completion and create the file
$kubebuilder completion bash >> ~/.kubebuilder

TODO: Add all the steps required to set up it. 


"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ kubebuilder completion bash >> ~/.bashrc

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
},
},
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
`,

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)
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
},
},
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
`,

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
`,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdyt about add the examples here in the commands?

}
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func buildCmdTree() *cobra.Command {
// kubebuilder version
rootCmd.AddCommand(version.NewVersionCmd())

// kubebuilder completion
rootCmd.AddCommand(newCompletionCmd())
bharathi-tenneti marked this conversation as resolved.
Show resolved Hide resolved

return rootCmd
}

Expand Down
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- [RBAC](./reference/markers/rbac.md)

- [controller-gen CLI](./reference/controller-gen.md)
- [completion](./reference/completion.md)
- [Artifacts](./reference/artifacts.md)
- [Writing controller tests](./reference/writing-tests.md)

Expand Down
8 changes: 8 additions & 0 deletions docs/book/src/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export PATH=$PATH:/usr/local/kubebuilder/bin

Also, you can install a master snapshot from `https://go.kubebuilder.io/dl/latest/${os}/${arch}`.


</aside>

<aside class="note">
<h1>Enabling shell autocompletion</h1>

Kubebuilder provides autocompletion support for Bash and Zsh via the command `kubebuilder completion <bash|zsh>`, which can save you a lot of typing. For further information see the [completion](./reference/completion.md) document.

bharathi-tenneti marked this conversation as resolved.
Show resolved Hide resolved
</aside>

## Create a Project
Expand Down
35 changes: 35 additions & 0 deletions docs/book/src/reference/completion.md
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.

Copy link
Member

@camilamacedo86 camilamacedo86 Mar 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it should be :

Bash on Linux

//Add the steps

Bash on Mac

// Add the steps

Zsh

// Add the steps

Copy link
Contributor

@estroz estroz Mar 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid discussing prereq installation steps on different systems, since "linux" has various distros (read: package managers). Perhaps a link to each package would help.

Listing the prerequisite packages and versions delegates installation (which is relatively simple in this case) to the user. By doing so we also avoid doc drift in case prereqs change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@estroz @camilamacedo86
As I could test out on macOS, I can list out steps for the same.
Any idea on how do folks go about for other distributions?
Can I leave a TODO note for others such as ""linux", and "zsh"?

Copy link
Member

@camilamacedo86 camilamacedo86 Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that I am not sure if the same steps will work for both (Linux or Mac).

Regards the pre-requirement for bash it is the same for kubectl and for both (Linux or Mac) its docs is addressing the same repo. There has the info over how to install in each SO.

So, IMO we can move forward with:

Enabling shell autocompletion

(introduction)

Bash

(pre-requirement note)

(steps)

Then, we can improve it in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camilamacedo86
So, no changes for now. Did I understand correctly?


- 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Follow a similar protocol for `zsh` completion.
</aside>
<aside class="note">
<h1>Zsh</h1>
Follow a similar protocol for `zsh` completion.
</aside>

</aside>
1 change: 1 addition & 0 deletions docs/book/src/reference/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [RBAC](markers/rbac.md)

- [controller-gen CLI](controller-gen.md)
- [completion](completion.md)
- [Artifacts](artifacts.md)
- [Writing controller tests](writing-tests.md)
- [Metrics](metrics.md)