Skip to content

Commit

Permalink
fix: disable logs on some commands (#4865)
Browse files Browse the repository at this point in the history
closes #4864

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed May 15, 2024
1 parent 9cf3bbb commit 7e0155c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
18 changes: 16 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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!")
},
}
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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))
})
}
}

0 comments on commit 7e0155c

Please sign in to comment.