@@ -26,17 +26,32 @@ impl Config {
26
26
override_api_key : Option < String > ,
27
27
) -> Result < Config , HoustonProblem > {
28
28
let home = match override_home {
29
- Some ( home) => PathBuf :: from ( home. as_ref ( ) ) ,
29
+ Some ( home) => {
30
+ let home_path = PathBuf :: from ( home. as_ref ( ) ) ;
31
+ if home_path. exists ( ) && !home_path. is_dir ( ) {
32
+ Err ( HoustonProblem :: InvalidOverrideConfigDir (
33
+ home_path. display ( ) . to_string ( ) ,
34
+ ) )
35
+ } else {
36
+ Ok ( home_path)
37
+ }
38
+ }
30
39
None => {
31
40
// Lin: /home/alice/.config/rover
32
41
// Win: C:\Users\Alice\AppData\Roaming\Apollo\Rover\config
33
42
// Mac: /Users/Alice/Library/Application Support/com.Apollo.Rover
34
- ProjectDirs :: from ( "com" , "Apollo" , "Rover" )
35
- . ok_or ( HoustonProblem :: ConfigDirNotFound ) ?
43
+ Ok ( ProjectDirs :: from ( "com" , "Apollo" , "Rover" )
44
+ . ok_or ( HoustonProblem :: DefaultConfigDirNotFound ) ?
36
45
. config_dir ( )
37
- . to_path_buf ( )
46
+ . to_path_buf ( ) )
38
47
}
39
- } ;
48
+ } ?;
49
+
50
+ if !home. exists ( ) {
51
+ fs:: create_dir_all ( & home) . map_err ( |_| {
52
+ HoustonProblem :: CouldNotCreateConfigHome ( home. display ( ) . to_string ( ) )
53
+ } ) ?;
54
+ }
40
55
41
56
Ok ( Config {
42
57
home,
@@ -47,7 +62,8 @@ impl Config {
47
62
/// Removes all configuration files from filesystem
48
63
pub fn clear ( & self ) -> Result < ( ) , HoustonProblem > {
49
64
tracing:: debug!( home_dir = ?self . home) ;
50
- fs:: remove_dir_all ( & self . home ) . map_err ( |_| HoustonProblem :: NoConfigFound )
65
+ fs:: remove_dir_all ( & self . home )
66
+ . map_err ( |_| HoustonProblem :: NoConfigFound ( self . home . display ( ) . to_string ( ) ) )
51
67
}
52
68
}
53
69
@@ -57,15 +73,10 @@ mod tests {
57
73
use assert_fs:: TempDir ;
58
74
#[ test]
59
75
fn it_can_clear_global_config ( ) {
60
- let config = get_config ( None ) ;
61
- std :: fs :: create_dir ( & config . home ) . unwrap ( ) ;
76
+ let tmp_home = TempDir :: new ( ) . unwrap ( ) ;
77
+ let config = Config :: new ( Some ( & tmp_home . path ( ) ) , None ) . unwrap ( ) ;
62
78
assert ! ( config. home. exists( ) ) ;
63
79
config. clear ( ) . unwrap ( ) ;
64
80
assert_eq ! ( config. home. exists( ) , false ) ;
65
81
}
66
-
67
- fn get_config ( override_api_key : Option < String > ) -> Config {
68
- let tmp_home = TempDir :: new ( ) . unwrap ( ) ;
69
- Config :: new ( Some ( & tmp_home. path ( ) ) , override_api_key) . unwrap ( )
70
- }
71
82
}
0 commit comments