@@ -2,6 +2,7 @@ use crate::utils::display::print_unicode_box;
2
2
use clap:: { Arg , ArgAction , ArgMatches , Command } ;
3
3
use colored:: * ;
4
4
use reqwest:: blocking:: Client ;
5
+ use reqwest:: StatusCode ;
5
6
use std:: collections:: HashSet ;
6
7
use std:: fs;
7
8
use std:: io:: Write ;
@@ -173,10 +174,25 @@ fn validate_provider(provider: Option<&str>) -> String {
173
174
// Function to fetch template content from URL
174
175
fn fetch_template ( url : & str ) -> Result < String , String > {
175
176
let client = Client :: new ( ) ;
176
- client
177
+ let response = client
177
178
. get ( url)
178
179
. send ( )
179
- . map_err ( |e| format ! ( "Failed to fetch template: {}" , e) ) ?
180
+ . map_err ( |e| format ! ( "Failed to fetch template: {}" , e) ) ?;
181
+
182
+ // Check if response is successful (status code 200-299)
183
+ if !response. status ( ) . is_success ( ) {
184
+ // Handle 404 and other error status codes
185
+ if response. status ( ) == StatusCode :: NOT_FOUND {
186
+ return Err ( format ! ( "Template not found at URL: {}" , url) ) ;
187
+ } else {
188
+ return Err ( format ! (
189
+ "Failed to fetch template: HTTP status {}" ,
190
+ response. status( )
191
+ ) ) ;
192
+ }
193
+ }
194
+
195
+ response
180
196
. text ( )
181
197
. map_err ( |e| format ! ( "Failed to read template content: {}" , e) )
182
198
}
@@ -269,11 +285,6 @@ fn create_project_structure(
269
285
return Err ( format ! ( "Directory '{}' already exists" , stack_name) ) ;
270
286
}
271
287
272
- // Create necessary directories
273
- let resource_dir = base_path. join ( "resources" ) ;
274
- fs:: create_dir_all ( & resource_dir)
275
- . map_err ( |e| format ! ( "Failed to create directories: {}" , e) ) ?;
276
-
277
288
// Determine sample resource name based on provider
278
289
let sample_res_name = template_source. get_sample_res_name ( ) ;
279
290
@@ -282,25 +293,32 @@ fn create_project_structure(
282
293
context. insert ( "stack_name" , stack_name) ;
283
294
context. insert ( "stack_env" , env) ;
284
295
296
+ // First validate that all templates can be fetched before creating any directories
297
+ let manifest_template = get_template_content ( template_source, "manifest" , "" ) ?;
298
+ let readme_template = get_template_content ( template_source, "readme" , "" ) ?;
299
+ let resource_template = get_template_content ( template_source, "resource" , sample_res_name) ?;
300
+
301
+ // Now create directories
302
+ let resource_dir = base_path. join ( "resources" ) ;
303
+ fs:: create_dir_all ( & resource_dir)
304
+ . map_err ( |e| format ! ( "Failed to create directories: {}" , e) ) ?;
305
+
285
306
// Create files
286
- create_manifest_file ( & base_path, template_source , & context) ?;
287
- create_readme_file ( & base_path, template_source , & context) ?;
288
- create_resource_file ( & resource_dir, sample_res_name, template_source , & context) ?;
307
+ create_manifest_file ( & base_path, & manifest_template , & context) ?;
308
+ create_readme_file ( & base_path, & readme_template , & context) ?;
309
+ create_resource_file ( & resource_dir, sample_res_name, & resource_template , & context) ?;
289
310
290
311
Ok ( ( ) )
291
312
}
292
313
293
314
fn create_resource_file (
294
315
resource_dir : & Path ,
295
316
sample_res_name : & str ,
296
- template_source : & TemplateSource ,
317
+ template_str : & str ,
297
318
context : & Context ,
298
319
) -> Result < ( ) , String > {
299
- // Get template content
300
- let template_str = get_template_content ( template_source, "resource" , sample_res_name) ?;
301
-
302
320
// Render template with Tera
303
- let resource_content = render_template ( & template_str, context)
321
+ let resource_content = render_template ( template_str, context)
304
322
. map_err ( |e| format ! ( "Template rendering error: {}" , e) ) ?;
305
323
306
324
let resource_path = resource_dir. join ( format ! ( "{}.iql" , sample_res_name) ) ;
@@ -315,14 +333,11 @@ fn create_resource_file(
315
333
316
334
fn create_manifest_file (
317
335
base_path : & Path ,
318
- template_source : & TemplateSource ,
336
+ template_str : & str ,
319
337
context : & Context ,
320
338
) -> Result < ( ) , String > {
321
- // Get template content
322
- let template_str = get_template_content ( template_source, "manifest" , "" ) ?;
323
-
324
339
// Render template with Tera
325
- let manifest_content = render_template ( & template_str, context)
340
+ let manifest_content = render_template ( template_str, context)
326
341
. map_err ( |e| format ! ( "Template rendering error: {}" , e) ) ?;
327
342
328
343
let manifest_path = base_path. join ( "stackql_manifest.yml" ) ;
@@ -337,14 +352,11 @@ fn create_manifest_file(
337
352
338
353
fn create_readme_file (
339
354
base_path : & Path ,
340
- template_source : & TemplateSource ,
355
+ template_str : & str ,
341
356
context : & Context ,
342
357
) -> Result < ( ) , String > {
343
- // Get template content
344
- let template_str = get_template_content ( template_source, "readme" , "" ) ?;
345
-
346
358
// Render template with Tera
347
- let readme_content = render_template ( & template_str, context)
359
+ let readme_content = render_template ( template_str, context)
348
360
. map_err ( |e| format ! ( "Template rendering error: {}" , e) ) ?;
349
361
350
362
let readme_path = base_path. join ( "README.md" ) ;
0 commit comments