+
Skip to content

Always show results with json output for db search commands #2692

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

Merged
merged 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions cmd/grype/cli/commands/db_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,14 @@ func runDBSearchMatches(opts dbSearchMatchOptions) error {
}
}

if len(rows) != 0 {
sb := &strings.Builder{}
err = presentDBSearchMatches(opts.Format.Output, rows, sb)
bus.Report(sb.String())
if err != nil {
return fmt.Errorf("unable to present search results: %w", err)
}
} else {
bus.Notify("No results found")
sb := &strings.Builder{}
err = presentDBSearchMatches(opts.Format.Output, rows, sb)
rep := sb.String()
if rep != "" {
bus.Report(rep)
}
if err != nil {
return fmt.Errorf("unable to present search results: %w", err)
}

return queryErr
Expand All @@ -189,6 +188,10 @@ func runDBSearchMatches(opts dbSearchMatchOptions) error {
func presentDBSearchMatches(outputFormat string, structuredRows dbsearch.Matches, output io.Writer) error {
switch outputFormat {
case tableOutputFormat:
if len(structuredRows) == 0 {
bus.Notify("No results found")
return nil
}
rows := renderDBSearchPackagesTableRows(structuredRows.Flatten())

table := newTable(output)
Expand All @@ -197,6 +200,10 @@ func presentDBSearchMatches(outputFormat string, structuredRows dbsearch.Matches
table.AppendBulk(rows)
table.Render()
case jsonOutputFormat:
if structuredRows == nil {
// always allocate the top level collection
structuredRows = dbsearch.Matches{}
}
enc := json.NewEncoder(output)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
Expand Down
24 changes: 14 additions & 10 deletions cmd/grype/cli/commands/db_search_vuln.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@ func runDBSearchVulnerabilities(opts dbSearchVulnerabilityOptions) error {
return err
}

if len(rows) != 0 {
sb := &strings.Builder{}
err = presentDBSearchVulnerabilities(opts.Format.Output, rows, sb)
bus.Report(sb.String())
} else {
bus.Notify("No results found")
sb := &strings.Builder{}
err = presentDBSearchVulnerabilities(opts.Format.Output, rows, sb)
rep := sb.String()
if rep != "" {
bus.Report(rep)
}

return err
Expand Down Expand Up @@ -132,12 +131,13 @@ func validateProvidersFilter(reader v6.Reader, providers []string) error {
}

func presentDBSearchVulnerabilities(outputFormat string, structuredRows []dbsearch.Vulnerability, output io.Writer) error {
if len(structuredRows) == 0 {
return nil
}

switch outputFormat {
case tableOutputFormat:
if len(structuredRows) == 0 {
bus.Notify("No results found")
return nil
}

rows := renderDBSearchVulnerabilitiesTableRows(structuredRows)

table := newTable(output)
Expand All @@ -146,6 +146,10 @@ func presentDBSearchVulnerabilities(outputFormat string, structuredRows []dbsear
table.AppendBulk(rows)
table.Render()
case jsonOutputFormat:
if structuredRows == nil {
// always allocate the top level collection
structuredRows = []dbsearch.Vulnerability{}
}
enc := json.NewEncoder(output)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载