Skip to content

Commit

Permalink
refactor(sbom): use multiline json for spdx-json format (#4404)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed May 16, 2023
1 parent ea5fd75 commit 7a20d96
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pkg/report/spdx/spdx.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package spdx

import (
"encoding/json"
"io"

"github.com/spdx/tools-golang/json"
"github.com/spdx/tools-golang/spdx/v2/v2_3"
"github.com/spdx/tools-golang/tagvalue"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -34,7 +35,7 @@ func (w Writer) Write(report types.Report) error {
}

if w.format == "spdx-json" {
if err := json.Write(spdxDoc, w.output); err != nil {
if err := writeSPDXJson(spdxDoc, w.output); err != nil {
return xerrors.Errorf("failed to save spdx json: %w", err)
}
} else {
Expand All @@ -45,3 +46,19 @@ func (w Writer) Write(report types.Report) error {

return nil
}

// writeSPDXJson writes in human-readable format(multiple lines)
// use function from `github.com/spdx/tools-golang` after release https://github.com/spdx/tools-golang/pull/213
func writeSPDXJson(doc *v2_3.Document, w io.Writer) error {
buf, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return err
}

_, err = w.Write(buf)
if err != nil {
return err
}

return nil
}

0 comments on commit 7a20d96

Please sign in to comment.