use std::path::Path;
use anyhow::{Context, Result};
use sdivi_config::Config;
use sdivi_core::input::{ThresholdOverrideInput, ThresholdsInput};
use sdivi_core::{compute_thresholds_check, ExitCode};
use sdivi_pipeline::store::latest_snapshot;
use sdivi_pipeline::{current_timestamp, Pipeline, WriteMode};
use crate::output;
pub fn run(repo_root: &Path, config: &Config, no_write: bool, format: &str) -> Result<ExitCode> {
let snapshot_dir = repo_root.join(&config.snapshots.dir);
let prior = latest_snapshot(&snapshot_dir)
.with_context(|| format!("failed to read snapshot dir: {}", snapshot_dir.display()))?;
let pipeline = Pipeline::new(config.clone(), super::all_adapters());
let timestamp = current_timestamp();
let mode = if no_write {
WriteMode::EphemeralForCheck
} else {
WriteMode::Persist
};
eprintln!("sdivi: checking repository at {}", repo_root.display());
let current = pipeline.snapshot_with_mode(repo_root, None, ×tamp, mode)?;
let summary = Pipeline::delta(prior.as_ref(), ¤t);
let today = chrono::Local::now().date_naive();
let thresholds = thresholds_input(config, today);
let check_result = compute_thresholds_check(&summary, &thresholds);
match format {
"json" => output::json::print_check(&check_result, &summary)?,
_ => output::text::print_check(&check_result, &summary),
}
if check_result.breached {
Ok(ExitCode::ThresholdExceeded)
} else {
Ok(ExitCode::Success)
}
}
fn thresholds_input(config: &Config, today: chrono::NaiveDate) -> ThresholdsInput {
let t = &config.thresholds;
let overrides = t
.overrides
.iter()
.map(|(k, v)| {
(
k.clone(),
ThresholdOverrideInput {
pattern_entropy_rate: v.pattern_entropy_rate,
convention_drift_rate: v.convention_drift_rate,
coupling_delta_rate: v.coupling_delta_rate,
boundary_violation_rate: v.boundary_violation_rate,
expires: v.expires.clone(),
},
)
})
.collect();
ThresholdsInput {
pattern_entropy_rate: t.pattern_entropy_rate,
convention_drift_rate: t.convention_drift_rate,
coupling_delta_rate: t.coupling_delta_rate,
boundary_violation_rate: t.boundary_violation_rate,
overrides,
today,
}
}