Replaces getRoot function by simply using cmd.Root() builtin

This commit is contained in:
Miguel de la Cruz 2020-10-05 11:58:31 +02:00
parent 12774baedc
commit c0bd4fd0e1

View file

@ -47,27 +47,14 @@ To configure your zsh shell to load completions for each session, add the above
} }
} }
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) { func bashCompletionCmdF(cmd *cobra.Command, args []string) {
root := getRoot(cmd) if err := cmd.Root().GenBashCompletion(os.Stdout); err != nil {
if err := root.GenBashCompletion(os.Stdout); err != nil {
ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err)) ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err))
} }
} }
func zshCompletionCmdF(cmd *cobra.Command, args []string) { func zshCompletionCmdF(cmd *cobra.Command, args []string) {
root := getRoot(cmd) if err := cmd.Root().GenZshCompletion(os.Stdout); err != nil {
if err := root.GenZshCompletion(os.Stdout); err != nil {
ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err)) ErrorAndExit(cmd, fmt.Errorf("unable to generate completions: %w", err))
} }
} }