Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "pretty" json configuration and change default behavior to be space-efficient #2275

Merged
merged 6 commits into from
Nov 20, 2023

Conversation

wagoodman
Copy link
Contributor

@wagoodman wagoodman commented Oct 31, 2023

This PR adds the following configuration options:

  • for *-json formats:
    • pretty: include indentation and newlines (default to false)
  • for *-xml formats:
    • pretty: include indentation at the beginning of new lines (default to false)

This affects the following formats:

  • syft-json
  • cyclonedx-json
  • cyclonedx-xml
  • spdx-json

A top-level format.pretty switch was also added that drives the default of any format.*.pretty option (by default it is unset, allowing the other options to default to false).

This changes the default JSON output to compact JSON.

Closes #561

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman added the enhancement New feature or request label Oct 31, 2023
@wagoodman wagoodman self-assigned this Oct 31, 2023
@wagoodman wagoodman marked this pull request as ready for review October 31, 2023 14:43
@wagoodman wagoodman requested a review from a team October 31, 2023 14:43
Copy link
Contributor

@willmurphyscode willmurphyscode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a few questions here. Happy to meet about for a sync discussion if that seems easier.

cmd/syft/cli/options/format_spdx_json_test.go Outdated Show resolved Hide resolved
cmd/syft/cli/options/format_cyclonedx_xml_test.go Outdated Show resolved Hide resolved
cmd/syft/cli/options/format_spdx_json.go Outdated Show resolved Hide resolved
cmd/syft/cli/options/format_syft_json.go Show resolved Hide resolved
syft/format/spdxjson/encoder_test.go Outdated Show resolved Hide resolved
syft/format/spdxjson/encoder_test.go Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
syft/format/cyclonedxxml/encoder_test.go Show resolved Hide resolved
@@ -62,6 +58,13 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon
return
}

var expected []byte
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this code moved down. It seems like this change has no effect, which makes me think I'm missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have explicitly labeled this change (I can split this into another PR if needed). The first iteration of this PR was heavy on snapshot testing, which after adding I backed out the change and added more explicit tests. In adding these snapshots I was surprised to see that I couldn't add any snapshots! This fixes that bug, where the snapshot needed to exist in order to update it, which is not correct.

Copy link
Contributor

@kzantow kzantow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers, necessarily, but I'd offer a suggestion (expanded in the comment):

  • don't add an escape HTML option, just never escape html
  • make compact/not pretty the default

cmd/syft/cli/options/format.go Show resolved Hide resolved
cmd/syft/cli/options/format_cyclonedx_json.go Outdated Show resolved Hide resolved
cmd/syft/cli/options/format_spdx_json.go Outdated Show resolved Hide resolved
cmd/syft/cli/options/format.go Show resolved Hide resolved
cmd/syft/cli/options/format_cyclonedx_xml.go Outdated Show resolved Hide resolved
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman changed the title Expose underlying format configuration (compact and escape HTML) Expose underlying format configuration (pretty json) Nov 3, 2023
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman changed the title Expose underlying format configuration (pretty json) Add "pretty" json configuration and change default behavior to be space-efficient Nov 3, 2023
@spiffcs
Copy link
Contributor

spiffcs commented Nov 3, 2023

@wagoodman I defer to the other reviewers for their comments being addressed here, but it looks like a cli test is failing

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Copy link
Contributor

@kzantow kzantow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman enabled auto-merge (squash) November 20, 2023 15:17
@wagoodman wagoodman merged commit 1676934 into main Nov 20, 2023
10 checks passed
@wagoodman wagoodman deleted the compact-json branch November 20, 2023 15:29
GijsCalis pushed a commit to GijsCalis/syft that referenced this pull request Feb 19, 2024
…ce-efficient (anchore#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose compact JSON and XML format configuration
4 participants