use std::io::Write as _;
use std::path::PathBuf;
use anyhow::Result;
use openmoat_hosts::Host;
use crate::canary::{self, Removed as Decoy};
use crate::home::{Home, user_home};
use crate::render::Deferred;
use crate::start::{self, Removed as Shim};
#[derive(Debug, Default)]
pub(super) struct Undone {
pub deleted: Vec<PathBuf>,
pub rewritten: Vec<PathBuf>,
}
pub(super) fn undo(
home: &Home,
hosts: &[Host],
every_agent: bool,
out: &mut Deferred,
) -> Result<Undone> {
let mut undone = Undone::default();
let (shims, rc) = start::remove(home, hosts)?;
for shim in shims {
match shim {
Shim::Deleted(path) => {
writeln!(out, "โ sandboxed start {} deleted", path.display())?;
undone.deleted.push(path);
}
Shim::Changed(path) => writeln!(
out,
"! sandboxed start {} kept: it changed since `openmoat init` wrote it",
path.display()
)?,
}
}
if let Some(rc) = rc {
writeln!(out, "โ sandboxed start {rc}")?;
}
if every_agent {
for (path, removed) in canary::remove(home, &user_home()?)? {
let line = match removed {
Decoy::Deleted => format!("โ decoy key {} deleted", path.display()),
Decoy::Changed => format!(
"! decoy key {} kept: it changed since `openmoat init` planted it",
path.display()
),
Decoy::Gone => format!("ยท decoy key {} was gone already", path.display()),
};
writeln!(out, "{line}")?;
}
}
undone.rewritten = [canary::record_path(home), start::record_path(home)]
.into_iter()
.filter(|p| p.is_file())
.collect();
Ok(undone)
}