github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/report/cyclonedx/cyclonedx.go (about) 1 package cyclonedx 2 3 import ( 4 "io" 5 6 cdx "github.com/CycloneDX/cyclonedx-go" 7 "golang.org/x/xerrors" 8 9 "github.com/devseccon/trivy/pkg/sbom/cyclonedx" 10 "github.com/devseccon/trivy/pkg/types" 11 ) 12 13 // Writer implements types.Writer 14 type Writer struct { 15 output io.Writer 16 format cdx.BOMFileFormat 17 marshaler *cyclonedx.Marshaler 18 } 19 20 func NewWriter(output io.Writer, appVersion string) Writer { 21 return Writer{ 22 output: output, 23 format: cdx.BOMFileFormatJSON, 24 marshaler: cyclonedx.NewMarshaler(appVersion), 25 } 26 } 27 28 // Write writes the results in CycloneDX format 29 func (w Writer) Write(report types.Report) error { 30 bom, err := w.marshaler.Marshal(report) 31 if err != nil { 32 return xerrors.Errorf("CycloneDX marshal error: %w", err) 33 } 34 35 encoder := cdx.NewBOMEncoder(w.output, w.format) 36 encoder.SetPretty(true) 37 if err = encoder.Encode(bom); err != nil { 38 return xerrors.Errorf("failed to encode bom: %w", err) 39 } 40 41 return nil 42 }