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 zsh completion #1531

Merged
merged 1 commit into from
Jan 26, 2019
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
46 changes: 31 additions & 15 deletions cmd/skaffold/app/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,46 @@ import (
"github.com/spf13/cobra"
)

// completionCmd represents the completion command
const longDescription = `
Outputs skaffold shell completion for the given shell (bash or zsh)

This depends on the bash-completion binary. Example installation instructions:
OS X:
$ brew install bash-completion
$ source $(brew --prefix)/etc/bash_completion
$ skaffold completion bash > ~/.skaffold-completion # for bash users
$ skaffold completion zsh > ~/.skaffold-completion # for zsh users
$ source ~/.skaffold-completion
Ubuntu:
$ apt-get install bash-completion
$ source /etc/bash-completion
$ source <(skaffold completion bash) # for bash users
$ source <(skaffold completion zsh) # for zsh users

Additionally, you may want to output the completion to a file and source in your .bashrc
`

var completionCmd = &cobra.Command{
// Only bash is supported for now. However, having args after
// "completion" will help when supporting multiple shells
Use: "completion bash",
Use: "completion SHELL",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could it default to bash to not break existing usage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, skaffold completion by itself was never valid but would print the help text. Only skaffold completion bash was allowed and that didn't change.

I also checked kubectl and minikube and neither defaults to bash. So I think the current behavior is most consistent and should not be changed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make sense

return fmt.Errorf("requires 1 arg, found %d", len(args))
}
return cobra.OnlyValidArgs(cmd, args)
},
ValidArgs: []string{"bash"},
Short: "Output command completion script for the bash shell",
Long: `To enable command completion run

eval "$(skaffold completion bash)"

To configure bash shell completion for all your sessions, add the following to your
~/.bashrc or ~/.bash_profile:
ValidArgs: []string{"bash", "zsh"},
Short: "Output skaffold shell completion for the given shell (bash or zsh)",
Long: longDescription,
Run: completion,
}

eval "$(skaffold completion bash)"`,
Run: func(cmd *cobra.Command, args []string) {
func completion(_cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
rootCmd.GenBashCompletion(os.Stdout)
},
case "zsh":
rootCmd.GenZshCompletion(os.Stdout)
}
}

// NewCmdCompletion returns the cobra command that outputs shell completion code
Expand Down
4 changes: 2 additions & 2 deletions docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ Env vars:

### skaffold completion

Output command completion script for the bash shell
Output skaffold shell completion for the given shell (bash or zsh)

```
Usage:
skaffold completion bash [flags]
skaffold completion SHELL [flags]

Global Flags:
-v, --verbosity string Log level (debug, info, warn, error, fatal, panic) (default "warning")
Expand Down