Skip to content

Commit

Permalink
Modernize terminal output (chainguard-dev#564)
Browse files Browse the repository at this point in the history
* Modernize terminal output

* Remove decorativeRisk function

* lint

* more output tuning

* improve consistency of terminal output

* update testdata
  • Loading branch information
tstromberg authored Nov 2, 2024
1 parent a0d9f6a commit 4d6d3b4
Show file tree
Hide file tree
Showing 35 changed files with 239 additions and 232 deletions.
5 changes: 4 additions & 1 deletion pkg/action/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ func handleFile(ctx context.Context, c malcontent.Config, fr, tr *malcontent.Fil

rbs := createFileReport(tr, fr)

// findings that exist only in the source
for _, fb := range fr.Behaviors {
// findings that exist only in the source
if !behaviorExists(fb, tr.Behaviors) {
fb.DiffRemoved = true
rbs.Behaviors = append(rbs.Behaviors, fb)
continue
}
// findings that exist in both, for reference
rbs.Behaviors = append(rbs.Behaviors, fb)
}

d.Modified.Set(relPath, rbs)
Expand Down
18 changes: 14 additions & 4 deletions pkg/render/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,22 @@ func (r Markdown) Full(ctx context.Context, rep *malcontent.Report) error {
}
}

// We split the added/removed up in Markdown to address readability feedback. Unfortunately,
// this means we hide "existing" behaviors, which causes context to suffer. We should evaluate an
// improved rendering, similar to the "terminal" refresh, that includes everything.
if added > 0 {
markdownTable(ctx, modified.Value, r.w, tableConfig{
Title: fmt.Sprintf("### %d new behaviors", added),
SkipRemoved: true,
Title: fmt.Sprintf("### %d new behaviors", added),
SkipRemoved: true,
SkipExisting: true,
})
}

if removed > 0 {
markdownTable(ctx, modified.Value, r.w, tableConfig{
Title: fmt.Sprintf("### %d removed behaviors", removed),
SkipAdded: true,
Title: fmt.Sprintf("### %d removed behaviors", removed),
SkipAdded: true,
SkipExisting: true,
})
}
}
Expand Down Expand Up @@ -173,6 +178,11 @@ func markdownTable(_ context.Context, fr *malcontent.FileReport, w io.Writer, rc
}

risk := k.Behavior.RiskLevel

if rc.SkipExisting && !(k.Behavior.DiffAdded || k.Behavior.DiffRemoved) {
continue
}

if k.Behavior.DiffAdded || rc.DiffAdded {
if rc.SkipAdded {
continue
Expand Down
8 changes: 4 additions & 4 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func New(kind string, w io.Writer) (malcontent.Renderer, error) {
}

func riskEmoji(score int) string {
symbol := ""
symbol := "🟢"
switch score {
case 2:
symbol = "⚠️"
symbol = "🟡"
case 3:
symbol = "🔥"
symbol = "🟠"
case 4:
symbol = "🚨"
symbol = "🛑"
}

return symbol
Expand Down
Loading

0 comments on commit 4d6d3b4

Please sign in to comment.