-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68d44e3
commit 28f8dbe
Showing
7 changed files
with
309 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
.circleci | ||
.git | ||
imgs | ||
.github | ||
.goreleaser.yaml | ||
.gitignore | ||
Dockerfile | ||
LICENSE | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package report | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/goodwithtech/dockle/pkg/types" | ||
) | ||
|
||
type JsonWriter struct { | ||
Output io.Writer | ||
IgnoreMap map[string]struct{} | ||
} | ||
|
||
type JsonOutputFormat struct { | ||
Summary JsonSummary `json:"summary"` | ||
Details []*JsonDetail `json:"details"` | ||
} | ||
type JsonSummary struct { | ||
Fatal int `json:"fatal"` | ||
Warn int `json:"warn"` | ||
Info int `json:"info"` | ||
Pass int `json:"pass"` | ||
} | ||
type JsonDetail struct { | ||
Code string `json:"code"` | ||
Title string `json:"title"` | ||
Level string `json:"level"` | ||
Alerts []string `json:"alerts"` | ||
} | ||
|
||
func (jw JsonWriter) Write(assessments []*types.Assessment) (bool, error) { | ||
var abendAssessments []*types.Assessment | ||
jsonSummary := JsonSummary{} | ||
jsonDetails := []*JsonDetail{} | ||
targetType := types.MinTypeNumber | ||
for targetType <= types.MaxTypeNumber { | ||
filtered := filteredAssessments(jw.IgnoreMap, targetType, assessments) | ||
level, detail := jsonDetail(targetType, filtered) | ||
if detail != nil { | ||
jsonDetails = append(jsonDetails, detail) | ||
} | ||
|
||
// increment summary | ||
switch level { | ||
case types.FatalLevel: | ||
jsonSummary.Fatal++ | ||
case types.WarnLevel: | ||
jsonSummary.Warn++ | ||
case types.InfoLevel: | ||
jsonSummary.Info++ | ||
default: | ||
jsonSummary.Pass++ | ||
} | ||
|
||
for _, assessment := range filtered { | ||
abendAssessments = filterAbendAssessments(jw.IgnoreMap, abendAssessments, assessment) | ||
} | ||
targetType++ | ||
} | ||
result := JsonOutputFormat{ | ||
Summary: jsonSummary, | ||
Details: jsonDetails, | ||
} | ||
output, err := json.MarshalIndent(result, "", " ") | ||
if err != nil { | ||
return false, xerrors.Errorf("failed to marshal json: %w", err) | ||
} | ||
|
||
if _, err = fmt.Fprint(jw.Output, string(output)); err != nil { | ||
return false, xerrors.Errorf("failed to write json: %w", err) | ||
} | ||
return len(abendAssessments) > 0, nil | ||
} | ||
func jsonDetail(assessmentType int, assessments []*types.Assessment) (level int, jsonInfo *JsonDetail) { | ||
if len(assessments) == 0 { | ||
return types.PassLevel, nil | ||
} | ||
if assessments[0].Level == types.SkipLevel { | ||
return types.SkipLevel, nil | ||
} | ||
|
||
detail := types.AlertDetails[assessmentType] | ||
level = detail.DefaultLevel | ||
if assessments[0].Level == types.IgnoreLevel { | ||
level = types.IgnoreLevel | ||
} | ||
|
||
alerts := []string{} | ||
for _, assessment := range assessments { | ||
alerts = append(alerts, assessment.Desc) | ||
} | ||
jsonInfo = &JsonDetail{ | ||
Code: detail.Code, | ||
Title: detail.Title, | ||
Level: AlertLabels[level], | ||
Alerts: alerts, | ||
} | ||
return level, jsonInfo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package report | ||
|
||
import ( | ||
"github.com/goodwithtech/dockle/pkg/types" | ||
) | ||
|
||
var AlertLabels = []string{ | ||
"INFO", | ||
"WARN", | ||
"FATAL", | ||
"PASS", | ||
"SKIP", | ||
"IGNORE", | ||
} | ||
|
||
type Writer interface { | ||
Write(assessments []*types.Assessment) (bool, error) | ||
} | ||
|
||
func filteredAssessments(ignoreCheckpointMap map[string]struct{}, target int, assessments []*types.Assessment) (filtered []*types.Assessment) { | ||
detail := types.AlertDetails[target] | ||
for _, assessment := range assessments { | ||
if assessment.Type == target { | ||
if _, ok := ignoreCheckpointMap[detail.Code]; ok { | ||
assessment.Level = types.IgnoreLevel | ||
} | ||
filtered = append(filtered, assessment) | ||
} | ||
} | ||
return filtered | ||
} | ||
|
||
func filterAbendAssessments(ignoreCheckpointMap map[string]struct{}, abendAssessments []*types.Assessment, assessment *types.Assessment) []*types.Assessment { | ||
if assessment.Level == types.SkipLevel { | ||
return abendAssessments | ||
} | ||
|
||
detail := types.AlertDetails[assessment.Type] | ||
if _, ok := ignoreCheckpointMap[detail.Code]; ok { | ||
return abendAssessments | ||
} | ||
return append(abendAssessments, assessment) | ||
} |
Oops, something went wrong.