21
21
import yaml
22
22
from click import ClickException
23
23
from snowflake .cli .__about__ import VERSION
24
+ from snowflake .cli ._plugins .cicd .manager import (
25
+ CIProvider ,
26
+ CIProviderChoices ,
27
+ CIProviderManager ,
28
+ )
24
29
from snowflake .cli .api .commands .flags import (
25
30
NoInteractiveOption ,
26
31
variables_option ,
42
47
43
48
44
49
DEFAULT_SOURCE = "https://github.com/snowflakedb/snowflake-cli-templates"
50
+ DEFAULT_CI_SOURCE = DEFAULT_SOURCE + "/cicd"
45
51
46
52
log = logging .getLogger (__name__ )
47
53
@@ -72,6 +78,17 @@ def _path_argument_callback(path: str) -> str:
72
78
"--template-source" ,
73
79
help = f"local path to template directory or URL to git repository with templates." ,
74
80
)
81
+ CIProviderOption = typer .Option (
82
+ None ,
83
+ "--ci-provider" ,
84
+ help = f"CI provider to generate workflow for." ,
85
+ case_sensitive = True ,
86
+ )
87
+ CITemplateSourceOption = typer .Option (
88
+ None ,
89
+ "--ci-template-source" ,
90
+ help = f"local path to template directory or URL to git repository with ci/cd templates." ,
91
+ )
75
92
VariablesOption = variables_option (
76
93
"String in `key=value` format. Provided variables will not be prompted for."
77
94
)
@@ -191,6 +208,8 @@ def init(
191
208
path : str = PathArgument ,
192
209
template : Optional [str ] = TemplateOption ,
193
210
template_source : Optional [str ] = SourceOption ,
211
+ ci_provider : Optional [CIProviderChoices ] = CIProviderOption ,
212
+ ci_template_source : Optional [str ] = CITemplateSourceOption ,
194
213
variables : Optional [List [str ]] = VariablesOption ,
195
214
no_interactive : bool = NoInteractiveOption ,
196
215
** options ,
@@ -219,6 +238,28 @@ def init(
219
238
destination = tmpdir ,
220
239
)
221
240
241
+ if ci_provider :
242
+ ci_provider_instance = CIProvider .from_choice (ci_provider )
243
+ if ci_template_source is not None :
244
+ with SecurePath .temporary_directory () as cicd_tmpdir :
245
+ cicd_template_root = _fetch_remote_template (
246
+ url = ci_template_source ,
247
+ path = None ,
248
+ destination = cicd_tmpdir
249
+ # type: ignore
250
+ )
251
+ cicd_template_root .copy (template_root .path )
252
+
253
+ elif ci_provider_instance .has_template (template_root ):
254
+ pass # template has ci files
255
+ else :
256
+ # use generic ci/cd template
257
+ with SecurePath .temporary_directory () as cicd_tmpdir :
258
+ cicd_template_root = _fetch_remote_template (
259
+ url = DEFAULT_SOURCE , path = f"cicd/{ ci_provider_instance .name .lower ()} " , destination = cicd_tmpdir # type: ignore
260
+ )
261
+ cicd_template_root .copy (template_root .path )
262
+
222
263
template_metadata = _read_template_metadata (
223
264
template_root , args_error_msg = args_error_msg
224
265
)
@@ -233,16 +274,22 @@ def init(
233
274
"project_dir_name" : SecurePath (path ).name ,
234
275
"snowflake_cli_version" : VERSION ,
235
276
}
236
- log . debug (
237
- "Rendering template files: %s" , ", " . join ( template_metadata . files_to_render )
277
+ files_to_render = template_metadata . files_to_render + list (
278
+ ci_provider_instance . get_files_to_render ( template_root )
238
279
)
280
+ log .debug ("Rendering template files: %s" , ", " .join (files_to_render ))
239
281
render_template_files (
240
282
template_root = template_root ,
241
- files_to_render = template_metadata . files_to_render ,
283
+ files_to_render = files_to_render ,
242
284
data = variable_values ,
243
285
)
244
286
_remove_template_metadata_file (template_root )
287
+ post_generate (template_root , ci_provider_instance )
245
288
SecurePath (path ).parent .mkdir (exist_ok = True , parents = True )
246
289
template_root .copy (path )
247
290
248
291
return MessageResult (f"Initialized the new project in { path } " )
292
+
293
+
294
+ def post_generate (template_root : SecurePath , ci_provider : Optional [CIProvider ]):
295
+ CIProviderManager .project_post_gen_cleanup (ci_provider , template_root )
0 commit comments