use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Origin {
Default,
User,
Frontier,
Env,
}
impl Origin {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Default => "default",
Self::User => "user",
Self::Frontier => "frontier",
Self::Env => "env",
}
}
}
#[derive(Debug)]
pub(crate) struct KeySpec {
pub key: &'static str,
pub default: &'static str,
pub env: &'static str,
pub frontier: Option<bool>,
pub allowed: &'static [&'static str],
pub help: &'static str,
}
pub(crate) const KEYS: &[KeySpec] = &[
KeySpec {
key: "hub.url",
default: crate::cli_identity::DEFAULT_HUB,
env: "VELA_HUB_URL",
frontier: None, allowed: &[],
help: "the hub this machine publishes to and verifies against",
},
KeySpec {
key: "publish.git_push",
default: "off",
env: "VELA_PUBLISH_GIT_PUSH",
frontier: Some(true),
allowed: &["auto", "off"],
help: "push the decision commit automatically (default off; publish with git push or --push)",
},
KeySpec {
key: "ui.color",
default: "auto",
env: "VELA_UI_COLOR",
frontier: None,
allowed: &["auto", "always", "never"],
help: "terminal color (NO_COLOR is always respected)",
},
KeySpec {
key: "ui.advice",
default: "on",
env: "VELA_ADVICE",
frontier: None,
allowed: &["on", "off", "0", "1"],
help: "hint: lines naming the next command on errors",
},
KeySpec {
key: "core.editor",
default: "",
env: "VELA_EDITOR",
frontier: None,
allowed: &[],
help: "editor for sign-session notes (falls back to VISUAL/EDITOR)",
},
KeySpec {
key: "work.lease_ttl_seconds",
default: "86400",
env: "VELA_WORK_LEASE_TTL_SECONDS",
frontier: Some(false), allowed: &[],
help: "default lease length for `work`/claim",
},
KeySpec {
key: "mcp.profile",
default: "read-only",
env: "VELA_MCP_PROFILE",
frontier: Some(false),
allowed: &["read-only", "draft"],
help: "default serve/agents-sync MCP profile",
},
];
pub(crate) fn spec(key: &str) -> Option<&'static KeySpec> {
KEYS.iter().find(|s| s.key == key)
}
fn user_config_path() -> Option<PathBuf> {
std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".vela").join("config.toml"))
}
fn frontier_config_path(frontier: &Path) -> PathBuf {
frontier.join(".vela").join("config.toml")
}
fn load_flat(path: &Path) -> BTreeMap<String, String> {
let mut out = BTreeMap::new();
let Ok(raw) = std::fs::read_to_string(path) else {
return out;
};
let Ok(value) = raw.parse::<toml::Value>() else {
eprintln!(" warning: {} is not valid TOML; ignored", path.display());
return out;
};
if let toml::Value::Table(sections) = value {
for (section, body) in sections {
match body {
toml::Value::Table(entries) => {
for (k, v) in entries {
let vs = match v {
toml::Value::String(s) => s,
other => other.to_string(),
};
out.insert(format!("{section}.{k}"), vs);
}
}
other => {
out.insert(section, other.to_string());
}
}
}
}
out
}
pub(crate) fn resolve(key: &str, frontier: Option<&Path>) -> (String, Origin) {
resolve_with(
key,
frontier,
|name| std::env::var(name).ok(),
user_config_path(),
)
}
fn resolve_with(
key: &str,
frontier: Option<&Path>,
env_get: impl Fn(&str) -> Option<String>,
user_path: Option<PathBuf>,
) -> (String, Origin) {
let Some(spec) = spec(key) else {
return (String::new(), Origin::Default);
};
if let Some(v) = env_get(spec.env)
&& !v.trim().is_empty()
{
return (v, Origin::Env);
}
let user_val = user_path
.map(|p| load_flat(&p))
.and_then(|m| m.get(key).cloned());
if let Some(dir) = frontier
&& let Some(frontier_rule) = spec.frontier
{
let fmap = load_flat(&frontier_config_path(dir));
if let Some(fv) = fmap.get(key) {
if frontier_rule {
if fv == "off" {
return (fv.clone(), Origin::Frontier);
}
} else {
if user_val.is_none() {
return (fv.clone(), Origin::Frontier);
}
}
}
}
if let Some(v) = user_val {
return (v, Origin::User);
}
(spec.default.to_string(), Origin::Default)
}
pub(crate) fn warn_unknown_frontier_keys(frontier: &Path) {
let path = frontier_config_path(frontier);
if !path.exists() {
return;
}
for key in load_flat(&path).keys() {
match spec(key) {
Some(s) if s.frontier.is_some() => {}
_ => eprintln!(
" warning: frontier config key `{key}` is not frontier-scoped; ignored \
(routing/identity/UI keys are user-scope only)"
),
}
}
}
fn validate_value(key: &str, value: &str) -> Result<&'static KeySpec, String> {
let spec = spec(key).ok_or_else(|| {
format!("unknown key `{key}` โ the set is closed; `vela config list` shows it in full")
})?;
if !spec.allowed.is_empty() && !spec.allowed.contains(&value) {
return Err(format!(
"`{value}` is not a valid {key} (allowed: {})",
spec.allowed.join(", ")
));
}
Ok(spec)
}
pub(crate) fn set(key: &str, value: &str, frontier: Option<&Path>) -> Result<PathBuf, String> {
let spec = validate_value(key, value)?;
let path = match frontier {
Some(dir) => {
if spec.frontier.is_none() {
return Err(format!(
"`{key}` is user-scope only (a cloned frontier must never control it); \
drop --frontier"
));
}
if spec.frontier == Some(true) && value != "off" {
return Err(format!(
"frontier scope may only NARROW `{key}` (set `off`, or set it at user scope)"
));
}
frontier_config_path(dir)
}
None => user_config_path().ok_or("no HOME")?,
};
set_at(&path, key, value)?;
Ok(path)
}
fn set_at(path: &Path, key: &str, value: &str) -> Result<(), String> {
let mut root: toml::Value = std::fs::read_to_string(path)
.ok()
.and_then(|raw| raw.parse().ok())
.unwrap_or(toml::Value::Table(Default::default()));
let (section, name) = key.split_once('.').ok_or("keys are section.name")?;
let table = root
.as_table_mut()
.ok_or("config root must be a table")?
.entry(section.to_string())
.or_insert(toml::Value::Table(Default::default()));
table
.as_table_mut()
.ok_or_else(|| format!("[{section}] is not a table"))?
.insert(name.to_string(), toml::Value::String(value.to_string()));
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
std::fs::write(
path,
toml::to_string_pretty(&root).map_err(|e| e.to_string())?,
)
.map_err(|e| e.to_string())
}
pub(crate) fn unset(key: &str, frontier: Option<&Path>) -> Result<(), String> {
spec(key).ok_or_else(|| format!("unknown key `{key}`"))?;
let path = match frontier {
Some(dir) => frontier_config_path(dir),
None => user_config_path().ok_or("no HOME")?,
};
let Ok(raw) = std::fs::read_to_string(&path) else {
return Ok(());
};
let mut root: toml::Value = raw.parse().map_err(|e| format!("parse: {e}"))?;
let (section, name) = key.split_once('.').ok_or("keys are section.name")?;
if let Some(table) = root
.as_table_mut()
.and_then(|t| t.get_mut(section))
.and_then(|s| s.as_table_mut())
{
table.remove(name);
}
std::fs::write(
&path,
toml::to_string_pretty(&root).map_err(|e| e.to_string())?,
)
.map_err(|e| e.to_string())
}
pub(crate) fn cmd_config_get(key: &str, frontier: Option<&Path>, json: bool) {
if spec(key).is_none() {
crate::ui::fail_with(
crate::ui::ErrorKind::NotFound,
&format!("unknown key `{key}`"),
Some("`vela config list` shows the whole closed set"),
);
}
let (value, origin) = resolve(key, frontier);
if json {
println!(
"{}",
serde_json::json!({"ok": true, "command": "config", "key": key, "value": value, "origin": origin.as_str()})
);
} else {
println!("{value}");
}
}
pub(crate) fn cmd_config_set(key: &str, value: &str, frontier: Option<&Path>, json: bool) {
match set(key, value, frontier) {
Ok(path) => {
if json {
println!(
"{}",
serde_json::json!({"ok": true, "command": "config", "key": key, "value": value, "file": path.display().to_string()})
);
} else {
println!(" ยท {key} = {value} ({})", path.display());
}
}
Err(e) => crate::ui::fail_with(crate::ui::ErrorKind::Usage, &e, None),
}
}
pub(crate) fn cmd_config_unset(key: &str, frontier: Option<&Path>, json: bool) {
match unset(key, frontier) {
Ok(()) => {
if json {
println!(
"{}",
serde_json::json!({"ok": true, "command": "config", "key": key, "unset": true})
);
} else {
println!(" ยท {key} unset");
}
}
Err(e) => crate::ui::fail_with(crate::ui::ErrorKind::Usage, &e, None),
}
}
pub(crate) fn cmd_config_list(frontier: Option<&Path>, json: bool) {
if let Some(dir) = frontier {
warn_unknown_frontier_keys(dir);
}
if json {
let rows: Vec<_> = KEYS
.iter()
.map(|s| {
let (value, origin) = resolve(s.key, frontier);
serde_json::json!({"key": s.key, "value": value, "origin": origin.as_str(), "help": s.help})
})
.collect();
println!(
"{}",
serde_json::json!({"ok": true, "command": "config", "keys": rows})
);
return;
}
crate::ui::header("CONFIG", "", Some("the whole closed set; origins shown"));
for s in KEYS {
let (value, origin) = resolve(s.key, frontier);
println!(" {:<28} {:<12} [{}]", s.key, value, origin.as_str());
println!(" {}", s.help);
}
println!("\n identity + keys: `vela id` ยท what may enter the record: `vela policy`");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn closed_set_and_validation() {
let tmp = tempfile::TempDir::new().unwrap();
let user = tmp.path().join("config.toml");
assert!(spec("no.such_key").is_none());
assert!(set_at(&user, "publish.git_push", "off").is_ok());
let err = validate_value("publish.git_push", "sometimes").unwrap_err();
assert!(err.contains("not a valid"), "{err}");
let (v, o) = resolve_with("publish.git_push", None, |_| None, Some(user.clone()));
assert_eq!((v.as_str(), o), ("off", Origin::User));
}
#[test]
fn frontier_scope_is_protected() {
let tmp = tempfile::TempDir::new().unwrap();
let frontier = tmp.path().join("f");
std::fs::create_dir_all(frontier.join(".vela")).unwrap();
assert!(set("hub.url", "https://evil.example", Some(&frontier)).is_err());
assert!(set("publish.git_push", "auto", Some(&frontier)).is_err());
set("publish.git_push", "off", Some(&frontier)).unwrap();
let (v, o) = resolve_with("publish.git_push", Some(&frontier), |_| None, None);
assert_eq!((v.as_str(), o), ("off", Origin::Frontier));
std::fs::write(
frontier.join(".vela/config.toml"),
"[hub]\nurl = \"https://evil.example\"\n[publish]\ngit_push = \"off\"\n",
)
.unwrap();
let (v, o) = resolve_with("hub.url", Some(&frontier), |_| None, None);
assert_ne!(v, "https://evil.example");
assert_ne!(o, Origin::Frontier);
}
#[test]
fn env_beats_files_and_default_falls_through() {
let (v, o) = resolve_with(
"ui.color",
None,
|name| (name == "VELA_UI_COLOR").then(|| "never".to_string()),
None,
);
assert_eq!((v.as_str(), o), ("never", Origin::Env));
let (v, o) = resolve_with("ui.color", None, |_| None, None);
assert_eq!((v.as_str(), o), ("auto", Origin::Default));
}
}