Skip to content

Commit

Permalink
feat: optionally disable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickDaG committed Nov 17, 2024
1 parent ae61728 commit 869e77e
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,25 @@ fn run_command_or_open_shell(
fn main() -> ExitCode {
let args = Opt::parse();

let mut cache = Cache::new();
if let Err(ref e) = cache {
eprintln!("failed to initialize cache, disabling related functionality: {e}");
}
let mut cache = if !args.ignore_cache {
match Cache::new() {
Err(e) => {
eprintln!("failed to initialize cache, disabling related functionality: {e}");
None
}
Ok(x) => Some(x),
}
} else {
None
};

if args.update {
eprintln!("\"comma --update\" has been deprecated. either obtain a prebuilt database from https://github.com/Mic92/nix-index-database or use \"nix run 'nixpkgs#nix-index' --extra-experimental-features 'nix-command flakes'\"");
index::update_database();
}

if args.empty_cache {
if let Ok(ref mut cache) = cache {
if let Some(ref mut cache) = cache {
cache.empty();
}
}
Expand All @@ -146,7 +153,7 @@ fn main() -> ExitCode {
let trail = &args.cmd[1..];

if args.delete_entry {
if let Ok(ref mut cache) = cache {
if let Some(ref mut cache) = cache {
cache.delete(command);
}
}
Expand All @@ -170,13 +177,13 @@ fn main() -> ExitCode {
}

let derivation = match cache {
Ok(mut cache) => cache.query(command).or_else(|| {
Some(mut cache) => cache.query(command).or_else(|| {
index_database_pick(command, &args.picker).map(|derivation| {
cache.update(command, &derivation);
derivation
})
}),
Err(_) => index_database_pick(command, &args.picker),
None => index_database_pick(command, &args.picker),
};

let derivation = match derivation {
Expand Down Expand Up @@ -268,6 +275,10 @@ struct Opt {
#[clap(short, long = "empty-cache")]
empty_cache: bool,

/// Ignore the cache completely
#[clap(long = "ignore-cache", env = "COMMA_CACHING")]
ignore_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")]
Expand Down

0 comments on commit 869e77e

Please sign in to comment.