use std::fs;
use std::path::PathBuf;
use anyhow::{Context, Result};
fn global_config_path() -> Result<PathBuf> {
let home = dirs::home_dir().context("cannot find home directory")?;
Ok(home.join(".elisym").join("config.toml"))
}
pub(crate) fn get_default_agent() -> Option<String> {
let path = global_config_path().ok()?;
let contents = fs::read_to_string(&path).ok()?;
let table: toml::Table = toml::from_str(&contents).ok()?;
table
.get("default_agent")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}
pub(crate) fn set_default_agent(name: &str) -> Result<()> {
let path = global_config_path()?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let mut table: toml::Table = fs::read_to_string(&path)
.ok()
.and_then(|c| toml::from_str(&c).ok())
.unwrap_or_default();
table.insert(
"default_agent".to_string(),
toml::Value::String(name.to_string()),
);
let toml_str =
toml::to_string_pretty(&table).context("failed to serialize global config")?;
fs::write(&path, toml_str).context("failed to write ~/.elisym/config.toml")?;
Ok(())
}