mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-12 15:50:15 -08:00
31 lines
617 B
Go
31 lines
617 B
Go
package report
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/aquasecurity/trivy/pkg/types"
|
|
)
|
|
|
|
// JSONWriter implements result Writer
|
|
type JSONWriter struct {
|
|
Output io.Writer
|
|
}
|
|
|
|
// Write writes the results in JSON format
|
|
func (jw JSONWriter) Write(ctx context.Context, report types.Report) error {
|
|
output, err := json.MarshalIndent(report, "", " ")
|
|
if err != nil {
|
|
return xerrors.Errorf("failed to marshal json: %w", err)
|
|
}
|
|
|
|
if _, err = fmt.Fprintln(jw.Output, string(output)); err != nil {
|
|
return xerrors.Errorf("failed to write json: %w", err)
|
|
}
|
|
return nil
|
|
}
|