|
2 | 2 | """Define the CloudEndure template logic.""" |
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 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 | + |
5 | 51 |
|
6 | 52 | class TerraformTemplate: |
7 | 53 | """Define Terraform template entries. |
|
0 commit comments