Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/rv-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ impl Cache {
pub fn prune(&self) -> Result<Removal, io::Error> {
let mut summary = Removal::default();

if !&self.root.exists() {
debug!("No cache found at: {}", &self.root);
return Ok(summary);
}

// Remove any top-level directories that are unused. These typically represent
// outdated cache buckets (e.g., `ruby-v0`, when latest is `ruby-v0`).
for entry in fs_err::read_dir(&self.root)? {
Expand Down
13 changes: 13 additions & 0 deletions crates/rv/src/commands/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct CacheCommandArgs {
pub enum CacheCommand {
#[command(about = "Clear the cache")]
Clean,
#[command(about = "Prune all unused entries from the cache")]
Prune,
#[command(about = "Show the cache directory")]
Dir,
}
Expand All @@ -41,3 +43,14 @@ pub fn cache_clean(config: &Config) -> io::Result<()> {
);
Ok(())
}

pub fn cache_prune(config: &Config) -> io::Result<()> {
let removal = config.cache.prune()?;
let num_bytes_cleaned = ByteSize::b(removal.bytes).display().iec_short();
println!(
"Removed {} directories, totalling {}",
removal.dirs.cyan(),
num_bytes_cleaned.cyan()
);
Ok(())
}
3 changes: 2 additions & 1 deletion crates/rv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tracing_subscriber::{EnvFilter, layer::SubscriberExt as _, util::SubscriberI
pub mod commands;
pub mod config;

use crate::commands::cache::{CacheCommand, CacheCommandArgs, cache_clean, cache_dir};
use crate::commands::cache::{CacheCommand, CacheCommandArgs, cache_clean, cache_dir, cache_prune};
use crate::commands::ruby::dir::dir as ruby_dir;
use crate::commands::ruby::find::find as ruby_find;
use crate::commands::ruby::install::install as ruby_install;
Expand Down Expand Up @@ -293,6 +293,7 @@ async fn run() -> Result<()> {
Commands::Cache(cache) => match cache.command {
CacheCommand::Dir => cache_dir(&config)?,
CacheCommand::Clean => cache_clean(&config)?,
CacheCommand::Prune => cache_prune(&config)?,
},
Commands::Shell(shell) => match shell.command {
ShellCommand::Init { shell } => shell_init(&config, shell)?,
Expand Down