Skip to content

Commit

Permalink
feat: output compact JSON by default with option for pretty format (#…
Browse files Browse the repository at this point in the history
…2406)

Signed-off-by: tomersein <[email protected]>
Signed-off-by: Keith Zantow <[email protected]>
Co-authored-by: Keith Zantow <[email protected]>
  • Loading branch information
tomersein and kzantow authored Feb 1, 2025
1 parent ac07be7 commit 2720733
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cmd/grype/cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func runGrype(app clio.Application, opts *options.Grype, userInput string) (errs
writer, err := format.MakeScanResultWriter(opts.Outputs, opts.File, format.PresentationConfig{
TemplateFilePath: opts.OutputTemplateFile,
ShowSuppressed: opts.ShowSuppressed,
Pretty: opts.Pretty,
})
if err != nil {
return err
Expand Down Expand Up @@ -211,6 +212,7 @@ func runGrype(app clio.Application, opts *options.Grype, userInput string) (errs
SBOM: s,
AppConfig: opts,
DBStatus: status,
Pretty: opts.Pretty,
}); err != nil {
errs = appendErrors(errs, err)
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/grype/cli/options/grype.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

type Grype struct {
Outputs []string `yaml:"output" json:"output" mapstructure:"output"` // -o, <presenter>=<file> the Presenter hint string to use for report formatting and the output file
File string `yaml:"file" json:"file" mapstructure:"file"` // --file, the file to write report output to
Outputs []string `yaml:"output" json:"output" mapstructure:"output"` // -o, <presenter>=<file> the Presenter hint string to use for report formatting and the output file
File string `yaml:"file" json:"file" mapstructure:"file"` // --file, the file to write report output to
Pretty bool `yaml:"pretty" json:"pretty" mapstructure:"pretty"`
Distro string `yaml:"distro" json:"distro" mapstructure:"distro"` // --distro, specify a distro to explicitly use
GenerateMissingCPEs bool `yaml:"add-cpes-if-none" json:"add-cpes-if-none" mapstructure:"add-cpes-if-none"` // --add-cpes-if-none, automatically generate CPEs if they are not present in import (e.g. from a 3rd party SPDX document)
OutputTemplateFile string `yaml:"output-template-file" json:"output-template-file" mapstructure:"output-template-file"` // -t, the template file to use for formatting the final report
Expand Down Expand Up @@ -166,6 +167,7 @@ output-template-file: .grype/html.tmpl
write output report to a file (default is to write to stdout)`)
descriptions.Add(&o.Outputs, `the output format of the vulnerability report (options: table, template, json, cyclonedx)
when using template as the output type, you must also provide a value for 'output-template-file'`)
descriptions.Add(&o.Pretty, `pretty-print output`)
descriptions.Add(&o.FailOn, `upon scanning, if a severity is found at or above the given severity then the return code will be 1
default is unset which will skip this validation (options: negligible, low, medium, high, critical)`)
descriptions.Add(&o.Ignore, `A list of vulnerability ignore rules, one or more property may be specified and all matching vulnerabilities will be ignored.
Expand Down
6 changes: 5 additions & 1 deletion grype/presenter/json/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Presenter struct {
metadataProvider vulnerability.MetadataProvider
appConfig interface{}
dbStatus interface{}
pretty bool
}

// NewPresenter creates a new JSON presenter
Expand All @@ -34,6 +35,7 @@ func NewPresenter(pb models.PresenterConfig) *Presenter {
context: pb.Context,
appConfig: pb.AppConfig,
dbStatus: pb.DBStatus,
pretty: pb.Pretty,
}
}

Expand All @@ -48,6 +50,8 @@ func (pres *Presenter) Present(output io.Writer) error {
enc := json.NewEncoder(output)
// prevent > and < from being escaped in the payload
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if pres.pretty {
enc.SetIndent("", " ")
}
return enc.Encode(&doc)
}
1 change: 1 addition & 0 deletions grype/presenter/models/presenter_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type PresenterConfig struct {
SBOM *sbom.SBOM
AppConfig interface{}
DBStatus interface{}
Pretty bool
}
1 change: 1 addition & 0 deletions internal/format/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type PresentationConfig struct {
TemplateFilePath string
ShowSuppressed bool
Pretty bool
}

// GetPresenter retrieves a Presenter that matches a CLI option
Expand Down
6 changes: 3 additions & 3 deletions test/cli/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func TestCmd(t *testing.T) {
name: "ensure valid descriptor",
args: []string{getFixtureImage(t, "image-bare"), "-o", "json"},
assertions: []traitAssertion{
assertInOutput(`"check-for-app-update": false`), // assert existence of the app config block
assertInOutput(`"db": {`), // assert existence of the db status block
assertInOutput(`"built":`), // assert existence of the db status block
assertInOutput(`"check-for-app-update":`), // assert existence of the app config block
assertInOutput(`"db":`), // assert existence of the db status block
assertInOutput(`"built":`), // assert existence of the db status block
},
},
{
Expand Down

0 comments on commit 2720733

Please sign in to comment.