Save remote configuration to a config file. This preserves other sections in the config file and only updates the `[remotes]` section. # Arguments `path` - Path to the config file (typically `.atomic/config.toml`) # Errors Returns an error if the file cannot be written.
(&self, path: P)
| 328 | /// |
| 329 | /// Returns an error if the file cannot be written. |
| 330 | pub fn save<P: AsRef<Path>>(&self, path: P) -> RemoteResult<()> { |
| 331 | let path = path.as_ref(); |
| 332 | |
| 333 | // Load existing config or create empty |
| 334 | let mut full_config: toml::map::Map<String, toml::Value> = if path.exists() { |
| 335 | let content = std::fs::read_to_string(path).map_err(|e| RemoteError::ReadError { |
| 336 | path: path.display().to_string(), |
| 337 | source: e, |
| 338 | })?; |
| 339 | toml::from_str(&content).map_err(|e| RemoteError::ParseError { |
| 340 | path: path.display().to_string(), |
| 341 | message: e.to_string(), |
| 342 | })? |
| 343 | } else { |
| 344 | toml::map::Map::new() |
| 345 | }; |
| 346 | |
| 347 | // Update or remove the remotes section |
| 348 | if self.remotes.is_empty() { |
| 349 | full_config.remove("remotes"); |
| 350 | } else { |
| 351 | let remotes_value = |
| 352 | toml::Value::try_from(&self.remotes).map_err(|e| RemoteError::SerializeError { |
| 353 | message: e.to_string(), |
| 354 | })?; |
| 355 | full_config.insert("remotes".to_string(), remotes_value); |
| 356 | } |
| 357 | |
| 358 | // Serialize and write |
| 359 | let content = |
| 360 | toml::to_string_pretty(&full_config).map_err(|e| RemoteError::SerializeError { |
| 361 | message: e.to_string(), |
| 362 | })?; |
| 363 | |
| 364 | std::fs::write(path, content).map_err(|e| RemoteError::WriteError { |
| 365 | path: path.display().to_string(), |
| 366 | source: e, |
| 367 | })?; |
| 368 | |
| 369 | Ok(()) |
| 370 | } |
| 371 | |
| 372 | /// Check if there are no remotes configured. |
| 373 | pub fn is_empty(&self) -> bool { |