@@ -99,6 +99,13 @@ pub struct ConfigFile {
9999 pub profiles : HashMap < String , ProfileConfig > ,
100100}
101101
102+ fn write_config ( config_path : & std:: path:: Path , content : & str ) -> Result < ( ) , String > {
103+ if let Some ( parent) = config_path. parent ( ) {
104+ fs:: create_dir_all ( parent) . map_err ( |e| format ! ( "error creating config directory: {e}" ) ) ?;
105+ }
106+ fs:: write ( config_path, content) . map_err ( |e| format ! ( "error writing config file: {e}" ) )
107+ }
108+
102109pub fn save_api_key ( profile : & str , api_key : & str ) -> Result < ( ) , String > {
103110 let user_dirs = UserDirs :: new ( ) . ok_or ( "could not determine home directory" ) ?;
104111 let config_path = user_dirs. home_dir ( ) . join ( ".hotdata" ) . join ( "config.yml" ) ;
@@ -122,7 +129,7 @@ pub fn save_api_key(profile: &str, api_key: &str) -> Result<(), String> {
122129 let content = serde_yaml:: to_string ( & config_file)
123130 . map_err ( |e| format ! ( "error serializing config: {e}" ) ) ?;
124131
125- fs :: write ( & config_path, content) . map_err ( |e| format ! ( "error writing config file: {e}" ) )
132+ write_config ( & config_path, & content)
126133}
127134
128135pub fn remove_api_key ( profile : & str ) -> Result < ( ) , String > {
@@ -145,7 +152,7 @@ pub fn remove_api_key(profile: &str) -> Result<(), String> {
145152
146153 let content = serde_yaml:: to_string ( & config_file)
147154 . map_err ( |e| format ! ( "error serializing config: {e}" ) ) ?;
148- fs :: write ( & config_path, content) . map_err ( |e| format ! ( "error writing config file: {e}" ) )
155+ write_config ( & config_path, & content)
149156}
150157
151158pub fn save_workspaces ( profile : & str , workspaces : Vec < WorkspaceEntry > ) -> Result < ( ) , String > {
@@ -171,7 +178,7 @@ pub fn save_workspaces(profile: &str, workspaces: Vec<WorkspaceEntry>) -> Result
171178 let content = serde_yaml:: to_string ( & config_file)
172179 . map_err ( |e| format ! ( "error serializing config: {e}" ) ) ?;
173180
174- fs :: write ( & config_path, content) . map_err ( |e| format ! ( "error writing config file: {e}" ) )
181+ write_config ( & config_path, & content)
175182}
176183
177184pub fn save_default_workspace ( profile : & str , workspace : WorkspaceEntry ) -> Result < ( ) , String > {
@@ -192,7 +199,7 @@ pub fn save_default_workspace(profile: &str, workspace: WorkspaceEntry) -> Resul
192199
193200 let content = serde_yaml:: to_string ( & config_file)
194201 . map_err ( |e| format ! ( "error serializing config: {e}" ) ) ?;
195- fs :: write ( & config_path, content) . map_err ( |e| format ! ( "error writing config file: {e}" ) )
202+ write_config ( & config_path, & content)
196203}
197204
198205pub fn resolve_workspace_id ( provided : Option < String > , profile_config : & ProfileConfig ) -> Result < String , String > {
0 commit comments