use anyhow::Result;
use podbox::cli::OutputFormat;
use podbox::config::Config;
use podbox::diff;
pub fn run_diff(
config: &Config,
name: &str,
username: &str,
apply: bool,
output: OutputFormat,
) -> Result<()> {
let result = diff::compute(config, name, username)?;
match output {
OutputFormat::Json => {
println!("{}", serde_json::to_string_pretty(&result)?);
}
OutputFormat::Text => {
if !result.has_drift {
println!("✓ No drift detected — all declared packages are installed.");
return Ok(());
}
println!("{}", diff::format_report(&result));
}
}
if apply {
let definition_path = podbox::config::find_definition()
.ok_or_else(|| anyhow::anyhow!("No definition file found to patch"))?;
let original = std::fs::read_to_string(&definition_path)?;
let mut reconciled: Vec<String> = result
.config_install
.iter()
.filter(|p| !result.missing.contains(p))
.cloned()
.collect();
for pkg in &result.unexpected {
if !reconciled.contains(pkg) {
reconciled.push(pkg.clone());
}
}
reconciled.sort();
let patched = diff::patch_toml(&original, &reconciled)?;
std::fs::write(&definition_path, patched)?;
println!(
"\n✓ Updated {} with reconciled package list.",
definition_path.display()
);
}
Ok(())
}