Skip to content

Commit

Permalink
Add shorter output format for 'scan' mode (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
tstromberg committed Sep 16, 2024
1 parent 20da9b7 commit dfc2c13
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
29 changes: 21 additions & 8 deletions bincapz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"context"
"fmt"
"io/fs"
"log/slog"
"os"
Expand Down Expand Up @@ -174,13 +175,6 @@ func main() {
}
}

renderer, err = render.New(formatFlag, outFile)
if err != nil {
log.Error("invalid format", slog.Any("error", err), slog.String("format", formatFlag))
returnCode = ExitInvalidArgument
return err
}

rfs := []fs.FS{rules.FS}
if thirdPartyFlag {
rfs = append(rfs, thirdparty.FS)
Expand All @@ -200,6 +194,21 @@ func main() {
scanPaths = args[2:]
}

chosenFormat := formatFlag
if chosenFormat == "auto" {
chosenFormat = "terminal"
if slices.Contains(args, "scan") {
chosenFormat = "terminal_brief"
}
}

renderer, err = render.New(chosenFormat, outFile)
if err != nil {
log.Error("invalid format", slog.Any("error", err), slog.String("format", formatFlag))
returnCode = ExitInvalidArgument
return err
}

bc = bincapz.Config{
Concurrency: concurrencyFlag,
ErrFirstHit: errFirstHitFlag,
Expand Down Expand Up @@ -241,7 +250,7 @@ func main() {
},
&cli.StringFlag{
Name: "format",
Value: "terminal",
Value: "auto",
Usage: "Output format (json, markdown, simple, terminal, yaml)",
Destination: &formatFlag,
},
Expand Down Expand Up @@ -433,6 +442,10 @@ func main() {
return err
}

if res.Files.Len() > 0 {
fmt.Fprintf(os.Stderr, "\n\ntip: For detailed analysis, run: bincapz analyze <path>\n")
}

return nil
},
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func New(kind string, w io.Writer) (bincapz.Renderer, error) {
switch kind {
case "", "auto", "terminal":
return NewTerminal(w), nil
case "terminal_brief":
return NewTerminalBrief(w), nil
case "markdown":
return NewMarkdown(w), nil
case "yaml":
Expand Down
69 changes: 69 additions & 0 deletions pkg/render/terminal_brief.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 Chainguard, Inc.
// SPDX-License-Identifier: Apache-2.0
//
// Terminal Brief renderer
//
// Example:
//
// [CRITICAL] /bin/ls: frobber (whatever), xavier (whatever)
// [HIGH ] /bin/zxa:
// [MED ] /bin/ar:

package render

import (
"context"
"fmt"
"io"
"strings"

"github.com/chainguard-dev/bincapz/pkg/bincapz"
"github.com/fatih/color"
)

type TerminalBrief struct {
w io.Writer
}

func NewTerminalBrief(w io.Writer) TerminalBrief {
return TerminalBrief{w: w}
}

func briefRiskColor(level string) string {
switch level {
case "LOW":
return color.HiGreenString("LOW ")
case "MEDIUM", "MED":
return color.HiYellowString("MED ")
case "HIGH":
return color.HiRedString("HIGH")
case "CRITICAL", "CRIT":
return color.HiMagentaString("CRIT")
default:
return color.WhiteString(level)
}
}

func (r TerminalBrief) File(_ context.Context, fr *bincapz.FileReport) error {
if len(fr.Behaviors) == 0 {
return nil
}

reasons := []string{}
for _, b := range fr.Behaviors {
reasons = append(reasons, fmt.Sprintf("%s %s%s%s", color.HiYellowString(b.ID), color.HiBlackString("("), b.Description, color.HiBlackString(")")))
}

fmt.Fprintf(r.w, "%s%s%s %s: %s", color.HiBlackString("["), briefRiskColor(fr.RiskLevel), color.HiBlackString("]"), color.HiGreenString(fr.Path),
strings.Join(reasons, color.HiBlackString(", ")))
return nil
}

func (r TerminalBrief) Full(_ context.Context, rep *bincapz.Report) error {
// Non-diff files are handled on the fly by File()
if rep.Diff == nil {
return nil
}

return fmt.Errorf("diffs are unsupported by the TerminalBrief renderer")
}

0 comments on commit dfc2c13

Please sign in to comment.