MCPcopy Index your code
hub / github.com/ChrisFeldmeier/OpenCodeRust / update_global_config

Function update_global_config

crates/opencode-config/src/loader.rs:1362–1393  ·  view source on GitHub ↗

Update global config by merging a patch.

(patch: &Config)

Source from the content-addressed store, hash-verified

1360
1361/// Update global config by merging a patch.
1362pub 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)]
1396mod tests {

Callers

nothing calls this directly

Calls 6

get_global_config_pathFunction · 0.85
parse_jsoncFunction · 0.85
findMethod · 0.80
existsMethod · 0.80
mergeMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected