You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

  1. package commands
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. // NewCompletionCmd returns a cobra.Command that generates bash and zsh
  7. // completion scripts for the given root command. If hidden is true, the
  8. // command will not show up in the root command's list of available commands.
  9. func NewCompletionCmd(rootCmd *cobra.Command, hidden bool) *cobra.Command {
  10. flagZsh := "zsh"
  11. cmd := &cobra.Command{
  12. Use: "completion",
  13. Short: "Generate shell completion scripts",
  14. Long: fmt.Sprintf(`Generate Bash and Zsh completion scripts and print them to STDOUT.
  15. Once saved to file, a completion script can be loaded in the shell's
  16. current session as shown:
  17. $ . <(%s completion)
  18. To configure your bash shell to load completions for each session add to
  19. your $HOME/.bashrc or $HOME/.profile the following instruction:
  20. . <(%s completion)
  21. `, rootCmd.Use, rootCmd.Use),
  22. RunE: func(cmd *cobra.Command, _ []string) error {
  23. zsh, err := cmd.Flags().GetBool(flagZsh)
  24. if err != nil {
  25. return err
  26. }
  27. if zsh {
  28. return rootCmd.GenZshCompletion(cmd.OutOrStdout())
  29. }
  30. return rootCmd.GenBashCompletion(cmd.OutOrStdout())
  31. },
  32. Hidden: hidden,
  33. Args: cobra.NoArgs,
  34. }
  35. cmd.Flags().Bool(flagZsh, false, "Generate Zsh completion script")
  36. return cmd
  37. }