campaigner/cmd/completion.go

60 lines
1.4 KiB
Go
Raw Normal View History

2020-10-05 11:56:21 +02:00
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
func CompletionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "completion",
Short: "Generates autocompletions for bash and zsh",
}
cmd.AddCommand(
BashCompletionCmd(),
ZshCompletionCmd(),
)
return cmd
}
func BashCompletionCmd() *cobra.Command {
return &cobra.Command{
Use: "bash",
Short: "Generates autocompletions for bash",
Long: `Generates autocompletions for bash. To load them, run:
. <(campaigner completion bash)
To configure your bash shell to load completions for each session, add the above line to your ~/.bashrc`,
Run: bashCompletionCmdF,
}
}
func ZshCompletionCmd() *cobra.Command {
return &cobra.Command{
Use: "zsh",
Short: "Generates autocompletions for zsh",
Long: `Generates autocompletions for zsh. To load them, run:
. <(campaigner completion zsh)
To configure your zsh shell to load completions for each session, add the above line to your ~/.zshrc`,
Run: zshCompletionCmdF,
}
}
func bashCompletionCmdF(cmd *cobra.Command, args []string) {
2020-10-05 11:59:24 +02:00
if err := cmd.Root().GenBashCompletion(cmd.OutOrStdout()); err != nil {
2020-10-05 11:56:21 +02:00
ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err))
}
}
func zshCompletionCmdF(cmd *cobra.Command, args []string) {
2020-10-05 11:59:24 +02:00
if err := cmd.Root().GenZshCompletion(cmd.OutOrStdout()); err != nil {
2020-10-05 11:56:21 +02:00
ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err))
}
}