Skip to content

Commit 8ff337e

Browse files
Remove duplicate call to funtction GetOutput and added a empty test case to yaml parser
1 parent 5bfcf8b commit 8ff337e

File tree

4 files changed

+63
-49
lines changed

4 files changed

+63
-49
lines changed

cmd/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,21 @@ func postRun(cmd *cobra.Command, args []string) error {
164164

165165
cfg := config.LoadConfig("2ms", Version)
166166

167+
var output string
168+
var err error
169+
167170
if Report.TotalItemsScanned > 0 {
168-
if err := Report.ShowReport(stdoutFormatVar, cfg); err != nil {
171+
output, err = Report.GetOutput(stdoutFormatVar, cfg)
172+
if err != nil {
173+
return err
174+
}
175+
176+
if err := Report.ShowReport(output); err != nil {
169177
return err
170178
}
171179

172180
if len(reportPathVar) > 0 {
173-
err := Report.WriteFile(reportPathVar, cfg)
174-
if err != nil {
175-
return fmt.Errorf("failed to create report file with error: %s", err)
176-
}
181+
Report.WriteFile(output, reportPathVar, cfg)
177182
}
178183
} else {
179184
log.Info().Msg("Scan completed with empty content")

lib/reporting/report.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,12 @@ func Init() *Report {
2828
Results: make(map[string][]*secrets.Secret),
2929
}
3030
}
31-
32-
func (r *Report) ShowReport(format string, cfg *config.Config) error {
33-
output, err := r.GetOutput(format, cfg)
34-
if err != nil {
35-
return err
36-
}
37-
31+
func (r *Report) ShowReport(output string) error {
3832
log.Info().Msg("\n" + output)
3933
return nil
4034
}
4135

42-
func (r *Report) WriteFile(reportPath []string, cfg *config.Config) error {
36+
func (r *Report) WriteFile(output string, reportPath []string, cfg *config.Config) error {
4337
for _, path := range reportPath {
4438
err := os.MkdirAll(filepath.Dir(path), 0750)
4539
if err != nil {

lib/reporting/report_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,17 @@ JPcHeO7M6FohKgcEHX84koQDN98J/L7pFlSoU7WOl6f8BKavIdeSTPS9qQYWdQuT
213213

214214
func TestWriteReportInNonExistingDir(t *testing.T) {
215215
report := Init()
216-
217216
tempDir := os.TempDir()
218-
path := filepath.Join(tempDir, "test_temp_dir", "sub_dir", "report.yaml")
219-
err := report.WriteFile([]string{path}, &config.Config{Name: "report", Version: "5"})
217+
dirPath := filepath.Join(tempDir, "test_temp_dir", "sub_dir")
218+
filePath := filepath.Join(dirPath, "report.yaml")
219+
defer os.RemoveAll(filepath.Join(tempDir, "test_temp_dir"))
220+
output, err := report.GetOutput("yaml", &config.Config{Name: "report", Version: "5"})
220221
if err != nil {
221-
t.Error(err)
222+
t.Error("Failed to get report output:", err)
223+
return
222224
}
223225

224-
os.RemoveAll(filepath.Join(tempDir, "test_temp_dir"))
226+
report.WriteFile(output, []string{filePath}, &config.Config{Name: "report", Version: "5"})
225227
}
226228

227229
func TestGetOutputSarif(t *testing.T) {
@@ -340,7 +342,14 @@ func TestGetOutputYAML(t *testing.T) {
340342
testCases := []struct {
341343
name string
342344
report Report
343-
}{
345+
}{{
346+
name: "No secrets found",
347+
report: Report{
348+
TotalItemsScanned: 5,
349+
TotalSecretsFound: 0,
350+
Results: map[string][]*secrets.Secret{},
351+
},
352+
},
344353
{
345354
name: "Single real secret in hardcodedPassword.go",
346355
report: Report{

lib/reporting/yaml.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,49 @@ import (
88
)
99

1010
func writeYaml(report *Report) (string, error) {
11+
estimatedSize := 1024 + len(report.Results)*512
1112
var builder strings.Builder
13+
builder.Grow(estimatedSize)
1214

13-
builder.WriteString(fmt.Sprintf("totalitemsscanned: %d\n", report.TotalItemsScanned))
14-
builder.WriteString(fmt.Sprintf("totalsecretsfound: %d\n", report.TotalSecretsFound))
15-
builder.WriteString("results:\n")
15+
fmt.Fprintf(&builder, "totalitemsscanned: %d\n", report.TotalItemsScanned)
16+
fmt.Fprintf(&builder, "totalsecretsfound: %d\n", report.TotalSecretsFound)
17+
if report.TotalSecretsFound == 0 {
18+
fmt.Fprint(&builder, "results: {}\n")
19+
} else {
1620

17-
for _, secretsList := range report.Results {
18-
if len(secretsList) > 0 {
19-
builder.WriteString(fmt.Sprintf(" %s:\n", secretsList[0].ID))
20-
}
21-
for _, s := range secretsList {
22-
builder.WriteString(" - id: " + s.ID + "\n")
23-
builder.WriteString(" source: " + s.Source + "\n")
24-
builder.WriteString(" ruleid: " + s.RuleID + "\n")
25-
builder.WriteString(fmt.Sprintf(" startline: %d\n", s.StartLine))
26-
builder.WriteString(fmt.Sprintf(" endline: %d\n", s.EndLine))
27-
builder.WriteString(fmt.Sprintf(" linecontent: %q\n", s.LineContent))
28-
builder.WriteString(fmt.Sprintf(" startcolumn: %d\n", s.StartColumn))
29-
builder.WriteString(fmt.Sprintf(" endcolumn: %d\n", s.EndColumn))
30-
builder.WriteString(" value: " + s.Value + "\n")
31-
builder.WriteString(fmt.Sprintf(" validationstatus: %q\n", fmt.Sprintf("%v", s.ValidationStatus)))
32-
builder.WriteString(" ruledescription: " + s.RuleDescription + "\n")
33-
if len(s.ExtraDetails) > 0 {
34-
builder.WriteString(" extradetails:\n")
35-
marshaled, err := yaml.Marshal(s.ExtraDetails)
36-
if err != nil {
37-
builder.WriteString(fmt.Sprintf(" error: %v\n", err))
38-
} else {
39-
lines := strings.Split(string(marshaled), "\n")
40-
for _, line := range lines {
41-
if line != "" {
42-
builder.WriteString(" " + line + "\n")
21+
builder.WriteString("results:\n")
22+
for _, secretsList := range report.Results {
23+
if len(secretsList) > 0 {
24+
fmt.Fprintf(&builder, " %s:\n", secretsList[0].ID)
25+
}
26+
for _, s := range secretsList {
27+
fmt.Fprintf(&builder, " - id: %s\n", s.ID)
28+
fmt.Fprintf(&builder, " source: %s\n", s.Source)
29+
fmt.Fprintf(&builder, " ruleid: %s\n", s.RuleID)
30+
fmt.Fprintf(&builder, " startline: %d\n", s.StartLine)
31+
fmt.Fprintf(&builder, " endline: %d\n", s.EndLine)
32+
fmt.Fprintf(&builder, " linecontent: %q\n", s.LineContent)
33+
fmt.Fprintf(&builder, " startcolumn: %d\n", s.StartColumn)
34+
fmt.Fprintf(&builder, " endcolumn: %d\n", s.EndColumn)
35+
fmt.Fprintf(&builder, " value: %s\n", s.Value)
36+
fmt.Fprintf(&builder, " validationstatus: %q\n", fmt.Sprintf("%v", s.ValidationStatus))
37+
fmt.Fprintf(&builder, " ruledescription: %s\n", s.RuleDescription)
38+
if len(s.ExtraDetails) > 0 {
39+
builder.WriteString(" extradetails:\n")
40+
marshaled, err := yaml.Marshal(s.ExtraDetails)
41+
if err != nil {
42+
fmt.Fprintf(&builder, " error: %v\n", err)
43+
} else {
44+
lines := strings.Split(string(marshaled), "\n")
45+
for _, line := range lines {
46+
if line != "" {
47+
fmt.Fprintf(&builder, " %s\n", line)
48+
}
4349
}
4450
}
4551
}
52+
fmt.Fprintf(&builder, " cvssscore: %.1f\n", s.CvssScore)
4653
}
47-
builder.WriteString(fmt.Sprintf(" cvssscore: %.1f\n", s.CvssScore))
4854
}
4955
}
5056

0 commit comments

Comments
 (0)