@@ -61,22 +61,46 @@ def image_for_severity_count(severity: str, count: int) -> str:
6161 "JSON_FILE" ,
6262 type = click .Path (exists = True , dir_okay = False , file_okay = True , readable = True , resolve_path = True ),
6363)
64+ @click .option (
65+ "--severity" ,
66+ type = click .Choice (list (COLORS .keys ()), case_sensitive = False ),
67+ help = "Request a single severity group only. This results in explicit zero counts." ,
68+ )
69+ @click .option (
70+ "--all" ,
71+ "all_" ,
72+ is_flag = True ,
73+ help = "Will explicitly extract all known severity groups, even zero counts." ,
74+ )
6475@click .option (
6576 "--replace" ,
6677 is_flag = True ,
67- show_default = True ,
6878 help = "Replace items in case they already exists." ,
6979)
7080@click .pass_obj
71- def trivy_scan_command (app : ApplicationContext , json_file : str , replace : bool ) -> None :
72- """Extract metadata from a trivy scan JSON output file."""
81+ def trivy_scan_command (
82+ app : ApplicationContext , json_file : str , severity : str , all_ : bool , replace : bool
83+ ) -> None :
84+ """Extract metadata from a trivy scan JSON output file.
85+
86+ This command will extract counts of vulnerabilities, grouped by
87+ severity. Per default, only severity groups with at least one
88+ vulnerability will be extracted. If you need explicit zero counts,
89+ use `--severity` or `--all`.
90+ """
7391 counter = count_json_file (json_file = json_file )
74- for severity , count in counter .items ():
75- key = f"trivy-scan-{ severity .lower ()} "
92+ severities = [severity ] if severity else counter .keys ()
93+ if all_ :
94+ severities = list (COLORS .keys ())
95+ severities .extend (counter .keys ())
96+ severities = list (set (severities ))
97+ for _ in severities :
98+ count = counter .get (_ , 0 )
99+ key = f"trivy-scan-{ _ .lower ()} "
76100 new_item = Item (
77101 value = str (count ),
78- label = severity ,
79- description = f"Count of found vulnerabilities with severity '{ severity } '" ,
80- image = image_for_severity_count (severity = severity , count = count ),
102+ label = _ ,
103+ description = f"Count of found vulnerabilities with severity '{ _ } '" ,
104+ image = image_for_severity_count (severity = _ , count = count ),
81105 )
82106 app .add_item (key = key , item = new_item , replace = replace )
0 commit comments