From 86b18205ea8bf85fbaeb001f187f8ea88a65ea43 Mon Sep 17 00:00:00 2001 From: diniamo Date: Thu, 22 Aug 2024 23:23:17 +0200 Subject: [PATCH] feat: more sensible cache management --- src/cache.rs | 13 +++++++------ src/main.rs | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 7324cae..cdd6699 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -36,12 +36,13 @@ impl Cache { self.update = true; } - pub fn delete(&mut self, command: &Option) { - if let Some(command) = command { - self.data.0.remove(command); - } else { - self.data.0.clear(); - } + pub fn delete(&mut self, command: &str) { + self.data.0.remove(command); + self.update = true; + } + + pub fn empty(&mut self) { + self.data.0.clear(); self.update = true; } } diff --git a/src/main.rs b/src/main.rs index 5a33da2..7e27277 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,15 +120,15 @@ fn main() -> ExitCode { index::update_database(); } - if let Some(ref delete_cache_opt) = args.delete_cache { + if args.empty_cache { if let Ok(ref mut cache) = cache { - cache.delete(delete_cache_opt); + cache.empty(); } } // The command may not be given if `--update` was specified. if args.cmd.is_empty() { - return if args.update || args.delete_cache.is_some() { + return if args.update || args.empty_cache { ExitCode::SUCCESS } else { ExitCode::FAILURE @@ -138,6 +138,12 @@ fn main() -> ExitCode { let command = &args.cmd[0]; let trail = &args.cmd[1..]; + if args.delete_entry { + if let Ok(ref mut cache) = cache { + cache.delete(command); + } + } + let derivation = match cache { Ok(mut cache) => cache.query(command).or_else(|| { index_database(command, &args.picker).map(|derivation| { @@ -240,11 +246,16 @@ struct Opt { #[clap(short = 'x', long = "print-path")] print_path: bool, - /// Delete a cache entry, or the entire cache if a value wasn't specified - #[clap(short = 'd', long = "delete-cache")] - delete_cache: Option>, + /// Empty the cache + #[clap(short, long = "empty-cache")] + empty_cache: bool, + + /// Overwrite the cache entry for the specified command. This is achieved by first deleting it + /// from the cache, then running comma as normal. + #[clap(short, long = "delete-entry")] + delete_entry: bool, /// Command to run - #[clap(required_unless_present_any = ["update", "delete_cache"], name = "cmd")] + #[clap(required_unless_present_any = ["update", "empty_cache"], name = "cmd")] cmd: Vec, }