1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/cmd/root.go
Carlos Alexandro Becker fe7e2123bd
feat: replacing the log library (#3139)
* feat: replacing logs

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: tests et al

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* feat: update termenv/lipgloss

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* wip: output

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: pin dep

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: update

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: deps

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: dep

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2022-06-21 21:11:15 -03:00

123 lines
2.9 KiB
Go

package cmd
import (
"errors"
"os"
"github.com/caarlos0/log"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)
var boldStyle = lipgloss.NewStyle().Bold(true)
func Execute(version string, exit func(int), args []string) {
// enable colored output on travis
if os.Getenv("CI") != "" {
lipgloss.SetColorProfile(termenv.ANSI256)
}
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",
Long: `GoReleaser is a release automation tool for Go projects.
Its goal is to simplify the build, release and publish steps while providing variant customization options for all steps.
GoReleaser is built for CI tools, you only need to download and execute it in your build script. Of course, you can also install it locally if you wish.
You can also customize your entire release process through a single .goreleaser.yaml file.
`,
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,
newDocsCmd().cmd,
newManCmd().cmd,
newSchemaCmd().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] == "completion" ||
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
}