11import dataclasses
22import json
33import platform
4+ from typing import Dict
45
56import click
67
78from cycode import __version__
89from cycode .cli .commands .auth_common import get_authorization_info
910from cycode .cli .consts import PROGRAM_NAME
1011from cycode .cli .user_settings .configuration_manager import ConfigurationManager
12+ from cycode .cli .utils .get_api_client import get_scan_cycode_client
13+ from cycode .cyclient import logger
14+
15+
16+ class CliStatusBase :
17+ def as_dict (self ) -> Dict [str , any ]:
18+ return dataclasses .asdict (self )
19+
20+ def _get_text_message_part (self , key : str , value : any , intent : int = 0 ) -> str :
21+ message_parts = []
22+
23+ intent_prefix = ' ' * intent * 2
24+ human_readable_key = key .replace ('_' , ' ' ).capitalize ()
25+
26+ if isinstance (value , dict ):
27+ message_parts .append (f'{ intent_prefix } { human_readable_key } :' )
28+ for sub_key , sub_value in value .items ():
29+ message_parts .append (self ._get_text_message_part (sub_key , sub_value , intent = intent + 1 ))
30+ elif isinstance (value , (list , set , tuple )):
31+ message_parts .append (f'{ intent_prefix } { human_readable_key } :' )
32+ for index , sub_value in enumerate (value ):
33+ message_parts .append (self ._get_text_message_part (f'#{ index } ' , sub_value , intent = intent + 1 ))
34+ else :
35+ message_parts .append (f'{ intent_prefix } { human_readable_key } : { value } ' )
36+
37+ return '\n ' .join (message_parts )
38+
39+ def as_text (self ) -> str :
40+ message_parts = []
41+ for key , value in self .as_dict ().items ():
42+ message_parts .append (self ._get_text_message_part (key , value ))
43+
44+ return '\n ' .join (message_parts )
45+
46+ def as_json (self ) -> str :
47+ return json .dumps (self .as_dict ())
48+
49+
50+ @dataclasses .dataclass
51+ class CliSupportedModulesStatus (CliStatusBase ):
52+ secret_scanning : bool = False
53+ sca_scanning : bool = False
54+ iac_scanning : bool = False
55+ sast_scanning : bool = False
56+ ai_large_language_model : bool = False
1157
1258
1359@dataclasses .dataclass
14- class Status :
60+ class CliStatus ( CliStatusBase ) :
1561 program : str
1662 version : str
1763 os : str
@@ -23,26 +69,30 @@ class Status:
2369 is_authenticated : bool
2470 user_id : str = None
2571 tenant_id : str = None
72+ supported_modules : CliSupportedModulesStatus = None
2673
27- def as_text (self ) -> str :
28- message_parts = []
29- for key , value in dataclasses .asdict (self ).items ():
30- human_readable_key = key .replace ('_' , ' ' ).capitalize ()
31- message_parts .append (f'{ human_readable_key } : { value } ' )
3274
33- return '\n ' .join (message_parts )
34-
35- def as_json (self ) -> str :
36- return json .dumps (dataclasses .asdict (self ))
37-
38-
39- def get_cli_status () -> Status :
75+ def get_cli_status () -> CliStatus :
4076 configuration_manager = ConfigurationManager ()
77+
4178 auth_info = get_authorization_info ()
79+ is_authenticated = auth_info is not None
80+
81+ supported_modules_status = CliSupportedModulesStatus ()
82+ if is_authenticated :
83+ try :
84+ client = get_scan_cycode_client ()
85+ supported_modules_preferences = client .get_supported_modules_preferences ()
4286
43- # TODO: Add supported modules; add AI status
87+ supported_modules_status .secret_scanning = supported_modules_preferences .secret_scanning
88+ supported_modules_status .sca_scanning = supported_modules_preferences .sca_scanning
89+ supported_modules_status .iac_scanning = supported_modules_preferences .iac_scanning
90+ supported_modules_status .sast_scanning = supported_modules_preferences .sast_scanning
91+ supported_modules_status .ai_large_language_model = supported_modules_preferences .ai_large_language_model
92+ except Exception as e :
93+ logger .debug ('Failed to get supported modules preferences' , exc_info = e )
4494
45- return Status (
95+ return CliStatus (
4696 program = PROGRAM_NAME ,
4797 version = __version__ ,
4898 os = platform .system (),
@@ -51,9 +101,10 @@ def get_cli_status() -> Status:
51101 installation_id = configuration_manager .get_or_create_installation_id (),
52102 app_url = configuration_manager .get_cycode_app_url (),
53103 api_url = configuration_manager .get_cycode_api_url (),
54- is_authenticated = auth_info is not None ,
104+ is_authenticated = is_authenticated ,
55105 user_id = auth_info .user_id if auth_info else None ,
56106 tenant_id = auth_info .tenant_id if auth_info else None ,
107+ supported_modules = supported_modules_status ,
57108 )
58109
59110
0 commit comments