Skip to content

Commit a90d94f

Browse files
Add support for add subcommand to scaffold devfile resources
1 parent a93ac24 commit a90d94f

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

src/ansible_creator/arg_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
COMING_SOON = (
3838
"add resource devcontainer",
39-
"add resource devfile",
39+
# "add resource devfile",
4040
"add resource role",
4141
"add plugin action",
4242
"add plugin filter",

src/ansible_creator/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class Config:
3535
creator_version: str
3636
output: Output
3737
subcommand: str
38-
38+
type: str = ""
39+
resource_type :str = ""
40+
path :str = ""
3941
collection: str = ""
4042
force: bool = False
4143
overwrite: bool = False
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Definitions for ansible-creator add action."""
2+
3+
from __future__ import annotations
4+
5+
from pathlib import Path
6+
from typing import TYPE_CHECKING
7+
8+
from ansible_creator.exceptions import CreatorError
9+
from ansible_creator.templar import Templar
10+
from ansible_creator.types import TemplateData
11+
from ansible_creator.utils import Walker, Copier, ask_yes_no
12+
13+
14+
if TYPE_CHECKING:
15+
from ansible_creator.config import Config
16+
from ansible_creator.output import Output
17+
18+
class Add:
19+
"""Class to handle the add subcommand."""
20+
21+
common_resources = ("common.devfile",)
22+
23+
24+
def __init__(
25+
self: Add,
26+
config: Config,
27+
) -> None:
28+
29+
self._resource_type: str = config.resource_type
30+
self._add_path: Path = Path(config.path)
31+
self._force = config.force
32+
self._overwrite = config.overwrite
33+
self._no_overwrite = config.no_overwrite
34+
self._creator_version = config.creator_version
35+
self._project = config.project
36+
self.output: Output = config.output
37+
self.templar = Templar()
38+
39+
def run(self) -> None:
40+
"""Start scaffolding the resource file."""
41+
self._check_add_path()
42+
self.output.debug(msg=f"final collection path set to {self._add_path}")
43+
44+
self._scaffold()
45+
46+
def _check_add_path(self) -> None:
47+
"""Validate the provided add path."""
48+
if not self._add_path.exists():
49+
raise CreatorError(f"The path {self._add_path} does not exist. Please provide an existing directory.")
50+
51+
def _scaffold(self) -> None:
52+
"""Scaffold the specified resource file."""
53+
self.output.debug(f"Started copying {self._project} resource to destination")
54+
55+
# Set up template data
56+
template_data = TemplateData(
57+
resource_type=self._resource_type,
58+
creator_version=self._creator_version,
59+
)
60+
61+
# Initialize Walker and Copier for file operations
62+
63+
64+
walker = Walker(
65+
resources=self.common_resources,
66+
resource_id="common.devfile",
67+
dest=self._add_path,
68+
output=self.output,
69+
template_data=template_data,
70+
templar=self.templar,
71+
)
72+
paths = walker.collect_paths()
73+
copier = Copier(output=self.output)
74+
75+
if self._no_overwrite:
76+
if paths.has_conflicts():
77+
raise CreatorError(
78+
"The destination directory contains files that may be overwritten. "
79+
"Please re-run ansible-creator with --overwrite to proceed."
80+
)
81+
82+
if not paths.has_conflicts() or self._force or self._overwrite:
83+
copier.copy_containers(paths)
84+
self.output.note(f"Resource added to {self._add_path}")
85+
return
86+
87+
88+
if not self._overwrite:
89+
question = "Some files in the destination directory may be overwritten. Do you want to proceed?"
90+
if ask_yes_no(question):
91+
copier.copy_containers(paths)
92+
else:
93+
raise CreatorError(
94+
"The destination contains files that could be overwritten. "
95+
"Please re-run ansible-creator with --overwrite to continue."
96+
)
97+
98+
self.output.note(f"Resource added to {self._add_path}")

src/ansible_creator/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TemplateData:
2727
recommended_extensions: A list of recommended VsCode extensions.
2828
"""
2929

30+
resource_type: str = ""
3031
additions: dict[str, dict[str, dict[str, str | bool]]] = field(default_factory=dict)
3132
collection_name: str = ""
3233
creator_version: str = ""

0 commit comments

Comments
 (0)