From 67890f9935f6723194a57b81eae1575bdc751a2f Mon Sep 17 00:00:00 2001 From: lvoloshyn-sekoia Date: Tue, 2 Jul 2024 17:54:51 +0300 Subject: [PATCH] Add proper CLI --- sekoia_automation/cli.py | 17 +++++++++++++++++ sekoia_automation/scripts/action_runner.py | 21 +++++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/sekoia_automation/cli.py b/sekoia_automation/cli.py index 84d2247..611ac2e 100644 --- a/sekoia_automation/cli.py +++ b/sekoia_automation/cli.py @@ -12,6 +12,7 @@ from sekoia_automation.scripts.openapi import OpenApiToModule from sekoia_automation.scripts.sync_library import SyncLibrary from sekoia_automation.scripts.update_sdk_version import SDKUpdater +from sekoia_automation.scripts.action_runner import ModuleItemRunner app = typer.Typer( help="Sekoia.io's automation helper to generate playbook modules", @@ -185,5 +186,21 @@ def update_sekoia_library( SDKUpdater(modules_path=modules_path).update_sdk_version() +@app.command(name="run-action") +def run_action( + modules_path: Path = typer.Option(".", help="Path to the playbook modules"), + module_name: str = typer.Option(..., help="Name of the module to test"), + class_name: str = typer.Option(..., help="Class name of the action to test"), + args: list[str] = typer.Argument(None, help="Module/Action configuration fields"), +): + kwargs = { + arg.split("=", maxsplit=1)[0]: arg.split("=", maxsplit=1)[1] for arg in args + } + module_runner = ModuleItemRunner( + module_name=module_name, class_name=class_name, root_path=modules_path + ) + print(module_runner.run(args=kwargs)) + + if __name__ == "__main__": app() diff --git a/sekoia_automation/scripts/action_runner.py b/sekoia_automation/scripts/action_runner.py index fddaf68..f6634a8 100644 --- a/sekoia_automation/scripts/action_runner.py +++ b/sekoia_automation/scripts/action_runner.py @@ -12,12 +12,10 @@ class ModuleItemRunner: - def __init__(self, class_name: str, module_path: str | Path): + def __init__(self, class_name: str, module_name: str, root_path: Path): self.__class_name = class_name - self.__module_path = ( - module_path if isinstance(module_path, Path) else Path(module_path) - ).resolve() - self.__root_path = self.__module_path.parent + self.__root_path = root_path # `automation-library` folder by default + self.__module_path = (root_path / module_name).resolve() def load_class_from_path(self, path: Path | str, class_name: str) -> typing.Type: # Add the directory containing the module to sys.path @@ -142,7 +140,7 @@ def __iter_all_parents(c): raise ValueError("Incorrect class") - def run(self, args: dict, module_conf: dict | None = None) -> dict | None: + def run(self, args: dict) -> dict | None: cls_to_docker = self.get_docker_params_from_main_py() docker_param = cls_to_docker[self.__class_name] manifest: dict = self.get_manifest_by_docker_param( @@ -171,7 +169,7 @@ def run(self, args: dict, module_conf: dict | None = None) -> dict | None: # Prepare module configuration module_annotations = module_cls.__annotations__ module_config_cls = module_annotations["configuration"] - conf_args = module_conf if module_conf else {} + conf_args = {key: args.get(key) for key in module_config_cls.__fields__} module_conf = module_config_cls(**conf_args) module = module_cls() @@ -203,12 +201,3 @@ def run(self, args: dict, module_conf: dict | None = None) -> dict | None: module_item.run() return None - - -if __name__ == "__main__": - class_name = "RequestAction" - module_name = Path("~/PycharmProjects/automation-library/HTTP").expanduser() - args = {"method": "get", "url": "https://dummyjson.com/test"} - - c = ModuleItemRunner(module_path=module_name, class_name=class_name) - print(c.run(args=args, module_conf={}))