use std::fmt;
use anyhow::Result;
use clap::Args;
use pimalaya_cli::printer::Printer;
use serde::Serialize;
use crate::cli::{StoreFlags, bytes, report};
#[derive(Debug, Args)]
pub struct GcCommand;
impl GcCommand {
pub fn execute(self, printer: &mut impl Printer, store: &StoreFlags) -> Result<()> {
let collected = store.owner()?.collect_garbage().map_err(report)?;
printer.out(GcOutput {
objects: collected.objects,
blobs: collected.blobs,
bytes: collected.bytes,
})
}
}
#[derive(Debug, Serialize)]
pub struct GcOutput {
pub objects: usize,
pub blobs: usize,
pub bytes: u64,
}
impl fmt::Display for GcOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.objects == 0 && self.blobs == 0 {
return writeln!(f, "Nothing to collect: every body is still referenced");
}
writeln!(
f,
"Collected {} object(s) and {} blob file(s), freeing {}",
self.objects,
self.blobs,
bytes(self.bytes)
)
}
}