Skip to content

Commit

Permalink
feat: more sensible cache management
Browse files Browse the repository at this point in the history
  • Loading branch information
diniamo committed Aug 22, 2024
1 parent 5273f2c commit 86b1820
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ impl Cache {
self.update = true;
}

pub fn delete(&mut self, command: &Option<String>) {
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;
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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| {
Expand Down Expand Up @@ -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<Option<String>>,
/// 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<String>,
}

0 comments on commit 86b1820

Please sign in to comment.