1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-22 04:08:49 +02:00
goreleaser/cmd/root.go
Carlos Alexandro Becker 29cb60330b
docs: add cmd docs (#2184)
* docs: add cmd docs

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* docs: cmd docs

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: num cpus

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* docs: improve some things

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: improve docs

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: script

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
2021-04-22 10:45:36 -03:00

114 lines
2.4 KiB
Go

package cmd
import (
"errors"
"os"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
func Execute(version string, exit func(int), args []string) {
// enable colored output on travis
if os.Getenv("CI") != "" {
color.NoColor = false
}
log.SetHandler(cli.Default)
newRootCmd(version, exit).Execute(args)
}
func (cmd *rootCmd) Execute(args []string) {
cmd.cmd.SetArgs(args)
if shouldPrependRelease(cmd.cmd, args) {
cmd.cmd.SetArgs(append([]string{"release"}, args...))
}
if err := cmd.cmd.Execute(); err != nil {
code := 1
msg := "command failed"
eerr := &exitError{}
if errors.As(err, &eerr) {
code = eerr.code
if eerr.details != "" {
msg = eerr.details
}
}
log.WithError(err).Error(msg)
cmd.exit(code)
}
}
type rootCmd struct {
cmd *cobra.Command
debug bool
exit func(int)
}
func newRootCmd(version string, exit func(int)) *rootCmd {
root := &rootCmd{
exit: exit,
}
cmd := &cobra.Command{
Use: "goreleaser",
Short: "Deliver Go binaries as fast and easily as possible",
Version: version,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if root.debug {
log.SetLevel(log.DebugLevel)
log.Debug("debug logs enabled")
}
},
}
cmd.PersistentFlags().BoolVar(&root.debug, "debug", false, "Enable debug mode")
cmd.AddCommand(
newBuildCmd().cmd,
newReleaseCmd().cmd,
newCheckCmd().cmd,
newInitCmd().cmd,
newCompletionCmd().cmd,
newDocsCmd().cmd,
)
root.cmd = cmd
return root
}
func shouldPrependRelease(cmd *cobra.Command, args []string) bool {
// find current cmd, if its not root, it means the user actively
// set a command, so let it go
xmd, _, _ := cmd.Find(args)
if xmd != cmd {
return false
}
// allow help and the two __complete commands.
if len(args) > 0 && (args[0] == "help" ||
args[0] == cobra.ShellCompRequestCmd || args[0] == cobra.ShellCompNoDescRequestCmd) {
return false
}
// if we have != 1 args, assume its a release
if len(args) != 1 {
return true
}
// given that its 1, check if its one of the valid standalone flags
// for the root cmd
for _, s := range []string{"-h", "--help", "-v", "--version"} {
if s == args[0] {
// if it is, we should run the root cmd
return false
}
}
// otherwise, we should probably prepend release
return true
}