mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-12 15:50:15 -08:00
fix(option): warn list-all-pkgs only with the table format (#1755)
This commit is contained in:
@@ -9,7 +9,7 @@ linters-settings:
|
||||
revive:
|
||||
ignore-generated-header: true
|
||||
gocyclo:
|
||||
min-complexity: 10
|
||||
min-complexity: 20
|
||||
dupl:
|
||||
threshold: 100
|
||||
goconst:
|
||||
|
||||
@@ -56,12 +56,6 @@ 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
|
||||
}
|
||||
|
||||
|
||||
@@ -93,43 +93,6 @@ 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"},
|
||||
@@ -214,7 +177,6 @@ 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", "")
|
||||
|
||||
@@ -77,12 +77,6 @@ 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
|
||||
}
|
||||
|
||||
|
||||
@@ -138,45 +138,6 @@ 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"},
|
||||
@@ -277,7 +238,6 @@ 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", "")
|
||||
|
||||
@@ -70,6 +70,12 @@ func (c *ReportOption) Init(output io.Writer, logger *zap.SugaredLogger) error {
|
||||
}
|
||||
}
|
||||
|
||||
// "--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" {
|
||||
logger.Warn(`"--list-all-pkgs" cannot be used with "--format table". Try "--format json" or other formats.`)
|
||||
}
|
||||
|
||||
if c.forceListAllPkgs(logger) {
|
||||
c.ListAllPkgs = true
|
||||
}
|
||||
|
||||
@@ -177,6 +177,28 @@ func TestReportReportConfig_Init(t *testing.T) {
|
||||
SecurityChecks: []string{types.SecurityCheckVulnerability},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid option combination: --list-all-pkgs with --format table",
|
||||
fields: fields{
|
||||
Format: "table",
|
||||
severities: "LOW",
|
||||
vulnType: "os",
|
||||
securityChecks: "vuln",
|
||||
listAllPksgs: true,
|
||||
},
|
||||
args: []string{"centos:7"},
|
||||
logs: []string{
|
||||
`"--list-all-pkgs" cannot be used with "--format table". Try "--format json" or other formats.`,
|
||||
},
|
||||
want: ReportOption{
|
||||
Format: "table",
|
||||
Output: os.Stdout,
|
||||
Severities: []dbTypes.Severity{dbTypes.SeverityLow},
|
||||
VulnType: []string{types.VulnTypeOS},
|
||||
SecurityChecks: []string{types.SecurityCheckVulnerability},
|
||||
ListAllPkgs: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user