Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metedata to the report file #39

Merged
merged 3 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions internal/printer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"os"
"time"

"github.com/aquasecurity/chain-bench/internal/models/checkmodels"
)
Expand All @@ -15,11 +16,7 @@ var (
output io.Writer = os.Stdout
)

type Statistics struct {
Passed int
Failed int
}
type displayResult struct {
type reportResult struct {
ID string
Name string
Descrition string
Expand All @@ -29,45 +26,60 @@ type displayResult struct {
Url string
}

type reportMetadata struct {
Date string
Statistics Statistics
}

type reportResults struct {
Metadata reportMetadata
Results []reportResult
}

// println prints a string to the current configured output
func println(msg string) {
fmt.Fprintln(output, msg)
}

func PrintOutputToFile(data []checkmodels.CheckRunResult, outputFilePath string) {
file, _ := json.MarshalIndent(getPrintFormat(data), "", "")
reportRes, statistics := getPrintFormat(data)

// Populate the report metadata.
reportMetadata := reportMetadata{
Date: time.Now().Format(time.RFC3339),
Statistics: statistics,
}

// Populate the report.
report := reportResults{
reportMetadata,
reportRes,
}
file, _ := json.MarshalIndent(report, "", "")
err := ioutil.WriteFile(outputFilePath, file, 0644)
if err != nil {
PrintError("Failed to write to output file, make sure your path is valid")
}
}

func getPrintFormat(results []checkmodels.CheckRunResult) []displayResult {
resultsToDisplay := []displayResult{}
func getPrintFormat(results []checkmodels.CheckRunResult) ([]reportResult, Statistics) {
resultsToDisplay := []reportResult{}
statistics := NewStatistics()

for _, r := range results {
resultsToDisplay = append(resultsToDisplay, displayResult{
resultsToDisplay = append(resultsToDisplay, reportResult{
Name: r.Metadata.Title,
ID: r.ID,
Descrition: r.Metadata.Description,
Remediation: r.Metadata.Remediation,
Result: string(r.Result.Status),
Reason: r.Result.Details,
Url: r.Metadata.Url})
}

return resultsToDisplay
}

func initializeStatistics() Statistics {
return Statistics{Passed: 0, Failed: 0}
}

func addToStatistics(s *Statistics, r checkmodels.ResultStatus) {
if r == checkmodels.Passed {
s.Passed++
} else {
s.Failed++
statistics.Add(r.Result.Status)
}

return resultsToDisplay, statistics
}

// PrintErrorf prints a message with error color
Expand Down
4 changes: 2 additions & 2 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func PrintFindings(results []checkmodels.CheckRunResult, outputFilePath string,
PrintOutputToFile(results, outputFilePath)
}
if !isQuiet {
s := initializeStatistics()
s := NewStatistics()
table.Header = CreateHeader([]string{"ID", "Name", "Result", "Reason"})
for _, row := range results {
rowData := []CellData{
Expand All @@ -39,7 +39,7 @@ func PrintFindings(results []checkmodels.CheckRunResult, outputFilePath string,
{text: row.Result.Details},
}
table.Body.Cells = append(table.Body.Cells, CreateBodyRow(rowData))
addToStatistics(&s, row.Result.Status)
s.Add(row.Result.Status)
}
table.Footer = CreateFooter(s, len(table.Header.Cells))
fmt.Println(table.String())
Expand Down
54 changes: 54 additions & 0 deletions internal/printer/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package printer

import (
"fmt"

"github.com/aquasecurity/chain-bench/internal/models/checkmodels"
)

type Statistics struct {
Passed int
Failed int
Unknown int
Total int
}

// NewStatistics initializes a new Statistics struct.
func NewStatistics() Statistics {
return Statistics{Passed: 0, Failed: 0, Unknown: 0, Total: 0}
}

// Add increments the value of a specific field as well as the total value.
func (s *Statistics) Add(value checkmodels.ResultStatus) error {
switch value {
case checkmodels.Passed:
s.Passed++
case checkmodels.Failed:
s.Failed++
case checkmodels.Unknown:
s.Unknown++
default:
return fmt.Errorf("unknown statistical value: %s", value)
}
s.Total++

return nil
}

// Sub decrements the value of a specific field as well as the total value.
func (s *Statistics) Sub(value checkmodels.ResultStatus) error {
switch value {
case checkmodels.Passed:
s.Passed--
case checkmodels.Failed:
s.Failed--
case checkmodels.Unknown:
s.Unknown--
default:
return fmt.Errorf("unknown statistical value: %s", value)
}

s.Total--

return nil
}