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

refs #225: add support for ZSH completion #233

Merged
merged 1 commit into from
Nov 26, 2018
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
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,33 @@ If you want to build it yourself later, you can change into the source directory

### Code completion

To load completion for bash env run
#### BASH

. <(hetzner-kube completion)
To load completion run

To configure your bash shell to load completions for each session add to your `.bashrc` file:
source <(hetzner-kube completion bash)

To configure your bash shell to load completions for each session add to your "~/.bashrc" file

# ~/.bashrc or ~/.profile
. <(hetzner-kube completion)
echo 'source <(hetzner-kube completion bash)\n' >> ~/.bashrc

Or you can add it to your `bash_completition.d` folder:

# On linux
hetzner-kube completion > /etc/bash_completion.d/hetzner-kube
hetzner-kube completion bash > /etc/bash_completion.d/hetzner-kube
# On OSX with completion installed via brew (`brew intall bash-completion`)
hetzner-kube completion > /usr/local/etc/bash_completion.d/hetzner-kube
hetzner-kube completion bash > /usr/local/etc/bash_completion.d/hetzner-kube

#### ZSH

To load completion run

source <(hetzner-kube completion zsh)

To configure your zsh shell to load completions for each session add to your "~/.zshrc" file

echo 'source <(hetzner-kube completion zsh)\n' >> ~/.zshrc

## Usage

Expand Down
38 changes: 30 additions & 8 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,51 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

var completionCmd = &cobra.Command{
Use: "completion",
Use: "completion <SHELL_TYPE>",
Short: "Generates bash completion scripts",
Long: `To load completion run
Long: `BASH:

. <(hetzner-kube completion)
To load completion run

To configure your bash shell to load completions for each session add to your bashrc
source <(hetzner-kube completion bash)

To configure your bash shell to load completions for each session add to your "~/.bashrc" file

# ~/.bashrc or ~/.profile
. <(hetzner-kube completion)
echo 'source <(hetzner-kube completion bash)\n' >> ~/.bashrc

Or you can add it to your bash_completition folder:

hetzner-kube completion > /usr/local/etc/bash_completion.d/hetzner-kube
hetzner-kube completion bash > /usr/local/etc/bash_completion.d/hetzner-kube

ZSH:

To load completion run

source <(hetzner-kube completion zsh)

To configure your zsh shell to load completions for each session add to your "~/.zshrc" file

echo 'source <(hetzner-kube completion zsh)\n' >> ~/.zshrc
`,
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenBashCompletion(os.Stdout)
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
switch strings.ToLower(args[0]) {
case "bash":
return rootCmd.GenBashCompletion(os.Stdout)
case "zsh":
return rootCmd.GenZshCompletion(os.Stdout)
default:
return fmt.Errorf("Unable to generate completition script for shell %q, please specify `bash` or `zsh`", args[0])
}
},
}

Expand Down