From f5867ce0b66a364df45962f4ebfaf448c86fcf95 Mon Sep 17 00:00:00 2001 From: Tanmay Naik Date: Fri, 4 Sep 2020 13:56:47 -0400 Subject: [PATCH] cli: group CVEs by severity --- pkg/cli/service.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/cli/service.go b/pkg/cli/service.go index 938b560c..b8d59d50 100644 --- a/pkg/cli/service.go +++ b/pkg/cli/service.go @@ -311,6 +311,8 @@ func (service searchService) getCveByImage(ctx context.Context, config searchCon return } + result.Data.CVEListForImage.CVEList = groupCVEsBySeverity(result.Data.CVEListForImage.CVEList) + str, err := result.string(*config.outputFormat) if err != nil { if isContextDone(ctx) { @@ -327,6 +329,27 @@ func (service searchService) getCveByImage(ctx context.Context, config searchCon c <- stringResult{str, nil} } +func groupCVEsBySeverity(cveList []cve) []cve { + high := make([]cve, 0) + med := make([]cve, 0) + low := make([]cve, 0) + + for _, cve := range cveList { + switch cve.Severity { + case "LOW": + low = append(low, cve) + + case "MEDIUM": + med = append(med, cve) + + case "HIGH": + high = append(high, cve) + } + } + + return append(append(high, med...), low...) +} + func isContextDone(ctx context.Context) bool { select { case <-ctx.Done():