-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
table.go
166 lines (142 loc) · 4.17 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package table
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"github.com/fatih/color"
"golang.org/x/exp/slices"
"github.com/aquasecurity/table"
"github.com/aquasecurity/tml"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/types"
)
var (
SeverityColor = []func(a ...interface{}) string{
color.New(color.FgCyan).SprintFunc(), // UNKNOWN
color.New(color.FgBlue).SprintFunc(), // LOW
color.New(color.FgYellow).SprintFunc(), // MEDIUM
color.New(color.FgHiRed).SprintFunc(), // HIGH
color.New(color.FgRed).SprintFunc(), // CRITICAL
}
)
// Writer implements Writer and output in tabular form
type Writer struct {
Severities []dbTypes.Severity
Output io.Writer
// Show dependency origin tree
Tree bool
// We have to show a message once about using the '-format json' subcommand to get the full pkgPath
ShowMessageOnce *sync.Once
// For misconfigurations
IncludeNonFailures bool
Trace bool
// For licenses
LicenseRiskThreshold int
IgnoredLicenses []string
}
type Renderer interface {
Render() string
}
// Write writes the result on standard output
func (tw Writer) Write(report types.Report) error {
for _, result := range report.Results {
// Not display a table of custom resources
if result.Class == types.ClassCustom {
continue
}
tw.write(result)
}
return nil
}
func (tw Writer) write(result types.Result) {
if result.IsEmpty() && result.Class != types.ClassOSPkg {
return
}
var renderer Renderer
switch {
// vulnerability
case result.Class == types.ClassOSPkg || result.Class == types.ClassLangPkg:
renderer = NewVulnerabilityRenderer(result, tw.isOutputToTerminal(), tw.Tree, tw.Severities)
// misconfiguration
case result.Class == types.ClassConfig:
renderer = NewMisconfigRenderer(result, tw.Severities, tw.Trace, tw.IncludeNonFailures, tw.isOutputToTerminal())
// secret
case result.Class == types.ClassSecret:
renderer = NewSecretRenderer(result.Target, result.Secrets, tw.isOutputToTerminal(), tw.Severities)
// package license
case result.Class == types.ClassLicense:
renderer = NewPkgLicenseRenderer(result, tw.isOutputToTerminal(), tw.Severities)
// file license
case result.Class == types.ClassLicenseFile:
renderer = NewFileLicenseRenderer(result, tw.isOutputToTerminal(), tw.Severities)
default:
return
}
_, _ = fmt.Fprint(tw.Output, renderer.Render())
}
func (tw Writer) isOutputToTerminal() bool {
return IsOutputToTerminal(tw.Output)
}
func newTableWriter(output io.Writer, isTerminal bool) *table.Table {
tableWriter := table.New(output)
if isTerminal { // use ansi output if we're not piping elsewhere
tableWriter.SetHeaderStyle(table.StyleBold)
tableWriter.SetLineStyle(table.StyleDim)
}
tableWriter.SetBorders(true)
tableWriter.SetAutoMerge(true)
tableWriter.SetRowLines(true)
return tableWriter
}
func summarize(specifiedSeverities []dbTypes.Severity, severityCount map[string]int) (int, []string) {
var total int
var severities []string
for _, sev := range specifiedSeverities {
severities = append(severities, sev.String())
}
var summaries []string
for _, severity := range dbTypes.SeverityNames {
if !slices.Contains(severities, severity) {
continue
}
count := severityCount[severity]
r := fmt.Sprintf("%s: %d", severity, count)
summaries = append(summaries, r)
total += count
}
return total, summaries
}
func IsOutputToTerminal(output io.Writer) bool {
if runtime.GOOS == "windows" {
// if its windows, we don't support formatting
return false
}
if output != os.Stdout {
return false
}
o, err := os.Stdout.Stat()
if err != nil {
return false
}
return (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice
}
func RenderTarget(w io.Writer, target string, isTerminal bool) {
if isTerminal {
// nolint
_ = tml.Fprintf(w, "\n<underline><bold>%s</bold></underline>\n\n", target)
} else {
_, _ = fmt.Fprintf(w, "\n%s\n", target)
_, _ = fmt.Fprintf(w, "%s\n", strings.Repeat("=", len(target)))
}
}
func ColorizeSeverity(value, severity string) string {
for i, name := range dbTypes.SeverityNames {
if severity == name {
return SeverityColor[i](value)
}
}
return color.New(color.FgBlue).SprintFunc()(severity)
}