Skip to content

Commit 1fad466

Browse files
Should not print, if printer is not ready (#3037)
Signed-off-by: Felipe Avelar <[email protected]>
1 parent f23f1b5 commit 1fad466

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

internal/console/kics.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package console
33
import (
44
"context"
55
"fmt"
6-
"strings"
76
"time"
87

98
"github.com/Checkmarx/kics/internal/console/printer"
@@ -128,10 +127,7 @@ func Execute() error {
128127

129128
if err := rootCmd.ExecuteContext(ctx); err != nil {
130129
sentry.CaptureException(err)
131-
if !(strings.HasPrefix(err.Error(), "unknown shorthand flag") ||
132-
strings.HasPrefix(err.Error(), "unknown flag") ||
133-
strings.HasPrefix(err.Error(), "unknown command") ||
134-
strings.HasPrefix(err.Error(), "initialization error -")) {
130+
if printer.IsInitialized() {
135131
log.Err(err).Msg("Failed to run application")
136132
}
137133
return err

internal/console/printer/printer.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ var (
5757
outFileLogger interface{}
5858
outConsoleLogger interface{}
5959

60-
loggerFile interface{}
60+
loggerFile interface{}
61+
initialized bool
6162
)
6263

6364
// SetupPrinter - configures stdout and log options with given FlagSet
@@ -101,10 +102,15 @@ func SetupPrinter(flags *pflag.FlagSet) error {
101102
if err != nil {
102103
return err
103104
}
104-
105+
initialized = true
105106
return nil
106107
}
107108

109+
// IsInitialized returns true if printer is ready, false otherwise
110+
func IsInitialized() bool {
111+
return initialized
112+
}
113+
108114
func getFlagValue(flagName string, flags *pflag.FlagSet) bool {
109115
v, _ := strconv.ParseBool(flags.Lookup(flagName).Value.String())
110116
return v

internal/console/printer/printer_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/rs/zerolog"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
type args struct {
@@ -306,3 +307,7 @@ func TestPrinter_NoColor(t *testing.T) {
306307
})
307308
}
308309
}
310+
311+
func TestPrinter_IsInitialized(t *testing.T) {
312+
require.False(t, IsInitialized())
313+
}

0 commit comments

Comments
 (0)