Update global config by merging a patch.
(patch: &Config)
| 1360 | |
| 1361 | /// Update global config by merging a patch. |
| 1362 | pub fn update_global_config(patch: &Config) -> Result<()> { |
| 1363 | let global_path = get_global_config_path(); |
| 1364 | |
| 1365 | // Try to find existing global config file |
| 1366 | let config_path = ["jsonc", "json"] |
| 1367 | .iter() |
| 1368 | .map(|ext| global_path.with_extension(ext)) |
| 1369 | .find(|p| p.exists()) |
| 1370 | .unwrap_or_else(|| global_path.with_extension("json")); |
| 1371 | |
| 1372 | // Ensure parent directory exists |
| 1373 | if let Some(parent) = config_path.parent() { |
| 1374 | fs::create_dir_all(parent)?; |
| 1375 | } |
| 1376 | |
| 1377 | let existing = if config_path.exists() { |
| 1378 | let content = fs::read_to_string(&config_path)?; |
| 1379 | parse_jsonc(&content).unwrap_or_default() |
| 1380 | } else { |
| 1381 | Config::default() |
| 1382 | }; |
| 1383 | |
| 1384 | let mut merged = existing; |
| 1385 | merged.merge(patch.clone()); |
| 1386 | |
| 1387 | let json = |
| 1388 | serde_json::to_string_pretty(&merged).with_context(|| "Failed to serialize config")?; |
| 1389 | fs::write(&config_path, json) |
| 1390 | .with_context(|| format!("Failed to write global config to {:?}", config_path))?; |
| 1391 | |
| 1392 | Ok(()) |
| 1393 | } |
| 1394 | |
| 1395 | #[cfg(test)] |
| 1396 | mod tests { |
nothing calls this directly
no test coverage detected