mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-21 23:00:42 -08:00
* feat: introduce a new JSON schema * test: update * chore(mod): update fanal * refactor: add a comment * test(report): fix * refactor(writer): add omitempty * refactor: replace url * test(scanner): fix
39 lines
925 B
Go
39 lines
925 B
Go
package report
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/aquasecurity/trivy/pkg/log"
|
|
)
|
|
|
|
// JSONWriter implements result Writer
|
|
type JSONWriter struct {
|
|
Output io.Writer
|
|
}
|
|
|
|
// Write writes the results in JSON format
|
|
func (jw JSONWriter) Write(report Report) error {
|
|
var v interface{} = report
|
|
if os.Getenv("TRIVY_NEW_JSON_SCHEMA") == "" {
|
|
// After migrating to the new JSON schema, TRIVY_NEW_JSON_SCHEMA will be removed.
|
|
log.Logger.Warnf("DEPRECATED: the current JSON schema is deprecated, check %s for more information.",
|
|
"https://github.com/aquasecurity/trivy/discussions/1050")
|
|
v = report.Results
|
|
}
|
|
|
|
output, err := json.MarshalIndent(v, "", " ")
|
|
if err != nil {
|
|
return xerrors.Errorf("failed to marshal json: %w", err)
|
|
}
|
|
|
|
if _, err = fmt.Fprint(jw.Output, string(output)); err != nil {
|
|
return xerrors.Errorf("failed to write json: %w", err)
|
|
}
|
|
return nil
|
|
}
|