Skip to content

Commit

Permalink
Add zsh completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Cornelius Weig committed Jan 24, 2019
1 parent c946082 commit 71c4639
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
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 {
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

0 comments on commit 71c4639

Please sign in to comment.