@@ -2,6 +2,7 @@ use crate::utils::display::print_unicode_box;
22use clap:: { Arg , ArgAction , ArgMatches , Command } ;
33use colored:: * ;
44use reqwest:: blocking:: Client ;
5+ use reqwest:: StatusCode ;
56use std:: collections:: HashSet ;
67use std:: fs;
78use std:: io:: Write ;
@@ -173,10 +174,25 @@ fn validate_provider(provider: Option<&str>) -> String {
173174// Function to fetch template content from URL
174175fn fetch_template ( url : & str ) -> Result < String , String > {
175176 let client = Client :: new ( ) ;
176- client
177+ let response = client
177178 . get ( url)
178179 . 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
180196 . text ( )
181197 . map_err ( |e| format ! ( "Failed to read template content: {}" , e) )
182198}
@@ -269,11 +285,6 @@ fn create_project_structure(
269285 return Err ( format ! ( "Directory '{}' already exists" , stack_name) ) ;
270286 }
271287
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-
277288 // Determine sample resource name based on provider
278289 let sample_res_name = template_source. get_sample_res_name ( ) ;
279290
@@ -282,25 +293,32 @@ fn create_project_structure(
282293 context. insert ( "stack_name" , stack_name) ;
283294 context. insert ( "stack_env" , env) ;
284295
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+
285306 // 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) ?;
289310
290311 Ok ( ( ) )
291312}
292313
293314fn create_resource_file (
294315 resource_dir : & Path ,
295316 sample_res_name : & str ,
296- template_source : & TemplateSource ,
317+ template_str : & str ,
297318 context : & Context ,
298319) -> Result < ( ) , String > {
299- // Get template content
300- let template_str = get_template_content ( template_source, "resource" , sample_res_name) ?;
301-
302320 // Render template with Tera
303- let resource_content = render_template ( & template_str, context)
321+ let resource_content = render_template ( template_str, context)
304322 . map_err ( |e| format ! ( "Template rendering error: {}" , e) ) ?;
305323
306324 let resource_path = resource_dir. join ( format ! ( "{}.iql" , sample_res_name) ) ;
@@ -315,14 +333,11 @@ fn create_resource_file(
315333
316334fn create_manifest_file (
317335 base_path : & Path ,
318- template_source : & TemplateSource ,
336+ template_str : & str ,
319337 context : & Context ,
320338) -> Result < ( ) , String > {
321- // Get template content
322- let template_str = get_template_content ( template_source, "manifest" , "" ) ?;
323-
324339 // Render template with Tera
325- let manifest_content = render_template ( & template_str, context)
340+ let manifest_content = render_template ( template_str, context)
326341 . map_err ( |e| format ! ( "Template rendering error: {}" , e) ) ?;
327342
328343 let manifest_path = base_path. join ( "stackql_manifest.yml" ) ;
@@ -337,14 +352,11 @@ fn create_manifest_file(
337352
338353fn create_readme_file (
339354 base_path : & Path ,
340- template_source : & TemplateSource ,
355+ template_str : & str ,
341356 context : & Context ,
342357) -> Result < ( ) , String > {
343- // Get template content
344- let template_str = get_template_content ( template_source, "readme" , "" ) ?;
345-
346358 // Render template with Tera
347- let readme_content = render_template ( & template_str, context)
359+ let readme_content = render_template ( template_str, context)
348360 . map_err ( |e| format ! ( "Template rendering error: {}" , e) ) ?;
349361
350362 let readme_path = base_path. join ( "README.md" ) ;
0 commit comments