use std::path::PathBuf;
pub const DEFAULT_AGENT_ID: &str = "asd-cli-user";
#[derive(Debug, Clone)]
pub struct Config {
pub db_path: PathBuf,
pub agent_id: String,
pub policy_path: Option<PathBuf>,
pub audit_log_path: Option<PathBuf>,
pub brief: bool,
}
impl Config {
pub fn resolve(
explicit_db: Option<PathBuf>,
explicit_policy: Option<PathBuf>,
explicit_audit_log: Option<PathBuf>,
) -> Self {
Self::resolve_with_brief(
explicit_db,
explicit_policy,
explicit_audit_log,
false,
true,
)
}
pub fn resolve_with_brief(
explicit_db: Option<PathBuf>,
explicit_policy: Option<PathBuf>,
explicit_audit_log: Option<PathBuf>,
brief_flag: bool,
local_only: bool,
) -> Self {
let db_path = explicit_db
.or_else(|| std::env::var_os("ASD_DB").map(PathBuf::from))
.unwrap_or_else(|| {
if local_only {
PathBuf::from("./.asd-state.db")
} else {
resolve_default_db()
}
});
let policy_path =
explicit_policy.or_else(|| std::env::var_os("ASD_POLICY").map(PathBuf::from));
let audit_log_path =
explicit_audit_log.or_else(|| std::env::var_os("ASD_AUDIT_LOG").map(PathBuf::from));
let brief = brief_flag
|| std::env::var("ASD_FORMAT")
.map(|v| v.eq_ignore_ascii_case("brief"))
.unwrap_or(false);
Self {
db_path,
agent_id: DEFAULT_AGENT_ID.to_string(),
policy_path,
audit_log_path,
brief,
}
}
}
fn resolve_default_db() -> PathBuf {
let cwd_db = PathBuf::from("./.asd-state.db");
if cwd_db.exists() {
return cwd_db;
}
if let Some(found) = agentstatedeveloper_core::registry::find_db_upwards() {
return found;
}
if let Some(active) = registry_active_db() {
return active;
}
cwd_db
}
fn registry_active_db() -> Option<PathBuf> {
let reg = agentstatedeveloper_core::registry::Registry::load().ok()?;
reg.active().map(|e| e.path.clone())
}