Skip to content

Commit

Permalink
Add proper CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
lvoloshyn-sekoia committed Jul 2, 2024
1 parent 2c3b888 commit 67890f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
17 changes: 17 additions & 0 deletions sekoia_automation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check failure on line 17 in sekoia_automation/cli.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (I001)

sekoia_automation/cli.py:1:1: I001 Import block is un-sorted or un-formatted
help="Sekoia.io's automation helper to generate playbook modules",
Expand Down Expand Up @@ -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()
21 changes: 5 additions & 16 deletions sekoia_automation/scripts/action_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@


class ModuleItemRunner:

Check failure on line 14 in sekoia_automation/scripts/action_runner.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (I001)

sekoia_automation/scripts/action_runner.py:1:1: I001 Import block is un-sorted or un-formatted
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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={}))

0 comments on commit 67890f9

Please sign in to comment.