diff --git a/jeeves/core/actions/__init__.py b/jeeves/core/actions/__init__.py index 4dce1f9..e10adea 100644 --- a/jeeves/core/actions/__init__.py +++ b/jeeves/core/actions/__init__.py @@ -1,7 +1,10 @@ from jeeves.core.actions.shell import ScriptAction from jeeves.core.actions.docker import DockerBuildAction, DockerRunAction +from jeeves.core.actions.files import TemplateAction __all__ = [ + # Files + TemplateAction, # Shell ScriptAction, # Docker diff --git a/jeeves/core/actions/files.py b/jeeves/core/actions/files.py new file mode 100644 index 0000000..91a6574 --- /dev/null +++ b/jeeves/core/actions/files.py @@ -0,0 +1,43 @@ +from typing import Text +import os + +from jinja2 import Template + +from jeeves.core.objects import Result +from jeeves.core.actions.base import Action + + +class TemplateAction(Action): + """ + Parses the source jinja2 template into the output path + """ + + id = "contrib/template" + verbose_name = "Render template" + + class Parameters(Action.Parameters): + """ + +----------------+------+-----------+----------------------------------------------+ + | Parameter name | Type | Mandatory | Description | + +================+======+===========+==============================================+ + | ``src`` | text | yes | The source template | + | ``dest`` | text | yes | The destination file (relative to workspace) | + +----------------+------+-----------+----------------------------------------------+ + """ + + src: Text + dest: Text + + def execute(self, **kwargs): + workspace = kwargs.get("workspace") + arguments = kwargs.get("arguments") + + source_path = os.path.join(os.getcwd(), self.parameters.src) + + assert os.path.exists(source_path), "Source template does not exist" + + template = Template(open(source_path, "r").read()) + with open(os.path.join(workspace.path, self.parameters.dest), "w") as handler: + handler.write(template.render(**arguments)) + + return Result(success=True)