github.com/amane3/goreleaser@v0.182.0/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"github.com/apex/log"
     8  	"github.com/apex/log/handlers/cli"
     9  	"github.com/fatih/color"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  func Execute(version string, exit func(int), args []string) {
    14  	// enable colored output on travis
    15  	if os.Getenv("CI") != "" {
    16  		color.NoColor = false
    17  	}
    18  	log.SetHandler(cli.Default)
    19  	newRootCmd(version, exit).Execute(args)
    20  }
    21  
    22  func (cmd *rootCmd) Execute(args []string) {
    23  	cmd.cmd.SetArgs(args)
    24  
    25  	if shouldPrependRelease(cmd.cmd, args) {
    26  		cmd.cmd.SetArgs(append([]string{"release"}, args...))
    27  	}
    28  
    29  	if err := cmd.cmd.Execute(); err != nil {
    30  		var code = 1
    31  		var msg = "command failed"
    32  		var eerr = &exitError{}
    33  		if errors.As(err, &eerr) {
    34  			code = eerr.code
    35  			if eerr.details != "" {
    36  				msg = eerr.details
    37  			}
    38  		}
    39  		log.WithError(err).Error(msg)
    40  		cmd.exit(code)
    41  	}
    42  }
    43  
    44  type rootCmd struct {
    45  	cmd   *cobra.Command
    46  	debug bool
    47  	exit  func(int)
    48  }
    49  
    50  func newRootCmd(version string, exit func(int)) *rootCmd {
    51  	var root = &rootCmd{
    52  		exit: exit,
    53  	}
    54  	var cmd = &cobra.Command{
    55  		Use:           "goreleaser",
    56  		Short:         "Deliver Go binaries as fast and easily as possible",
    57  		Version:       version,
    58  		SilenceUsage:  true,
    59  		SilenceErrors: true,
    60  		Args:          cobra.NoArgs,
    61  		PersistentPreRun: func(cmd *cobra.Command, args []string) {
    62  			if root.debug {
    63  				log.SetLevel(log.DebugLevel)
    64  				log.Debug("debug logs enabled")
    65  			}
    66  		},
    67  	}
    68  
    69  	cmd.PersistentFlags().BoolVar(&root.debug, "debug", false, "Enable debug mode")
    70  	cmd.AddCommand(
    71  		newBuildCmd().cmd,
    72  		newReleaseCmd().cmd,
    73  		newCheckCmd().cmd,
    74  		newInitCmd().cmd,
    75  		newCompletionCmd().cmd,
    76  	)
    77  
    78  	root.cmd = cmd
    79  	return root
    80  }
    81  
    82  func shouldPrependRelease(cmd *cobra.Command, args []string) bool {
    83  	// find current cmd, if its not root, it means the user actively
    84  	// set a command, so let it go
    85  	xmd, _, _ := cmd.Find(args)
    86  	if xmd != cmd {
    87  		return false
    88  	}
    89  
    90  	// allow help and the two __complete commands.
    91  	if len(args) > 0 && (args[0] == "help" ||
    92  		args[0] == cobra.ShellCompRequestCmd || args[0] == cobra.ShellCompNoDescRequestCmd) {
    93  		return false
    94  	}
    95  
    96  	// if we have != 1 args, assume its a release
    97  	if len(args) != 1 {
    98  		return true
    99  	}
   100  
   101  	// given that its 1, check if its one of the valid standalone flags
   102  	// for the root cmd
   103  	for _, s := range []string{"-h", "--help", "-v", "--version"} {
   104  		if s == args[0] {
   105  			// if it is, we should run the root cmd
   106  			return false
   107  		}
   108  	}
   109  
   110  	// otherwise, we should probably prepend release
   111  	return true
   112  }