Skip to content

Commit

Permalink
wrapper to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoAtGit committed Jan 24, 2024
1 parent c699c79 commit f728333
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
26 changes: 26 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package logger

import (
"github.com/rs/zerolog"

Check failure on line 4 in pkg/logger/logger.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/golangci/golangci-lint (goimports)
"os"
)

type LogSink struct {
logs []string
}

func NewLogger(logs *LogSink) zerolog.Logger {
if logs == nil {
return zerolog.New(os.Stdout).With().Timestamp().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
return zerolog.New(logs)
}

func (l *LogSink) Write(p []byte) (n int, err error) {
l.logs = append(l.logs, string(p))
return len(p), nil
}

func (l *LogSink) Index(i int) string {
return l.logs[i]
}
8 changes: 4 additions & 4 deletions pkg/printer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"

consoleFlags "github.com/Checkmarx/kics/internal/console/flags"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog"
)

func validateFlags() error {
Expand All @@ -27,12 +27,12 @@ func validateFlags() error {
}

// PrintScanDuration prints the scan duration
func PrintScanDuration(elapsed time.Duration) {
func PrintScanDuration(logger *zerolog.Logger, elapsed time.Duration) {
if consoleFlags.GetBoolFlag(consoleFlags.CIFlag) {
elapsedStrFormat := "Scan duration: %vms\n"
log.Info().Msgf(elapsedStrFormat, elapsed.Milliseconds())
(*logger).Info().Msgf(elapsedStrFormat, elapsed.Milliseconds())
} else {
elapsedStrFormat := "Scan duration: %v\n"
log.Info().Msgf(elapsedStrFormat, elapsed)
(*logger).Info().Msgf(elapsedStrFormat, elapsed)
}
}
25 changes: 10 additions & 15 deletions pkg/printer/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package printer

import (
"io/ioutil"
"os"
"testing"
"time"

"github.com/Checkmarx/kics/internal/console/flags"
loggerwrapper "github.com/Checkmarx/kics/pkg/logger"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)

func Test_PrintScanDuration(t *testing.T) {
Expand All @@ -33,12 +32,12 @@ func Test_PrintScanDuration(t *testing.T) {
{
name: "should print scan duration",
cmd: mockCmd,
flagsListContent: "",
flagsListContent: ``,
persintentFlag: true,
supportedPlatforms: []string{"terraform"},
supportedCloudProviders: []string{"aws"},
elapsed: time.Duration(1),
expected: "Scan duration: 1ns\n",
expected: "Scan duration: 1ns",
},
{
name: "should print scan duration when ci flag is true",
Expand All @@ -53,25 +52,21 @@ func Test_PrintScanDuration(t *testing.T) {
supportedPlatforms: []string{"terraform"},
supportedCloudProviders: []string{"aws"},
elapsed: time.Duration(1),
expected: "Scan duration: 0ms\n",
expected: "Scan duration: 0ms",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
flags.InitJSONFlags(test.cmd, test.flagsListContent, test.persintentFlag, test.supportedPlatforms, test.supportedCloudProviders)

rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
myBuffer := loggerwrapper.LogSink{}
logger := loggerwrapper.NewLogger(&myBuffer)

PrintScanDuration(test.elapsed)
PrintScanDuration(&logger, test.elapsed)
aux := myBuffer.Index(0)

w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout

require.Equal(t, test.expected, string(out))
assert.Contains(t, aux, test.expected)
})
}
}
4 changes: 3 additions & 1 deletion pkg/scan/post_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scan

import (
_ "embed" // Embed kics CLI img and scan-flags
loggerwrapper "github.com/Checkmarx/kics/pkg/logger"

Check failure on line 5 in pkg/scan/post_scan.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/golangci/golangci-lint (goimports)
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -133,7 +134,8 @@ func (c *Client) postScan(scanResults *Results) error {

deleteExtractionFolder(scanResults.ExtractedPaths.ExtractionMap)

consolePrinter.PrintScanDuration(time.Since(c.ScanStartTime))
logger := loggerwrapper.NewLogger(nil)
consolePrinter.PrintScanDuration(&logger, time.Since(c.ScanStartTime))

printVersionCheck(c.Printer, &summary)

Expand Down

0 comments on commit f728333

Please sign in to comment.