diff --git a/pkg/commands/artifact/option.go b/pkg/commands/artifact/option.go index 95c817c478..39a7a5b3b7 100644 --- a/pkg/commands/artifact/option.go +++ b/pkg/commands/artifact/option.go @@ -56,6 +56,12 @@ func (c *Option) Init() error { return err } + // "--list-all-pkgs" option is unavailable with "--format table". + // If user specifies "--list-all-pkgs" with "--format table", we should warn it. + if c.ListAllPkgs && c.Format != "json" { + c.Logger.Warn(`"--list-all-pkgs" cannot be used with "--format table". Try "--format json" or other formats.`) + } + return nil } diff --git a/pkg/commands/artifact/option_test.go b/pkg/commands/artifact/option_test.go index 4ed00fa290..ad3bf0a4d0 100644 --- a/pkg/commands/artifact/option_test.go +++ b/pkg/commands/artifact/option_test.go @@ -93,6 +93,43 @@ func TestOption_Init(t *testing.T) { }, }, }, + { + name: "happy path with list-all-pkgs warning", + args: []string{"--format", "table", "--list-all-pkgs", "centos:7"}, + logs: []string{ + `"--list-all-pkgs" cannot be used with "--format table". Try "--format json" or other formats.`, + }, + want: Option{ + ReportOption: option.ReportOption{ + Severities: []dbTypes.Severity{dbTypes.SeverityCritical}, + Format: "table", + Output: os.Stdout, + VulnType: []string{types.VulnTypeOS, types.VulnTypeLibrary}, + SecurityChecks: []string{types.SecurityCheckVulnerability}, + ListAllPkgs: true, + }, + ArtifactOption: option.ArtifactOption{ + Target: "centos:7", + }, + }, + }, + { + name: "happy path without list-all-pkgs warning", + args: []string{"--format", "json", "--list-all-pkgs", "centos:7"}, + want: Option{ + ReportOption: option.ReportOption{ + Severities: []dbTypes.Severity{dbTypes.SeverityCritical}, + Format: "json", + Output: os.Stdout, + VulnType: []string{types.VulnTypeOS, types.VulnTypeLibrary}, + SecurityChecks: []string{types.SecurityCheckVulnerability}, + ListAllPkgs: true, + }, + ArtifactOption: option.ArtifactOption{ + Target: "centos:7", + }, + }, + }, { name: "invalid option combination: --template enabled without --format", args: []string{"--template", "@contrib/gitlab.tpl", "gitlab/gitlab-ce:12.7.2-ce.0"}, @@ -177,6 +214,7 @@ func TestOption_Init(t *testing.T) { set.Bool("reset", false, "") set.Bool("skip-db-update", false, "") set.Bool("download-db-only", false, "") + set.Bool("list-all-pkgs", false, "") set.String("severity", "CRITICAL", "") set.String("vuln-type", "os,library", "") set.String("security-checks", "vuln", "") diff --git a/pkg/commands/client/option.go b/pkg/commands/client/option.go index 150204ff2a..278c602561 100644 --- a/pkg/commands/client/option.go +++ b/pkg/commands/client/option.go @@ -77,6 +77,12 @@ func (c *Option) Init() (err error) { return err } + // "--list-all-pkgs" option is unavailable with "--format table". + // If user specifies "--list-all-pkgs" with "--format table", we should warn it. + if c.ListAllPkgs && c.Format == "table" { + c.Logger.Warn(`"--list-all-pkgs" cannot be used with "--format table". Try "--format json" or other formats.`) + } + return nil } diff --git a/pkg/commands/client/option_test.go b/pkg/commands/client/option_test.go index 491ebe07ca..fc67a12f35 100644 --- a/pkg/commands/client/option_test.go +++ b/pkg/commands/client/option_test.go @@ -138,6 +138,45 @@ func TestConfig_Init(t *testing.T) { CustomHeaders: http.Header{}, }, }, + { + name: "happy path with list-all-pkgs warning", + args: []string{"--format", "table", "--list-all-pkgs", "centos:7"}, + logs: []string{ + `"--list-all-pkgs" cannot be used with "--format table". Try "--format json" or other formats.`, + }, + want: Option{ + ReportOption: option.ReportOption{ + Severities: []dbTypes.Severity{dbTypes.SeverityCritical}, + Format: "table", + Output: os.Stdout, + VulnType: []string{types.VulnTypeOS, types.VulnTypeLibrary}, + SecurityChecks: []string{types.SecurityCheckVulnerability}, + ListAllPkgs: true, + }, + ArtifactOption: option.ArtifactOption{ + Target: "centos:7", + }, + CustomHeaders: http.Header{}, + }, + }, + { + name: "happy path without list-all-pkgs warning", + args: []string{"--format", "json", "--list-all-pkgs", "centos:7"}, + want: Option{ + ReportOption: option.ReportOption{ + Severities: []dbTypes.Severity{dbTypes.SeverityCritical}, + Format: "json", + Output: os.Stdout, + VulnType: []string{types.VulnTypeOS, types.VulnTypeLibrary}, + SecurityChecks: []string{types.SecurityCheckVulnerability}, + ListAllPkgs: true, + }, + ArtifactOption: option.ArtifactOption{ + Target: "centos:7", + }, + CustomHeaders: http.Header{}, + }, + }, { name: "invalid option combination: --template enabled without --format", args: []string{"--template", "@contrib/gitlab.tpl", "gitlab/gitlab-ce:12.7.2-ce.0"}, @@ -238,6 +277,7 @@ func TestConfig_Init(t *testing.T) { set.Bool("quiet", false, "") set.Bool("no-progress", false, "") set.Bool("clear-cache", false, "") + set.Bool("list-all-pkgs", false, "") set.String("severity", "CRITICAL", "") set.String("vuln-type", "os,library", "") set.String("security-checks", "vuln", "")