1- from typing import Type , NamedTuple
2-
31import click
42import json
53import logging
1412
1513from halo import Halo
1614
17- from cycode .cli .printers import ResultsPrinter
18- from cycode .cli .models import Document , DocumentDetections , Severity
15+ from cycode .cli .printers import ConsolePrinter
16+ from cycode .cli .models import Document , DocumentDetections , Severity , CliError , CliErrors
1917from cycode .cli .ci_integrations import get_commit_range
2018from cycode .cli .consts import *
2119from cycode .cli .config import configuration_manager
@@ -446,14 +444,15 @@ def print_scan_details(scan_details_response: ScanDetailsResponse):
446444 if scan_details_response .message is not None :
447445 logger .info (f"Scan message: { scan_details_response .message } " )
448446
449- def print_results ( context : click . Context , document_detections_list : List [ DocumentDetections ]):
450- output_type = context . obj [ 'output' ]
451- printer = ResultsPrinter ( )
452- printer .print_results ( context , document_detections_list , output_type )
447+
448+ def print_results ( context : click . Context , document_detections_list : List [ DocumentDetections ]) -> None :
449+ printer = ConsolePrinter ( context )
450+ printer .print_scan_results ( document_detections_list )
453451
454452
455- def enrich_scan_result (scan_result : ZippedFileScanResult , documents_to_scan : List [Document ]) -> \
456- List [DocumentDetections ]:
453+ def enrich_scan_result (
454+ scan_result : ZippedFileScanResult , documents_to_scan : List [Document ]
455+ ) -> List [DocumentDetections ]:
457456 logger .debug ('enriching scan result' )
458457 document_detections_list = []
459458 for detections_per_file in scan_result .detections_per_file :
@@ -819,49 +818,39 @@ def _is_subpath_of_cycode_configuration_folder(filename: str) -> bool:
819818 or filename .endswith (ConfigFileManager .get_config_file_route ())
820819
821820
822- class CliScanError (NamedTuple ):
823- soft_fail : bool
824- code : str
825- message : str
826-
827-
828- CliScanErrors = Dict [Type [Exception ], CliScanError ]
829-
830-
831821def _handle_exception (context : click .Context , e : Exception ):
832822 context .obj ['did_fail' ] = True
833823
834824 if context .obj ['verbose' ]:
835825 click .secho (f'Error: { traceback .format_exc ()} ' , fg = 'red' , nl = False )
836826
837- # TODO(MarshalX): Create global CLI errors database and move this
838- errors : CliScanErrors = {
839- NetworkError : CliScanError (
827+ errors : CliErrors = {
828+ NetworkError : CliError (
840829 soft_fail = True ,
841830 code = 'cycode_error' ,
842831 message = 'Cycode was unable to complete this scan. '
843832 'Please try again by executing the `cycode scan` command'
844833 ),
845- ScanAsyncError : CliScanError (
834+ ScanAsyncError : CliError (
846835 soft_fail = True ,
847836 code = 'scan_error' ,
848837 message = 'Cycode was unable to complete this scan. '
849838 'Please try again by executing the `cycode scan` command'
850839 ),
851- HttpUnauthorizedError : CliScanError (
840+ HttpUnauthorizedError : CliError (
852841 soft_fail = True ,
853842 code = 'auth_error' ,
854843 message = 'Unable to authenticate to Cycode, your token is either invalid or has expired. '
855844 'Please re-generate your token and reconfigure it by running the `cycode configure` command'
856845 ),
857- ZipTooLargeError : CliScanError (
846+ ZipTooLargeError : CliError (
858847 soft_fail = True ,
859848 code = 'zip_too_large_error' ,
860849 message = 'The path you attempted to scan exceeds the current maximum scanning size cap (10MB). '
861850 'Please try ignoring irrelevant paths using the ‘cycode ignore --by-path’ command '
862851 'and execute the scan again'
863852 ),
864- InvalidGitRepositoryError : CliScanError (
853+ InvalidGitRepositoryError : CliError (
865854 soft_fail = False ,
866855 code = 'invalid_git_error' ,
867856 message = 'The path you supplied does not correlate to a git repository. '
@@ -875,22 +864,14 @@ def _handle_exception(context: click.Context, e: Exception):
875864 if error .soft_fail is True :
876865 context .obj ['soft_fail' ] = True
877866
878- return _print_error (context , error )
867+ return ConsolePrinter (context ). print_error ( error )
879868
880869 if isinstance (e , click .ClickException ):
881870 raise e
882871
883872 raise click .ClickException (str (e ))
884873
885874
886- def _print_error (context : click .Context , error : CliScanError ) -> None :
887- # TODO(MarshalX): Extend functionality of CLI printers and move this
888- if context .obj ['output' ] == 'text' :
889- click .secho (error .message , fg = 'red' , nl = False )
890- elif context .obj ['output' ] == 'json' :
891- click .echo (json .dumps ({'error' : error .code , 'message' : error .message }, ensure_ascii = False ))
892-
893-
894875def _report_scan_status (context : click .Context , scan_type : str , scan_id : str , scan_completed : bool ,
895876 output_detections_count : int , all_detections_count : int , files_to_scan_count : int ,
896877 zip_size : int , command_scan_type : str , error_message : Optional [str ]):
0 commit comments