1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

fix: disable logs on some commands (#4865)

closes #4864

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2024-05-15 10:13:43 -03:00 committed by GitHub
parent 9cf3bbbc5c
commit 7e0155c557
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View File

@ -28,6 +28,10 @@ func (cmd *rootCmd) Execute(args []string) {
cmd.cmd.SetArgs(append([]string{"release"}, args...))
}
if shouldDisableLogs(args) {
log.SetLevel(log.FatalLevel)
}
if err := cmd.cmd.Execute(); err != nil {
code := 1
msg := "command failed"
@ -73,13 +77,13 @@ Check out our website for more information, examples and documentation: https://
SilenceErrors: true,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
PersistentPreRun: func(_ *cobra.Command, _ []string) {
PersistentPreRun: func(*cobra.Command, []string) {
if root.verbose || root.debug {
log.SetLevel(log.DebugLevel)
log.Debug("verbose output enabled")
}
},
PersistentPostRun: func(_ *cobra.Command, _ []string) {
PersistentPostRun: func(*cobra.Command, []string) {
log.Info("thanks for using goreleaser!")
},
}
@ -103,6 +107,16 @@ Check out our website for more information, examples and documentation: https://
return root
}
func shouldDisableLogs(args []string) bool {
return len(args) > 0 && (args[0] == "help" ||
args[0] == "completion" ||
args[0] == "man" ||
args[0] == "docs" ||
args[0] == "jsonschema" ||
args[0] == cobra.ShellCompRequestCmd ||
args[0] == cobra.ShellCompNoDescRequestCmd)
}
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

View File

@ -2,9 +2,11 @@ package cmd
import (
"bytes"
"strings"
"testing"
goversion "github.com/caarlos0/go-version"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
@ -100,3 +102,26 @@ func TestShouldPrependRelease(t *testing.T) {
require.False(t, result([]string{"__completeNoDesc"}))
})
}
func TestShouldDisableLogs(t *testing.T) {
testCases := []struct {
args []string
expect bool
}{
{nil, false},
{[]string{"release"}, false},
{[]string{"release", "--clean"}, false},
{[]string{"help"}, true},
{[]string{"completion"}, true},
{[]string{"man"}, true},
{[]string{"jsonschema"}, true},
{[]string{"docs"}, true},
{[]string{cobra.ShellCompRequestCmd}, true},
{[]string{cobra.ShellCompNoDescRequestCmd}, true},
}
for _, tC := range testCases {
t.Run(strings.Join(tC.args, " "), func(t *testing.T) {
require.Equal(t, tC.expect, shouldDisableLogs(tC.args))
})
}
}