use super::{READ_LIMIT, ToolCx, read};
use crate::DownloadItem;
use crate::cli::vagrant;
use crate::tool_output::vagrant::InstalledBox;
pub(super) async fn recording<T>(cx: &ToolCx<'_>, up: impl Future<Output = T>) -> T {
let before = before(cx).await;
let ended = up.await;
if let Some(before) = before {
record_added(cx, &before).await;
}
ended
}
async fn before(cx: &ToolCx<'_>) -> Option<Vec<InstalledBox>> {
cx.fetched?;
vagrant::boxes(&read(cx, READ_LIMIT))
.await
.inspect_err(|e| tracing::warn!(run = %cx.run, error = %e, "Vagrant's boxes couldn't be listed: what the up fetches isn't recorded"))
.ok()
}
async fn record_added(cx: &ToolCx<'_>, before: &[InstalledBox]) {
let Some(record) = cx.fetched else { return };
match vagrant::boxes(&read(cx, READ_LIMIT)).await {
Ok(after) => after
.into_iter()
.filter(|held| !before.contains(held))
.for_each(|added| record(DownloadItem::of_box(added))),
Err(e) => tracing::warn!(run = %cx.run, error = %e, "Vagrant's boxes couldn't be listed: what the up fetched isn't recorded"),
}
}