|
| 1 | +"""Definitions for ansible-creator add action.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import uuid |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | +from typing import TYPE_CHECKING |
| 9 | + |
| 10 | +from ansible_creator.exceptions import CreatorError |
| 11 | +from ansible_creator.templar import Templar |
| 12 | +from ansible_creator.types import TemplateData |
| 13 | +from ansible_creator.utils import Copier, Walker, ask_yes_no |
| 14 | + |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from ansible_creator.config import Config |
| 18 | + from ansible_creator.output import Output |
| 19 | + |
| 20 | + |
| 21 | +class Add: |
| 22 | + """Class to handle the add subcommand.""" |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self: Add, |
| 26 | + config: Config, |
| 27 | + ) -> None: |
| 28 | + """Initialize the add action. |
| 29 | +
|
| 30 | + Args: |
| 31 | + config: App configuration object. |
| 32 | + """ |
| 33 | + self._resource_type: str = config.resource_type |
| 34 | + self._resource_id: str = f"common.{self._resource_type}" |
| 35 | + self._add_path: Path = Path(config.path) |
| 36 | + self._force = config.force |
| 37 | + self._overwrite = config.overwrite |
| 38 | + self._no_overwrite = config.no_overwrite |
| 39 | + self._creator_version = config.creator_version |
| 40 | + self._project = config.project |
| 41 | + self.output: Output = config.output |
| 42 | + self.templar = Templar() |
| 43 | + |
| 44 | + def run(self) -> None: |
| 45 | + """Start scaffolding the resource file.""" |
| 46 | + self._check_add_path() |
| 47 | + self.output.debug(msg=f"final collection path set to {self._add_path}") |
| 48 | + |
| 49 | + self._scaffold() |
| 50 | + |
| 51 | + def _check_add_path(self) -> None: |
| 52 | + """Validate the provided add path. |
| 53 | +
|
| 54 | + Raises: |
| 55 | + CreatorError: If the add path does not exist. |
| 56 | + """ |
| 57 | + if not self._add_path.exists(): |
| 58 | + msg = f"The path {self._add_path} does not exist. Please provide an existing directory." |
| 59 | + raise CreatorError(msg) |
| 60 | + |
| 61 | + def unique_name_in_devfile(self) -> str: |
| 62 | + """Use project specific name in devfile. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + Unique name entry. |
| 66 | + """ |
| 67 | + final_name = ".".join(self._add_path.parts[-2:]) |
| 68 | + final_uuid = str(uuid.uuid4())[:8] |
| 69 | + return f"{final_name}-{final_uuid}" |
| 70 | + |
| 71 | + def _scaffold(self) -> None: |
| 72 | + """Scaffold the specified resource file based on the resource type. |
| 73 | +
|
| 74 | + Raises: |
| 75 | + CreatorError: If unsupported resource type is given. |
| 76 | + """ |
| 77 | + self.output.debug(f"Started copying {self._project} resource to destination") |
| 78 | + |
| 79 | + # Call the appropriate scaffolding function based on the resource type |
| 80 | + if self._resource_type == "devfile": |
| 81 | + template_data = self._get_devfile_template_data() |
| 82 | + |
| 83 | + else: |
| 84 | + |
| 85 | + msg = f"Unsupported resource type: {self._resource_type}" |
| 86 | + raise CreatorError(msg) |
| 87 | + |
| 88 | + self._perform_scaffold(template_data) |
| 89 | + |
| 90 | + def _perform_scaffold(self, template_data: TemplateData) -> None: |
| 91 | + """Perform the actual scaffolding process using the provided template data. |
| 92 | +
|
| 93 | + Args: |
| 94 | + template_data: TemplateData |
| 95 | +
|
| 96 | + Raises: |
| 97 | + CreatorError: If there are conflicts and overwriting is not allowed, or if the |
| 98 | + destination directory contains files that will be overwritten. |
| 99 | + """ |
| 100 | + walker = Walker( |
| 101 | + resources=(f"common.{self._resource_type}",), |
| 102 | + resource_id=self._resource_id, |
| 103 | + dest=self._add_path, |
| 104 | + output=self.output, |
| 105 | + template_data=template_data, |
| 106 | + templar=self.templar, |
| 107 | + ) |
| 108 | + paths = walker.collect_paths() |
| 109 | + copier = Copier(output=self.output) |
| 110 | + |
| 111 | + if self._no_overwrite: |
| 112 | + msg = "The flag `--no-overwrite` restricts overwriting." |
| 113 | + if paths.has_conflicts(): |
| 114 | + msg += ( |
| 115 | + "\nThe destination directory contains files that can be overwritten." |
| 116 | + "\nPlease re-run ansible-creator with --overwrite to continue." |
| 117 | + ) |
| 118 | + raise CreatorError(msg) |
| 119 | + |
| 120 | + if not paths.has_conflicts() or self._force or self._overwrite: |
| 121 | + copier.copy_containers(paths) |
| 122 | + self.output.note(f"Resource added to {self._add_path}") |
| 123 | + return |
| 124 | + |
| 125 | + if not self._overwrite: |
| 126 | + question = ( |
| 127 | + "Files in the destination directory will be overwritten. Do you want to proceed?" |
| 128 | + ) |
| 129 | + if ask_yes_no(question): |
| 130 | + copier.copy_containers(paths) |
| 131 | + else: |
| 132 | + msg = ( |
| 133 | + "The destination directory contains files that will be overwritten." |
| 134 | + " Please re-run ansible-creator with --overwrite to continue." |
| 135 | + ) |
| 136 | + raise CreatorError(msg) |
| 137 | + |
| 138 | + self.output.note(f"Resource added to {self._add_path}") |
| 139 | + |
| 140 | + def _get_devfile_template_data(self) -> TemplateData: |
| 141 | + """Get the template data for devfile resources. |
| 142 | +
|
| 143 | + Returns: |
| 144 | + TemplateData: Data required for templating the devfile resource. |
| 145 | + """ |
| 146 | + return TemplateData( |
| 147 | + resource_type=self._resource_type, |
| 148 | + creator_version=self._creator_version, |
| 149 | + dev_file_name=self.unique_name_in_devfile(), |
| 150 | + ) |
0 commit comments