Skip to content

Commit

Permalink
add dump api command (#4214)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilaner committed Apr 10, 2024
1 parent e1207ed commit 623aceb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .changelog/4214.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Added the **dump-api** command to dump the demisto-sdk API to a JSON file.
type: internal
pr_number: 4214
2 changes: 2 additions & 0 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ jobs:
coverage xml
- name: Coveralls-Action
uses: coverallsapp/github-action@v2
continue-on-error: true
- name: Coveralls-Comment
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
continue-on-error: true
run: |
if [ -n "$COVERALLS_REPO_TOKEN" ]; then
pip install coveralls
Expand Down
45 changes: 45 additions & 0 deletions demisto_sdk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -3809,5 +3810,49 @@ 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 or JSON file to save the demisto-sdk api.",
),
):
"""
This commands dumps the `demisto-sdk` API to a file.
It is used to view the help of all commands in one file.
Args:
ctx (typer.Context):
output_path (Path, optional): The output directory or JSON file to save the 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()
20 changes: 20 additions & 0 deletions demisto_sdk/commands/common/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 623aceb

Please sign in to comment.