From bcfb276b0a97f8f0be1c244bdac59096ca2d1811 Mon Sep 17 00:00:00 2001 From: ilan Date: Mon, 8 Apr 2024 17:20:28 +0300 Subject: [PATCH] dump api --- demisto_sdk/__main__.py | 37 ++++++++++++++++++++++++++++ demisto_sdk/commands/common/tools.py | 20 +++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/demisto_sdk/__main__.py b/demisto_sdk/__main__.py index 6ac80487f8..1d3c9325f2 100644 --- a/demisto_sdk/__main__.py +++ b/demisto_sdk/__main__.py @@ -44,6 +44,7 @@ logging_setup, ) from demisto_sdk.commands.common.tools import ( + convert_path_to_str, find_type, get_last_remote_release_version, get_release_note_entries, @@ -3809,5 +3810,41 @@ def xsoar_linter( main.add_command(typer.main.get_command(xsoar_linter_app), "xsoar-lint") +# ====================== export ====================== # + +export_app = typer.Typer(name="dump-api") + + +@export_app.command( + context_settings={"allow_extra_args": True, "ignore_unknown_options": True}, +) +def dump_api( + ctx: typer.Context, + output_path: Path = typer.Option( + CONTENT_PATH, + "-o", + "--output", + help="The output directory to save the exported demisto-sdk api.", + ), +): + output_json: dict = {} + for command_name, command in main.commands.items(): + if isinstance(command, click.Group): + output_json[command_name] = {} + for sub_command_name, sub_command in command.commands.items(): + output_json[command_name][sub_command_name] = sub_command.to_info_dict( + ctx + ) + else: + output_json[command_name] = command.to_info_dict(ctx) + convert_path_to_str(output_json) + if output_path.is_dir(): + output_path = output_path / "demisto-sdk-api.json" + output_path.write_text(json.dumps(output_json, indent=4)) + + +main.add_command(typer.main.get_command(export_app), "dump-api") + + if __name__ == "__main__": main() diff --git a/demisto_sdk/commands/common/tools.py b/demisto_sdk/commands/common/tools.py index 277a405cc8..20aadc8009 100644 --- a/demisto_sdk/commands/common/tools.py +++ b/demisto_sdk/commands/common/tools.py @@ -4490,3 +4490,23 @@ def get_relative_path(file_path: Union[str, Path], relative_to: Path) -> Path: if file_path.is_absolute(): file_path = file_path.relative_to(relative_to) return file_path + + +def convert_path_to_str(data: Union[dict, list]): + """This converts recursively all Path objects to strings in the given data. + + Args: + data (Union[dict, list]): The data to convert. + """ + if isinstance(data, dict): + for key, value in data.items(): + if isinstance(value, (dict, list)): + convert_path_to_str(value) + elif isinstance(value, Path): + data[key] = str(value) + elif isinstance(data, list): + for index, item in enumerate(data): + if isinstance(item, (dict, list)): + convert_path_to_str(item) + elif isinstance(item, Path): + data[index] = str(item)