Skip to content

Commit

Permalink
Add completion
Browse files Browse the repository at this point in the history
Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
  • Loading branch information
KeisukeYamashita committed Nov 23, 2020
1 parent f4391f6 commit 00a71b6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

func newCompletionCmd() *cobra.Command {
return &cobra.Command{
Use: "completion [bash|zsh|fish]",
Short: "Generate a shell completion for spin-admin",
Long: `To load completions:
Bash:
$ source <(spin-admin completion bash)
# To load completions for each session, execute once:
Linux:
$ spin-admin completion bash > /etc/bash_completion.d/spin-admin
MacOS:
$ spin-admin completion bash > /usr/local/etc/bash_completion.d/spin-admin
Zsh:
# If shell completion is not already enabled in your environment you will need
# to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ spin-admin completion zsh > "${fpath[1]}/_spin-admin"
# You will need to start a new shell for this setup to take effect.
Fish:
$ spin-admin completion fish | source
# To load completions for each session, execute once:
$ spin-admin completion fish > ~/.config/fish/completions/spin-admin.fish
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "fish", "zsh"},
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
switch args[0] {
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "bash":
err = cmd.Root().GenBashCompletion(os.Stdout)
case "fish":
err = cmd.Root().GenFishCompletion(os.Stdout, true)
}
if err != nil {
return err
}

return nil
},
}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(sa.NewServiceAccountCommand())
rootCmd.AddCommand(newVersionCommand())
rootCmd.AddCommand(newCompletionCmd())
}

func Execute() error {
Expand Down

0 comments on commit 00a71b6

Please sign in to comment.