feat: use STDOUT when report file not specified (#1642)

* feat: use STDOUT when report file not specified

Signed-off-by: Bryce Thuilot <bryce@thuilot.io>

* fix: update tests

Signed-off-by: Bryce Thuilot <bryce@thuilot.io>

* fix: use only '-' for stdout report

Signed-off-by: Bryce Thuilot <bryce@thuilot.io>

---------

Signed-off-by: Bryce Thuilot <bryce@thuilot.io>
This commit is contained in:
Bryce Thuilot authored and GitHub committed 2024-12-05 09:44:07 -06:00
1 parent ed205a5f63
commit f3adda07ca
3 files changed
+33 -22

No files matched your search

+10 -1
View File
@@ -283,8 +283,17 @@ func findingSummaryAndExit(findings []report.Finding, cmd *cobra.Command, cfg co
// write report if desired // write report if desired
reportPath, _ := cmd.Flags().GetString("report-path") reportPath, _ := cmd.Flags().GetString("report-path")
ext, _ := cmd.Flags().GetString("report-format") ext, _ := cmd.Flags().GetString("report-format")
if reportPath != "" { if reportPath != "" {
if err := report.Write(findings, cfg, ext, reportPath); err != nil { reportWriter := os.Stdout
if reportPath != "-" {
reportWriter, err = os.Create(reportPath)
if err != nil {
log.Fatal().Err(err).Msg("could not create report file")
}
}
if err = report.Write(findings, cfg, ext, reportWriter); err != nil {
log.Fatal().Err(err).Msg("could not write") log.Fatal().Err(err).Msg("could not write")
} }
} }
+8 -11
View File
@@ -1,7 +1,7 @@
package report package report
import ( import (
"os" "io"
"strings" "strings"
"github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/config"
@@ -13,23 +13,20 @@ const (
CWE_DESCRIPTION = "Use of Hard-coded Credentials" CWE_DESCRIPTION = "Use of Hard-coded Credentials"
) )
func Write(findings []Finding, cfg config.Config, ext string, reportPath string) error { func Write(findings []Finding, cfg config.Config, ext string, report io.WriteCloser) error {
file, err := os.Create(reportPath) var err error
if err != nil {
return err
}
ext = strings.ToLower(ext) ext = strings.ToLower(ext)
switch ext { switch ext {
case ".json", "json": case ".json", "json":
err = writeJson(findings, file) err = writeJson(findings, report)
case ".jsonextra", "jsonextra": case ".jsonextra", "jsonextra":
err = writeJsonExtra(findings, file) err = writeJsonExtra(findings, report)
case ".csv", "csv": case ".csv", "csv":
err = writeCsv(findings, file) err = writeCsv(findings, report)
case ".xml", "junit": case ".xml", "junit":
err = writeJunit(findings, file) err = writeJunit(findings, report)
case ".sarif", "sarif": case ".sarif", "sarif":
err = writeSarif(cfg, findings, file) err = writeSarif(cfg, findings, report)
} }
return err return err
+15 -10
View File
@@ -1,9 +1,7 @@
package report package report
import ( import (
"os" "bytes"
"path/filepath"
"strconv"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -97,15 +95,14 @@ func TestReport(t *testing.T) {
// }, // },
} }
for i, test := range tests { for _, test := range tests {
t.Run(test.ext, func(t *testing.T) { t.Run(test.ext, func(t *testing.T) {
tmpfile, err := os.Create(filepath.Join(t.TempDir(), strconv.Itoa(i)+test.ext)) buf := testWriter{
bytes.NewBuffer(nil),
}
err := Write(test.findings, config.Config{}, test.ext, buf)
require.NoError(t, err) require.NoError(t, err)
err = Write(test.findings, config.Config{}, test.ext, tmpfile.Name()) got := buf.Bytes()
require.NoError(t, err)
got, err := os.ReadFile(tmpfile.Name())
require.NoError(t, err)
assert.FileExists(t, tmpfile.Name())
if test.wantEmpty { if test.wantEmpty {
assert.Empty(t, got) assert.Empty(t, got)
return return
@@ -114,3 +111,11 @@ func TestReport(t *testing.T) {
}) })
} }
} }
type testWriter struct {
*bytes.Buffer
}
func (t testWriter) Close() error {
return nil
}