use crate::core::types;
use std::path::Path;
pub(super) fn write_undo_progress(state_dir: &Path, machine: &str, progress: &types::UndoProgress) {
let dir = state_dir.join(machine);
let _ = std::fs::create_dir_all(&dir);
let path = dir.join("undo-progress.yaml");
if let Ok(yaml) = serde_yaml_ng::to_string(progress) {
let _ = std::fs::write(path, yaml);
}
}
pub(super) fn read_undo_progress(state_dir: &Path, machine: &str) -> Option<types::UndoProgress> {
let path = state_dir.join(machine).join("undo-progress.yaml");
let content = std::fs::read_to_string(path).ok()?;
serde_yaml_ng::from_str(&content).ok()
}
pub(super) fn init_undo_progress(
current: u32,
target: u32,
changes: &[String],
) -> types::UndoProgress {
let mut resources = std::collections::HashMap::new();
for c in changes {
let rid = c.split_whitespace().nth(1).unwrap_or("unknown");
resources.insert(
rid.to_string(),
types::ResourceProgress {
status: types::ResourceProgressStatus::Pending,
at: None,
},
);
}
types::UndoProgress {
generation_from: current,
generation_to: target,
started_at: crate::tripwire::eventlog::now_iso8601(),
status: types::UndoStatus::InProgress,
resources,
}
}
pub(super) fn mark_undo_progress_final<'a>(
state_dir: &Path,
machines: impl Iterator<Item = &'a String>,
ok: bool,
) {
let final_status = if ok {
types::UndoStatus::Completed
} else {
types::UndoStatus::Partial
};
for machine in machines {
if let Some(mut p) = read_undo_progress(state_dir, machine) {
p.status = final_status;
write_undo_progress(state_dir, machine, &p);
}
}
}