From ba91442c58bbd36faab992635fabb4e636f39cdb Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Fri, 20 Dec 2024 12:05:49 -0500 Subject: [PATCH 1/2] clean up config Signed-off-by: Alex Goodman --- cmd/grype/cli/commands/db_check.go | 11 +++++++++-- cmd/grype/cli/commands/db_delete.go | 11 +++++++++-- cmd/grype/cli/commands/db_diff.go | 11 +++++++++-- cmd/grype/cli/commands/db_import.go | 11 +++++++++-- cmd/grype/cli/commands/db_list.go | 11 +++++++++-- cmd/grype/cli/commands/db_providers.go | 11 +++++++++-- cmd/grype/cli/commands/db_search.go | 11 +++++++++-- cmd/grype/cli/commands/db_status.go | 11 +++++++++-- cmd/grype/cli/commands/db_update.go | 11 +++++++++-- cmd/grype/cli/commands/explain.go | 11 +++++++++-- 10 files changed, 90 insertions(+), 20 deletions(-) diff --git a/cmd/grype/cli/commands/db_check.go b/cmd/grype/cli/commands/db_check.go index a5b8a02ad61..175b27fa974 100644 --- a/cmd/grype/cli/commands/db_check.go +++ b/cmd/grype/cli/commands/db_check.go @@ -36,7 +36,7 @@ func DBCheck(app clio.Application) *cobra.Command { DBOptions: *dbOptionsDefault(app.ID()), } - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "check", Short: "check to see if there is a database update available", PreRunE: func(cmd *cobra.Command, args []string) error { @@ -48,7 +48,14 @@ func DBCheck(app clio.Application) *cobra.Command { RunE: func(_ *cobra.Command, _ []string) error { return runDBCheck(*opts) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *dbCheckOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBCheck(opts dbCheckOptions) error { diff --git a/cmd/grype/cli/commands/db_delete.go b/cmd/grype/cli/commands/db_delete.go index 4d4ef142c55..5a0ea8be3bc 100644 --- a/cmd/grype/cli/commands/db_delete.go +++ b/cmd/grype/cli/commands/db_delete.go @@ -15,7 +15,7 @@ import ( func DBDelete(app clio.Application) *cobra.Command { opts := dbOptionsDefault(app.ID()) - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "delete", Short: "delete the vulnerability database", Args: cobra.ExactArgs(0), @@ -23,7 +23,14 @@ func DBDelete(app clio.Application) *cobra.Command { RunE: func(_ *cobra.Command, _ []string) error { return runDBDelete(*opts) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *DBOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBDelete(opts DBOptions) error { diff --git a/cmd/grype/cli/commands/db_diff.go b/cmd/grype/cli/commands/db_diff.go index 6ff85f3eafb..53910d1edd3 100644 --- a/cmd/grype/cli/commands/db_diff.go +++ b/cmd/grype/cli/commands/db_diff.go @@ -33,7 +33,7 @@ func DBDiff(app clio.Application) *cobra.Command { DBOptions: *dbOptionsDefault(app.ID()), } - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "diff [flags] base_db_url target_db_url", Short: "diff two DBs and display the result", Args: cobra.MaximumNArgs(2), @@ -61,7 +61,14 @@ func DBDiff(app clio.Application) *cobra.Command { return runDBDiff(opts, base, target) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *dbDiffOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBDiff(opts *dbDiffOptions, base string, target string) (errs error) { diff --git a/cmd/grype/cli/commands/db_import.go b/cmd/grype/cli/commands/db_import.go index 1f98c17b841..0112a2db53c 100644 --- a/cmd/grype/cli/commands/db_import.go +++ b/cmd/grype/cli/commands/db_import.go @@ -17,7 +17,7 @@ import ( func DBImport(app clio.Application) *cobra.Command { opts := dbOptionsDefault(app.ID()) - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "import FILE", Short: "import a vulnerability database archive", Long: fmt.Sprintf("import a vulnerability database archive from a local FILE.\nDB archives can be obtained from %q.", internal.DBUpdateURL), @@ -25,7 +25,14 @@ func DBImport(app clio.Application) *cobra.Command { RunE: func(_ *cobra.Command, args []string) error { return runDBImport(*opts, args[0]) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *DBOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBImport(opts DBOptions, dbArchivePath string) error { diff --git a/cmd/grype/cli/commands/db_list.go b/cmd/grype/cli/commands/db_list.go index e02f4de553e..e03ffb89be0 100644 --- a/cmd/grype/cli/commands/db_list.go +++ b/cmd/grype/cli/commands/db_list.go @@ -32,7 +32,7 @@ func DBList(app clio.Application) *cobra.Command { DBOptions: *dbOptionsDefault(app.ID()), } - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "list", Short: "list all DBs available according to the listing URL", PreRunE: disableUI(app), @@ -40,7 +40,14 @@ func DBList(app clio.Application) *cobra.Command { RunE: func(_ *cobra.Command, _ []string) error { return runDBList(*opts) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *dbListOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBList(opts dbListOptions) error { diff --git a/cmd/grype/cli/commands/db_providers.go b/cmd/grype/cli/commands/db_providers.go index c59bf6feb12..330b2d8cd19 100644 --- a/cmd/grype/cli/commands/db_providers.go +++ b/cmd/grype/cli/commands/db_providers.go @@ -37,14 +37,21 @@ func DBProviders(app clio.Application) *cobra.Command { DBOptions: *dbOptionsDefault(app.ID()), } - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "providers", Short: "list vulnerability database providers", Args: cobra.ExactArgs(0), RunE: func(_ *cobra.Command, _ []string) error { return runDBProviders(opts, app) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *dbProvidersOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBProviders(opts *dbProvidersOptions, app clio.Application) error { diff --git a/cmd/grype/cli/commands/db_search.go b/cmd/grype/cli/commands/db_search.go index 1e1af4492af..51cac7b9221 100644 --- a/cmd/grype/cli/commands/db_search.go +++ b/cmd/grype/cli/commands/db_search.go @@ -36,7 +36,7 @@ func DBSearch(app clio.Application) *cobra.Command { DBOptions: *dbOptionsDefault(app.ID()), } - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "search [vulnerability_id]", Short: "get information on a vulnerability from the db", Args: cobra.ExactArgs(1), @@ -44,7 +44,14 @@ func DBSearch(app clio.Application) *cobra.Command { id := args[0] return runDBSearch(*opts, id) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *dbQueryOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBSearch(opts dbQueryOptions, vulnerabilityID string) error { diff --git a/cmd/grype/cli/commands/db_status.go b/cmd/grype/cli/commands/db_status.go index 35643ded326..5c19beb7f79 100644 --- a/cmd/grype/cli/commands/db_status.go +++ b/cmd/grype/cli/commands/db_status.go @@ -32,7 +32,7 @@ func DBStatus(app clio.Application) *cobra.Command { DBOptions: *dbOptionsDefault(app.ID()), } - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "status", Short: "display database status", Args: cobra.ExactArgs(0), @@ -40,7 +40,14 @@ func DBStatus(app clio.Application) *cobra.Command { RunE: func(_ *cobra.Command, _ []string) error { return runDBStatus(*opts) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *dbStatusOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBStatus(opts dbStatusOptions) error { diff --git a/cmd/grype/cli/commands/db_update.go b/cmd/grype/cli/commands/db_update.go index aa153aeaf67..6f92de5dd9d 100644 --- a/cmd/grype/cli/commands/db_update.go +++ b/cmd/grype/cli/commands/db_update.go @@ -17,7 +17,7 @@ import ( func DBUpdate(app clio.Application) *cobra.Command { opts := dbOptionsDefault(app.ID()) - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "update", Short: "download the latest vulnerability database", Args: cobra.ExactArgs(0), @@ -29,7 +29,14 @@ func DBUpdate(app clio.Application) *cobra.Command { RunE: func(_ *cobra.Command, _ []string) error { return runDBUpdate(opts.DB, opts.Experimental.DBv6) }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *DBOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } func runDBUpdate(opts options.Database, expUseV6 bool) error { diff --git a/cmd/grype/cli/commands/explain.go b/cmd/grype/cli/commands/explain.go index 6770a17f468..36031b46654 100644 --- a/cmd/grype/cli/commands/explain.go +++ b/cmd/grype/cli/commands/explain.go @@ -27,7 +27,7 @@ func (d *explainOptions) AddFlags(flags clio.FlagSet) { func Explain(app clio.Application) *cobra.Command { opts := &explainOptions{} - return app.SetupCommand(&cobra.Command{ + cmd := &cobra.Command{ Use: "explain --id [VULNERABILITY ID]", Short: "Ask grype to explain a set of findings", PreRunE: disableUI(app), @@ -53,5 +53,12 @@ func Explain(app clio.Application) *cobra.Command { // TODO: implement return fmt.Errorf("requires grype json on stdin, please run 'grype -o json ... | grype explain ...'") }, - }, opts) + } + + // prevent from being shown in the grype config + type configWrapper struct { + Opts *explainOptions `json:"-" yaml:"-" mapstructure:"-"` + } + + return app.SetupCommand(cmd, &configWrapper{opts}) } From 2f03aff9c48ef0052c673d93503f01dbe0cd8b83 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 8 Jan 2025 14:08:47 -0500 Subject: [PATCH 2/2] expose DB options Signed-off-by: Alex Goodman --- cmd/grype/cli/commands/db_check.go | 5 +++-- cmd/grype/cli/commands/db_delete.go | 2 +- cmd/grype/cli/commands/db_diff.go | 5 +++-- cmd/grype/cli/commands/db_import.go | 2 +- cmd/grype/cli/commands/db_list.go | 5 +++-- cmd/grype/cli/commands/db_providers.go | 5 +++-- cmd/grype/cli/commands/db_search.go | 5 +++-- cmd/grype/cli/commands/db_status.go | 5 +++-- cmd/grype/cli/commands/db_update.go | 2 +- 9 files changed, 21 insertions(+), 15 deletions(-) diff --git a/cmd/grype/cli/commands/db_check.go b/cmd/grype/cli/commands/db_check.go index 175b27fa974..b9bf16a49e9 100644 --- a/cmd/grype/cli/commands/db_check.go +++ b/cmd/grype/cli/commands/db_check.go @@ -52,10 +52,11 @@ func DBCheck(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *dbCheckOptions `json:"-" yaml:"-" mapstructure:"-"` + Hidden *dbCheckOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } - return app.SetupCommand(cmd, &configWrapper{opts}) + return app.SetupCommand(cmd, &configWrapper{Hidden: opts, DBOptions: &opts.DBOptions}) } func runDBCheck(opts dbCheckOptions) error { diff --git a/cmd/grype/cli/commands/db_delete.go b/cmd/grype/cli/commands/db_delete.go index 5a0ea8be3bc..185a90134b4 100644 --- a/cmd/grype/cli/commands/db_delete.go +++ b/cmd/grype/cli/commands/db_delete.go @@ -27,7 +27,7 @@ func DBDelete(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *DBOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } return app.SetupCommand(cmd, &configWrapper{opts}) diff --git a/cmd/grype/cli/commands/db_diff.go b/cmd/grype/cli/commands/db_diff.go index 53910d1edd3..67d00a397a5 100644 --- a/cmd/grype/cli/commands/db_diff.go +++ b/cmd/grype/cli/commands/db_diff.go @@ -65,10 +65,11 @@ func DBDiff(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *dbDiffOptions `json:"-" yaml:"-" mapstructure:"-"` + Hidden *dbDiffOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } - return app.SetupCommand(cmd, &configWrapper{opts}) + return app.SetupCommand(cmd, &configWrapper{Hidden: opts, DBOptions: &opts.DBOptions}) } func runDBDiff(opts *dbDiffOptions, base string, target string) (errs error) { diff --git a/cmd/grype/cli/commands/db_import.go b/cmd/grype/cli/commands/db_import.go index 0112a2db53c..7e774dc10d7 100644 --- a/cmd/grype/cli/commands/db_import.go +++ b/cmd/grype/cli/commands/db_import.go @@ -29,7 +29,7 @@ func DBImport(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *DBOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } return app.SetupCommand(cmd, &configWrapper{opts}) diff --git a/cmd/grype/cli/commands/db_list.go b/cmd/grype/cli/commands/db_list.go index e03ffb89be0..b64c564c2e8 100644 --- a/cmd/grype/cli/commands/db_list.go +++ b/cmd/grype/cli/commands/db_list.go @@ -44,10 +44,11 @@ func DBList(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *dbListOptions `json:"-" yaml:"-" mapstructure:"-"` + Hidden *dbListOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } - return app.SetupCommand(cmd, &configWrapper{opts}) + return app.SetupCommand(cmd, &configWrapper{Hidden: opts, DBOptions: &opts.DBOptions}) } func runDBList(opts dbListOptions) error { diff --git a/cmd/grype/cli/commands/db_providers.go b/cmd/grype/cli/commands/db_providers.go index 330b2d8cd19..4d24d3d78c4 100644 --- a/cmd/grype/cli/commands/db_providers.go +++ b/cmd/grype/cli/commands/db_providers.go @@ -48,10 +48,11 @@ func DBProviders(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *dbProvidersOptions `json:"-" yaml:"-" mapstructure:"-"` + Hidden *dbProvidersOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } - return app.SetupCommand(cmd, &configWrapper{opts}) + return app.SetupCommand(cmd, &configWrapper{Hidden: opts, DBOptions: &opts.DBOptions}) } func runDBProviders(opts *dbProvidersOptions, app clio.Application) error { diff --git a/cmd/grype/cli/commands/db_search.go b/cmd/grype/cli/commands/db_search.go index 51cac7b9221..015fb329110 100644 --- a/cmd/grype/cli/commands/db_search.go +++ b/cmd/grype/cli/commands/db_search.go @@ -48,10 +48,11 @@ func DBSearch(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *dbQueryOptions `json:"-" yaml:"-" mapstructure:"-"` + Hidden *dbQueryOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } - return app.SetupCommand(cmd, &configWrapper{opts}) + return app.SetupCommand(cmd, &configWrapper{Hidden: opts, DBOptions: &opts.DBOptions}) } func runDBSearch(opts dbQueryOptions, vulnerabilityID string) error { diff --git a/cmd/grype/cli/commands/db_status.go b/cmd/grype/cli/commands/db_status.go index 5c19beb7f79..0b91589ec95 100644 --- a/cmd/grype/cli/commands/db_status.go +++ b/cmd/grype/cli/commands/db_status.go @@ -44,10 +44,11 @@ func DBStatus(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *dbStatusOptions `json:"-" yaml:"-" mapstructure:"-"` + Hidden *dbStatusOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } - return app.SetupCommand(cmd, &configWrapper{opts}) + return app.SetupCommand(cmd, &configWrapper{Hidden: opts, DBOptions: &opts.DBOptions}) } func runDBStatus(opts dbStatusOptions) error { diff --git a/cmd/grype/cli/commands/db_update.go b/cmd/grype/cli/commands/db_update.go index 6f92de5dd9d..7a0f1bc6d6e 100644 --- a/cmd/grype/cli/commands/db_update.go +++ b/cmd/grype/cli/commands/db_update.go @@ -33,7 +33,7 @@ func DBUpdate(app clio.Application) *cobra.Command { // prevent from being shown in the grype config type configWrapper struct { - Opts *DBOptions `json:"-" yaml:"-" mapstructure:"-"` + *DBOptions `yaml:",inline" mapstructure:",squash"` } return app.SetupCommand(cmd, &configWrapper{opts})