mirror of
https://github.com/anchore/syft.git
synced 2026-11-02 15:36:41 +01:00
Add "pretty" json configuration and change default behavior to be space-efficient (#2275)
* expose underlying format options Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * remove escape html options and address PR feedback Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * incorporate PR feedback Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli test Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
@@ -14,9 +14,10 @@ const DefaultVersion = "1.5"
|
||||
type Encoder struct {
|
||||
version cyclonedx.SpecVersion
|
||||
format cyclonedx.BOMFileFormat
|
||||
pretty bool
|
||||
}
|
||||
|
||||
func NewEncoder(version string, format cyclonedx.BOMFileFormat) (Encoder, error) {
|
||||
func NewEncoder(version string, format cyclonedx.BOMFileFormat, pretty bool) (Encoder, error) {
|
||||
specVersion, err := SpecVersionFromString(version)
|
||||
if err != nil {
|
||||
return Encoder{}, err
|
||||
@@ -24,13 +25,14 @@ func NewEncoder(version string, format cyclonedx.BOMFileFormat) (Encoder, error)
|
||||
return Encoder{
|
||||
version: specVersion,
|
||||
format: format,
|
||||
pretty: pretty,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (e Encoder) Encode(writer io.Writer, s sbom.SBOM) error {
|
||||
bom := cyclonedxhelpers.ToFormatModel(s)
|
||||
enc := cyclonedx.NewBOMEncoder(writer, e.format)
|
||||
enc.SetPretty(true)
|
||||
enc.SetPretty(e.pretty)
|
||||
enc.SetEscapeHTML(false)
|
||||
|
||||
return enc.EncodeVersion(bom, e.version)
|
||||
|
||||
@@ -48,12 +48,8 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon
|
||||
return
|
||||
}
|
||||
|
||||
var expected []byte
|
||||
if cfg.Redactor != nil {
|
||||
actual = cfg.Redactor.Redact(actual)
|
||||
expected = cfg.Redactor.Redact(testutils.GetGoldenFileContents(t))
|
||||
} else {
|
||||
expected = testutils.GetGoldenFileContents(t)
|
||||
}
|
||||
|
||||
if cfg.UpdateSnapshot && cfg.PersistRedactionsInSnapshot {
|
||||
@@ -62,6 +58,13 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon
|
||||
return
|
||||
}
|
||||
|
||||
var expected []byte
|
||||
if cfg.Redactor != nil {
|
||||
expected = cfg.Redactor.Redact(testutils.GetGoldenFileContents(t))
|
||||
} else {
|
||||
expected = testutils.GetGoldenFileContents(t)
|
||||
}
|
||||
|
||||
if cfg.IsJSON {
|
||||
require.JSONEq(t, string(expected), string(actual))
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user