Adds completion command

This commit is contained in:
Miguel de la Cruz 2020-10-05 11:56:21 +02:00
parent 817d73abac
commit 12774baedc
2 changed files with 74 additions and 0 deletions

73
cmd/completion.go Normal file
View file

@ -0,0 +1,73 @@
package cmd
import (
"fmt"
"os"
"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 getRoot(cmd *cobra.Command) *cobra.Command {
root := cmd
for {
if !root.HasParent() {
break
}
root = root.Parent()
}
return root
}
func bashCompletionCmdF(cmd *cobra.Command, args []string) {
root := getRoot(cmd)
if err := root.GenBashCompletion(os.Stdout); err != nil {
ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err))
}
}
func zshCompletionCmdF(cmd *cobra.Command, args []string) {
root := getRoot(cmd)
if err := root.GenZshCompletion(os.Stdout); err != nil {
ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err))
}
}

View file

@ -53,6 +53,7 @@ func RootCmd() *cobra.Command {
PullCmd(), PullCmd(),
SyncCmd(), SyncCmd(),
ReportCmd(), ReportCmd(),
CompletionCmd(),
) )
return cmd return cmd