Skip to content

Commit 4a2bbdc

Browse files
authored
Merge pull request #74 from 2ndWatch/add-cookiecutter-base
Add cookiecutter base handler
2 parents ccff551 + 4d782e7 commit 4a2bbdc

File tree

3 files changed

+158
-15
lines changed

3 files changed

+158
-15
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ certifi = "*"
4444
python-dateutil = "*"
4545
setuptools = "*"
4646
urllib3 = "*"
47+
cookiecutter = "*"
4748

4849
[requires]
4950
python_version = "3.7"

Pipfile.lock

Lines changed: 111 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloudendure/templates.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,52 @@
22
"""Define the CloudEndure template logic."""
33
from __future__ import annotations
44

5+
from typing import Any, Dict
6+
7+
from cookiecutter.main import cookiecutter
8+
9+
10+
class CookiecutterHandler:
11+
"""Handle cookiecutter operations."""
12+
13+
def run(
14+
self,
15+
migration_data: Dict[str, Any],
16+
cookiecutter_path: str = "https://github.com/2ndWatch/cookiecutter-tf-cloudendure",
17+
) -> None:
18+
"""Run the automation of cookiecutter."""
19+
print("Running cookiecutter project creation with package: ", cookiecutter_path)
20+
for k, v in migration_data.items():
21+
print("Creating the cookiecutter repository subdirectory for: ", k)
22+
self.create_project(package_path=cookiecutter_path, context=v)
23+
24+
def create_project(
25+
self, package_path: str, context: Dict[Any, Any] = None, no_input: bool = True
26+
) -> bool:
27+
"""Create a cookiecutter project with the provided details.
28+
29+
Args:
30+
package_path (str): The path to the cookiecutter template.
31+
context (dict): The seed context to be used to populate cookiecutter values.
32+
Defaults to an empty dictionary.
33+
no_input (bool): Whether or not the interaction is no input.
34+
Requires True in order to function for bulk creation.
35+
Defaults to: True.
36+
37+
Returns:
38+
bool: Whether or not project creation was successful.
39+
40+
"""
41+
if not context:
42+
context = {}
43+
44+
try:
45+
cookiecutter(package_path, no_input=no_input, extra_context=context)
46+
except Exception as e:
47+
print("Exception: (%s) encountered in create_project", e)
48+
return False
49+
return True
50+
551

652
class TerraformTemplate:
753
"""Define Terraform template entries.

0 commit comments

Comments
 (0)