-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
writer.go
78 lines (68 loc) · 2.15 KB
/
writer.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
package report
import (
"io"
"path/filepath"
"strings"
"time"
"golang.org/x/xerrors"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/report/cyclonedx"
"github.com/aquasecurity/trivy/pkg/types"
)
const (
SchemaVersion = 2
)
// Now returns the current time
var Now = time.Now
type Option struct {
Format string
Output io.Writer
Severities []dbTypes.Severity
OutputTemplate string
AppVersion string
// For misconfigurations
IncludeNonFailures bool
Trace bool
}
// Write writes the result to output, format as passed in argument
func Write(report types.Report, option Option) error {
var writer Writer
switch option.Format {
case "table":
writer = &TableWriter{
Output: option.Output,
Severities: option.Severities,
IncludeNonFailures: option.IncludeNonFailures,
Trace: option.Trace,
}
case "json":
writer = &JSONWriter{Output: option.Output}
case "cyclonedx":
// TODO: support xml format option with cyclonedx writer
writer = cyclonedx.NewWriter(option.Output, option.AppVersion)
case "template":
// We keep `sarif.tpl` template working for backward compatibility for a while.
if strings.HasPrefix(option.OutputTemplate, "@") && filepath.Base(option.OutputTemplate) == "sarif.tpl" {
log.Logger.Warn("Using `--template sarif.tpl` is deprecated. Please migrate to `--format sarif`. See https://github.com/aquasecurity/trivy/discussions/1571")
writer = SarifWriter{Output: option.Output, Version: option.AppVersion}
break
}
var err error
if writer, err = NewTemplateWriter(option.Output, option.OutputTemplate); err != nil {
return xerrors.Errorf("failed to initialize template writer: %w", err)
}
case "sarif":
writer = SarifWriter{Output: option.Output, Version: option.AppVersion}
default:
return xerrors.Errorf("unknown format: %v", option.Format)
}
if err := writer.Write(report); err != nil {
return xerrors.Errorf("failed to write results: %w", err)
}
return nil
}
// Writer defines the result write operation
type Writer interface {
Write(types.Report) error
}