11//! src/initializers/init_project.rs
22
3- use std:: path:: PathBuf ;
4- use std:: { collections:: HashMap , fmt, str:: FromStr } ;
3+ use std:: { collections:: HashMap , fmt, path:: PathBuf , str:: FromStr } ;
54
65use clap:: Args ;
76
8- use crate :: utils:: file_writer:: FileWriteError ;
97use crate :: {
10- app_config:: app_config:: AppConfig ,
8+ app_config:: { AppCache , app_config:: AppConfig } ,
119 initializers:: project_types:: {
1210 ansible:: { AnsibleProject , AnsibleProjectError } ,
1311 maven:: { MavenProject , MavenProjectError } ,
1412 } ,
13+ utils:: file_writer:: FileWriteError ,
1514} ;
1615
1716#[ derive( Debug ) ]
1817pub enum InitProjectError {
1918 Invalid ( String ) ,
2019 FileWrite ( FileWriteError ) ,
20+ NotFound ( PathBuf ) ,
2121 // Specific project type errors
2222 MavenProject ( MavenProjectError ) ,
2323 AnsibleProject ( AnsibleProjectError ) ,
@@ -50,6 +50,9 @@ impl fmt::Display for InitProjectError {
5050 InitProjectError :: FileWrite ( e) => {
5151 write ! ( f, "{}" , e)
5252 }
53+ InitProjectError :: NotFound ( e) => {
54+ write ! ( f, "Template files not found: {}" , e. display( ) )
55+ }
5356 InitProjectError :: MavenProject ( e) => {
5457 write ! ( f, "{}" , e)
5558 }
@@ -126,7 +129,11 @@ impl ProjectFactory {
126129 }
127130}
128131
129- pub fn handle ( args : InitProjectArgs , config : AppConfig ) -> Result < ( ) , InitProjectError > {
132+ pub fn handle (
133+ args : InitProjectArgs ,
134+ config : AppConfig ,
135+ cache : AppCache ,
136+ ) -> Result < ( ) , InitProjectError > {
130137 // Ensure the passed project type and given profile, if any, is present in the config file
131138 // before passing it along
132139 let template: PathBuf = config
@@ -139,8 +146,17 @@ pub fn handle(args: InitProjectArgs, config: AppConfig) -> Result<(), InitProjec
139146 p. name == args. project_type && p. profile == "default"
140147 }
141148 } )
142- . map ( |p| p. template_files . clone ( ) )
143- . ok_or ( InitProjectError :: Invalid ( "Could not find template" . into ( ) ) ) ?;
149+ . ok_or ( InitProjectError :: Invalid ( String :: from (
150+ "Could not find template" ,
151+ ) ) )
152+ . and_then ( |p| -> Result < PathBuf , InitProjectError > {
153+ println ! ( "CACHE DIR: {}" , cache. cache_dir. clone( ) . display( ) ) ;
154+ if p. template_files . starts_with ( cache. cache_dir ) {
155+ Ok ( p. template_files . clone ( ) )
156+ } else {
157+ Err ( InitProjectError :: NotFound ( p. template_files . clone ( ) ) )
158+ }
159+ } ) ?;
144160
145161 // Convert custom key=value settings into map
146162 // for easier lookup
0 commit comments