22
33import click
44import cloudsmith_api
5+ from rich .console import Console
6+ from rich .table import Table
57
68from ...cli import utils
79from .. import ratelimits
810from .exceptions import catch_raise_api_exception
911from .init import get_api_client
1012
13+ # Severity color mapping for consistent styling
14+ SEVERITY_COLORS = {
15+ "critical" : "red" ,
16+ "high" : "bright_red" ,
17+ "medium" : "yellow" ,
18+ "low" : "blue" ,
19+ "unknown" : "dim white" ,
20+ }
21+
1122
1223def get_vulnerabilities_api ():
1324 """Get the vulnerabilities API client."""
1425 return get_api_client (cloudsmith_api .VulnerabilitiesApi )
1526
1627
28+ def _colorize_count (count , severity_key ):
29+ """Return a rich-styled count string, colored only when count > 0."""
30+ if count > 0 :
31+ color = SEVERITY_COLORS .get (severity_key , "white" )
32+ return f"[{ color } ]{ count } [/{ color } ]"
33+ return f"[dim]{ count } [/dim]"
34+
35+
1736def _print_vulnerabilities_summary_table (data , severity_filter , total_filtered_vulns ):
18- """Print vulnerabilities as a table."""
37+ """Print vulnerabilities as a color-coded table."""
1938
2039 severity_keys = {
2140 "Critical" : "critical" ,
@@ -29,10 +48,6 @@ def _print_vulnerabilities_summary_table(data, severity_filter, total_filtered_v
2948 allowed = [s .strip ().lower () for s in severity_filter .split ("," )]
3049 severity_keys = {k : v for k , v in severity_keys .items () if v in allowed }
3150
32- headers = [{"header" : "Package" , "justify" : "left" , "style" : "cyan" }]
33- for key in severity_keys .keys ():
34- headers .append ({"header" : key , "justify" : "center" , "style" : "white" })
35-
3651 # Get package name and version for the target label
3752 pkg_data = getattr (data , "package" , None )
3853 pkg_name = getattr (pkg_data , "name" , "Unknown" )
@@ -53,29 +68,43 @@ def _print_vulnerabilities_summary_table(data, severity_filter, total_filtered_v
5368 elif "unknown" in counts :
5469 counts ["unknown" ] += 1
5570
56- # Create the single summary row
57- row = [target_label ]
58- for _header , key in severity_keys .items ():
59- row .append (str (counts [key ]))
60-
61- rows = [row ]
62-
63- click .echo ()
64- click .echo ()
65-
66- utils .rich_print_table (headers = headers , rows = rows , title = "Vulnerabilities Summary" )
71+ # Build the rich table
72+ console = Console ()
73+ table = Table (
74+ title = "Vulnerabilities Summary" ,
75+ show_header = True ,
76+ header_style = "bold" ,
77+ show_lines = True ,
78+ border_style = "bright_black" ,
79+ padding = (0 , 1 ),
80+ )
81+
82+ table .add_column ("Package" , justify = "left" , style = "cyan" , no_wrap = True )
83+ for display_name , sev_key in severity_keys .items ():
84+ color = SEVERITY_COLORS .get (sev_key , "white" )
85+ table .add_column (display_name , justify = "center" , header_style = f"bold { color } " )
86+ table .add_column ("Total" , justify = "center" , header_style = "bold white" )
87+
88+ # Build the row
89+ row_total = 0
90+ cells = [target_label ]
91+ for _display , sev_key in severity_keys .items ():
92+ count = counts .get (sev_key , 0 )
93+ cells .append (_colorize_count (count , sev_key ))
94+ row_total += count
95+ total_style = "[bold red]" if row_total > 0 else "[dim]"
96+ cells .append (f"{ total_style } { row_total } [/]" )
97+ table .add_row (* cells )
98+
99+ console .print ()
100+ console .print (table )
67101
68102 if severity_filter :
69- filters = severity_filter .upper ()
70- click .echo (
71- f"\n Total Vulnerabilities: { getattr (data , 'num_vulnerabilities' , 0 )} "
103+ console .print (
104+ f"\n Filtered Vulnerabilities: [bold]{ total_filtered_vulns } [/bold]\n "
72105 )
73- click .echo (f"\n Total { filters } Vulnerabilities: { total_filtered_vulns } " )
74106 else :
75- click .echo (
76- f"\n Total Vulnerabilities: { getattr (data , 'num_vulnerabilities' , 0 )} "
77- )
78- click .echo ()
107+ console .print (f"\n Total Vulnerabilities: [bold]{ row_total } [/bold]\n " )
79108
80109
81110def _print_vulnerabilities_assessment_table (data , severity_filter = None ):
0 commit comments