use fallow_config::{CommandKind, EnvVar, RunInputs};
use fallow_types::sourced::{Scope, Source, Sourced};
use super::env_policy::applies;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AuditBaseDecision {
pub named: Option<Sourced<String>>,
pub fallback: Option<Sourced<String>>,
}
#[must_use]
pub fn decide_audit_base(inputs: &RunInputs) -> AuditBaseDecision {
if inputs.command != CommandKind::Audit {
return AuditBaseDecision::default();
}
let read = |var: EnvVar| {
if !applies(var, inputs.surface(), inputs.command) {
return None;
}
inputs
.env()
.get(var)
.and_then(|value| value.to_str())
.map(str::to_owned)
};
let environment = |value: String, var: EnvVar| {
Sourced::new(
value,
Source::Environment { name: var.name() },
Scope::Global,
)
};
let named = inputs
.audit_base
.as_ref()
.map(|given| {
Sourced::new(
given.value.clone(),
Source::Given {
origin: given.origin,
},
Scope::Global,
)
})
.or_else(|| {
read(EnvVar::AuditBase)
.map(|value| value.trim().to_owned())
.filter(|value| !value.is_empty())
.map(|value| environment(value, EnvVar::AuditBase))
});
let fallback = read(EnvVar::ChangedSince)
.filter(|value| !value.is_empty())
.map(|value| environment(value, EnvVar::ChangedSince));
AuditBaseDecision { named, fallback }
}
#[cfg(test)]
#[path = "audit_base_tests.rs"]
mod tests;