use eclipse_core::config::{self, Config};
use std::path::Path;
pub fn table(path: &Path, profile: Option<&str>) -> String {
let cfg = match Config::load_with_profile(Some(path), profile, &config::process_env) {
Ok(cfg) => cfg,
Err(e) => return format!("{} does not load: {e}", path.display()),
};
let mut out = format!(
"{} — profile {}, {} key(s)\n",
path.display(),
cfg.profile_name(),
cfg.settings().len()
);
for s in cfg.settings() {
out.push_str(&format!(
" {} = {}{}\n",
s.path(),
shown(&s),
if s.is_default() { " (default)" } else { "" }
));
}
out.push_str(
"\n/set <key> <value> writes one; `eclipse config keys` prints this outside the interface",
);
out
}
fn shown(s: &config::Setting) -> String {
if s.secret {
return if s.value.is_some() {
"(set)".to_string()
} else {
"(not set)".to_string()
};
}
s.rendered()
}
pub fn set_one(
path: &Path,
profile: Option<&str>,
key: &str,
value: &str,
) -> Result<String, String> {
let text = std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
let typed = config::parse_value(value);
let edited = config::set_in_text(&text, key, &typed)?;
config::save_config_text(path, &edited).map_err(|e| e.to_string())?;
let cfg = Config::load_with_profile(Some(path), profile, &config::process_env)
.map_err(|e| format!("written, but the file no longer loads: {e}"))?;
let setting = cfg
.setting(key)
.ok_or_else(|| format!("`{key}` was written but nothing in this build reads it"))?;
Ok(format!(
"{} = {}\n{}",
setting.path(),
shown(&setting),
setting.applies.as_str()
))
}