|  | 
|  | 1 | +import dataclasses | 
|  | 2 | +import json | 
|  | 3 | +import platform | 
|  | 4 | +from typing import Dict | 
|  | 5 | + | 
|  | 6 | +import click | 
|  | 7 | + | 
|  | 8 | +from cycode import __version__ | 
|  | 9 | +from cycode.cli.commands.auth_common import get_authorization_info | 
|  | 10 | +from cycode.cli.consts import PROGRAM_NAME | 
|  | 11 | +from 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 | 
|  | 57 | + | 
|  | 58 | + | 
|  | 59 | +@dataclasses.dataclass | 
|  | 60 | +class CliStatus(CliStatusBase): | 
|  | 61 | +    program: str | 
|  | 62 | +    version: str | 
|  | 63 | +    os: str | 
|  | 64 | +    arch: str | 
|  | 65 | +    python_version: str | 
|  | 66 | +    installation_id: str | 
|  | 67 | +    app_url: str | 
|  | 68 | +    api_url: str | 
|  | 69 | +    is_authenticated: bool | 
|  | 70 | +    user_id: str = None | 
|  | 71 | +    tenant_id: str = None | 
|  | 72 | +    supported_modules: CliSupportedModulesStatus = None | 
|  | 73 | + | 
|  | 74 | + | 
|  | 75 | +def get_cli_status() -> CliStatus: | 
|  | 76 | +    configuration_manager = ConfigurationManager() | 
|  | 77 | + | 
|  | 78 | +    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() | 
|  | 86 | + | 
|  | 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) | 
|  | 94 | + | 
|  | 95 | +    return CliStatus( | 
|  | 96 | +        program=PROGRAM_NAME, | 
|  | 97 | +        version=__version__, | 
|  | 98 | +        os=platform.system(), | 
|  | 99 | +        arch=platform.machine(), | 
|  | 100 | +        python_version=platform.python_version(), | 
|  | 101 | +        installation_id=configuration_manager.get_or_create_installation_id(), | 
|  | 102 | +        app_url=configuration_manager.get_cycode_app_url(), | 
|  | 103 | +        api_url=configuration_manager.get_cycode_api_url(), | 
|  | 104 | +        is_authenticated=is_authenticated, | 
|  | 105 | +        user_id=auth_info.user_id if auth_info else None, | 
|  | 106 | +        tenant_id=auth_info.tenant_id if auth_info else None, | 
|  | 107 | +        supported_modules=supported_modules_status, | 
|  | 108 | +    ) | 
|  | 109 | + | 
|  | 110 | + | 
|  | 111 | +@click.command(short_help='Show the CLI status and exit.') | 
|  | 112 | +@click.pass_context | 
|  | 113 | +def status_command(context: click.Context) -> None: | 
|  | 114 | +    output = context.obj['output'] | 
|  | 115 | + | 
|  | 116 | +    status = get_cli_status() | 
|  | 117 | +    message = status.as_text() | 
|  | 118 | +    if output == 'json': | 
|  | 119 | +        message = status.as_json() | 
|  | 120 | + | 
|  | 121 | +    click.echo(message, color=context.color) | 
|  | 122 | +    context.exit() | 
0 commit comments