8
8
from ansible_creator .exceptions import CreatorError
9
9
from ansible_creator .templar import Templar
10
10
from ansible_creator .types import TemplateData
11
- from ansible_creator .utils import Walker , Copier , ask_yes_no
11
+ from ansible_creator .utils import Copier , Walker , ask_yes_no
12
12
13
13
14
14
if TYPE_CHECKING :
15
15
from ansible_creator .config import Config
16
16
from ansible_creator .output import Output
17
17
18
+
18
19
class Add :
19
20
"""Class to handle the add subcommand."""
20
21
21
22
common_resources = ("common.devfile" ,)
22
-
23
-
23
+
24
24
def __init__ (
25
25
self : Add ,
26
26
config : Config ,
27
27
) -> None :
28
-
28
+ """Initialize the add action.
29
+
30
+ Args:
31
+ config: App configuration object.
32
+ """
33
+
29
34
self ._resource_type : str = config .resource_type
30
35
self ._add_path : Path = Path (config .path )
31
36
self ._force = config .force
@@ -35,7 +40,7 @@ def __init__(
35
40
self ._project = config .project
36
41
self .output : Output = config .output
37
42
self .templar = Templar ()
38
-
43
+
39
44
def run (self ) -> None :
40
45
"""Start scaffolding the resource file."""
41
46
self ._check_add_path ()
@@ -46,7 +51,9 @@ def run(self) -> None:
46
51
def _check_add_path (self ) -> None :
47
52
"""Validate the provided add path."""
48
53
if not self ._add_path .exists ():
49
- raise CreatorError (f"The path { self ._add_path } does not exist. Please provide an existing directory." )
54
+ raise CreatorError (
55
+ f"The path { self ._add_path } does not exist. Please provide an existing directory." ,
56
+ )
50
57
51
58
def _scaffold (self ) -> None :
52
59
"""Scaffold the specified resource file."""
@@ -59,8 +66,7 @@ def _scaffold(self) -> None:
59
66
)
60
67
61
68
# Initialize Walker and Copier for file operations
62
-
63
-
69
+
64
70
walker = Walker (
65
71
resources = self .common_resources ,
66
72
resource_id = "common.devfile" ,
@@ -71,28 +77,30 @@ def _scaffold(self) -> None:
71
77
)
72
78
paths = walker .collect_paths ()
73
79
copier = Copier (output = self .output )
74
-
80
+
75
81
if self ._no_overwrite :
82
+ msg = "The flag `--no-overwrite` restricts overwriting."
76
83
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 ."
84
+ msg += (
85
+ "\n The destination directory contains files that can be overwritten."
86
+ "\n Please re-run ansible-creator with --overwrite to continue ."
80
87
)
88
+ raise CreatorError (msg )
81
89
82
90
if not paths .has_conflicts () or self ._force or self ._overwrite :
83
91
copier .copy_containers (paths )
84
92
self .output .note (f"Resource added to { self ._add_path } " )
85
93
return
86
94
87
-
88
95
if not self ._overwrite :
89
96
question = "Some files in the destination directory may be overwritten. Do you want to proceed?"
90
97
if ask_yes_no (question ):
91
98
copier .copy_containers (paths )
92
99
else :
93
- raise CreatorError (
94
- "The destination contains files that could be overwritten. "
95
- "Please re-run ansible-creator with --overwrite to continue."
100
+ msg = (
101
+ "The destination directory contains files that will be overwritten."
102
+ " Please re-run ansible-creator with --overwrite to continue."
96
103
)
104
+ raise CreatorError (msg )
97
105
98
- self .output .note (f"Resource added to { self ._add_path } " )
106
+ self .output .note (f"Resource added to { self ._add_path } " )
0 commit comments