| 35 | |
| 36 | |
| 37 | class PAMDebugDumpCommand(Command): |
| 38 | parser = argparse.ArgumentParser( |
| 39 | prog='pam action debug dump', |
| 40 | description='Dump folder records and GraphSync data as JSON.') |
| 41 | parser.add_argument('folder_uid', action='store', |
| 42 | help='Folder UID or path. Use empty string for the root folder.') |
| 43 | parser.add_argument('--recursive', '-r', required=False, dest='recursive', action='store_true', |
| 44 | help='Include records in all subfolders.') |
| 45 | parser.add_argument('--format', choices=['json'], |
| 46 | help='Emit JSON to stdout (or the Service Mode API response).') |
| 47 | parser.add_argument('--save-as', '-s', required=False, dest='save_as', action='store', |
| 48 | help='Output file path to save JSON results.') |
| 49 | |
| 50 | def get_parser(self): |
| 51 | return PAMDebugDumpCommand.parser |
| 52 | |
| 53 | def execute(self, params: 'KeeperParams', **kwargs): |
| 54 | folder_uid_arg = kwargs.get('folder_uid', '') |
| 55 | recursive = kwargs.get('recursive', False) |
| 56 | save_as = kwargs.get('save_as') |
| 57 | output_format = kwargs.get('format') |
| 58 | |
| 59 | from ...importer.imp_exp import is_export_restricted |
| 60 | if is_export_restricted(params): |
| 61 | raise CommandError( |
| 62 | 'pam action debug dump', |
| 63 | 'PAM debug dump is disabled by enterprise export restrictions.', |
| 64 | ) |
| 65 | |
| 66 | if getattr(params, 'service_mode', False): |
| 67 | if save_as is not None or output_format != 'json': |
| 68 | raise CommandError( |
| 69 | 'pam action debug dump', |
| 70 | 'Service Mode allows only JSON output in the API response; ' |
| 71 | 'do not specify --save-as/-s.', |
| 72 | ) |
| 73 | folder_cache = getattr(params, 'folder_cache', {}) or {} |
| 74 | nsf_folders = getattr(params, 'nested_share_folders', {}) or {} |
| 75 | if folder_uid_arg not in folder_cache and folder_uid_arg not in nsf_folders: |
| 76 | raise CommandError( |
| 77 | 'pam action debug dump', |
| 78 | 'Service Mode requires a visible folder UID.', |
| 79 | ) |
| 80 | elif save_as is None and output_format != 'json': |
| 81 | raise CommandError( |
| 82 | 'pam action debug dump', |
| 83 | 'Specify --save-as/-s FILE or --format=json.', |
| 84 | ) |
| 85 | |
| 86 | def _write_result(rows: Iterable[dict]) -> None: |
| 87 | def _write_json_array(stream) -> int: |
| 88 | encoder = json.JSONEncoder(indent=2) |
| 89 | count = 0 |
| 90 | stream.write('[') |
| 91 | for row in rows: |
| 92 | stream.write('\n' if count == 0 else ',\n') |
| 93 | at_line_start = True |
| 94 | for chunk in encoder.iterencode(row): |
no outgoing calls