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 } " )
0 commit comments