github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/cmd/completion.go (about)

     1  package cmd
     2  
     3  import "github.com/spf13/cobra"
     4  
     5  type completionCmd struct {
     6  	cmd *cobra.Command
     7  }
     8  
     9  func newCompletionCmd() *completionCmd {
    10  	var root = &completionCmd{}
    11  	var cmd = &cobra.Command{
    12  		Use:   "completion [bash|zsh|fish]",
    13  		Short: "Print shell autocompletion scripts for goreleaser",
    14  		Long: `To load completions:
    15  
    16  Bash:
    17  
    18  $ source <(goreleaser completion bash)
    19  
    20  # To load completions for each session, execute once:
    21  Linux:
    22    $ goreleaser completion bash > /etc/bash_completion.d/goreleaser
    23  MacOS:
    24    $ goreleaser completion bash > /usr/local/etc/bash_completion.d/goreleaser
    25  
    26  Zsh:
    27  
    28  # If shell completion is not already enabled in your environment you will need
    29  # to enable it.  You can execute the following once:
    30  
    31  $ echo "autoload -U compinit; compinit" >> ~/.zshrc
    32  
    33  # To load completions for each session, execute once:
    34  $ goreleaser completion zsh > "${fpath[1]}/_goreleaser"
    35  
    36  # You will need to start a new shell for this setup to take effect.
    37  
    38  Fish:
    39  
    40  $ goreleaser completion fish | source
    41  
    42  # To load completions for each session, execute once:
    43  $ goreleaser completion fish > ~/.config/fish/completions/goreleaser.fish
    44  `,
    45  		SilenceUsage:          true,
    46  		DisableFlagsInUseLine: true,
    47  		ValidArgs:             []string{"bash", "zsh", "fish"},
    48  		Args:                  cobra.ExactValidArgs(1),
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			var err error
    51  			switch args[0] {
    52  			case "bash":
    53  				err = cmd.Root().GenBashCompletion(cmd.OutOrStdout())
    54  			case "zsh":
    55  				err = cmd.Root().GenZshCompletion(cmd.OutOrStdout())
    56  			case "fish":
    57  				err = cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true)
    58  			}
    59  
    60  			return err
    61  		},
    62  	}
    63  
    64  	root.cmd = cmd
    65  	return root
    66  }