use crate::cache;
use crate::cache::{PruneOptions, PruneResults};
use crate::config::Settings;
use crate::dirs::CACHE;
use eyre::Result;
use number_prefix::NumberPrefix;
use std::time::Duration;
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, visible_alias = "p")]
pub struct CachePrune {
plugin: Option<Vec<String>>,
#[clap(long)]
dry_run: bool,
#[clap(long, short, action = clap::ArgAction::Count)]
verbose: u8,
}
impl CachePrune {
pub fn run(self) -> Result<()> {
let settings = Settings::get();
let cache_dirs = vec![CACHE.to_path_buf()];
let opts = PruneOptions {
dry_run: self.dry_run,
verbose: self.verbose > 0,
age: settings
.cache_prune_age_duration()
.unwrap_or(Duration::from_secs(30 * 24 * 60 * 60)),
};
let mut results = PruneResults { size: 0, count: 0 };
for p in &cache_dirs {
let r = cache::prune(p, &opts)?;
results.size += r.size;
results.count += r.count;
}
let count = results.count;
let size = bytes_str(results.size);
info!("cache pruned {count} files, {size} bytes");
Ok(())
}
}
fn bytes_str(bytes: u64) -> String {
match NumberPrefix::binary(bytes as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) => format!("{:.1} {}B", n, prefix),
}
}