mirror of
https://github.com/nix-community/comma.git
synced 2024-10-05 15:57:55 +03:00
don't check database date if it's not writable
This commit is contained in:
parent
6da3019f2a
commit
10c65154db
44
src/index.rs
44
src/index.rs
@ -1,5 +1,9 @@
|
||||
use std::{
|
||||
env, os::unix::prelude::CommandExt, path::{Path, PathBuf}, process::Command, time::{Duration, SystemTime}
|
||||
env,
|
||||
os::unix::prelude::CommandExt,
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
/// Update the local nix-index database.
|
||||
@ -9,34 +13,42 @@ pub fn update_database() {
|
||||
}
|
||||
|
||||
/// Prints a warning if the nix-index database is non-existent
|
||||
pub fn check_database_exists() {
|
||||
pub fn check_database_exists() -> Result<(), ()> {
|
||||
let database_file = get_database_file();
|
||||
if !database_file.exists() {
|
||||
eprintln!("Warning: Nix-index database does not exist, either obtain a prebuilt database from https://github.com/Mic92/nix-index-database or try updating with `nix run 'nixpkgs#nix-index' --extra-experimental-features 'nix-command flakes'`.");
|
||||
return Err(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Prints a warning if the nix-index database is out of date.
|
||||
pub fn check_database_updated() {
|
||||
if env::var("NIX_INDEX_DATABASE").is_ok() {
|
||||
// If the user has set NIX_INDEX_DATABASE, they are responsible for keeping it up to date
|
||||
// because if it's part of the nix store, the timestamp be always 1970-01-01.
|
||||
// This environment variable is set by nix-index-database.
|
||||
return;
|
||||
}
|
||||
let database_file = get_database_file();
|
||||
if is_database_old(&database_file) {
|
||||
eprintln!(
|
||||
"Warning: Nix-index database is older than 30 days, either obtain a prebuilt database from https://github.com/Mic92/nix-index-database or try updating with `nix run 'nixpkgs#nix-index' --extra-experimental-features 'nix-command flakes'`."
|
||||
);
|
||||
}
|
||||
if check_database_exists().is_ok() {
|
||||
if database_file.metadata().unwrap().permissions().readonly() {
|
||||
// If db is not writable, they are responsible for keeping it up to date
|
||||
// because if it's part of the nix store, the timestamp will always 1970-01-01.
|
||||
return;
|
||||
};
|
||||
if is_database_old(&database_file) {
|
||||
eprintln!(
|
||||
"Warning: Nix-index database is older than 30 days, either obtain a prebuilt database from https://github.com/Mic92/nix-index-database or try updating with `nix run 'nixpkgs#nix-index' --extra-experimental-features 'nix-command flakes'`."
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Get the location of the nix-index database file
|
||||
fn get_database_file() -> PathBuf {
|
||||
let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap();
|
||||
let cache_dir = base.get_cache_home();
|
||||
cache_dir.join("files")
|
||||
match env::var("NIX_INDEX_DATABASE") {
|
||||
Ok(db) => PathBuf::from(db),
|
||||
Err(_) => {
|
||||
let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap();
|
||||
let cache_dir = base.get_cache_home();
|
||||
cache_dir.join("files")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Test whether the database is more than 30 days old
|
||||
|
@ -92,7 +92,6 @@ fn main() -> ExitCode {
|
||||
.expect("failed to execute nix-locate");
|
||||
|
||||
if !nix_locate_output.status.success() {
|
||||
index::check_database_exists();
|
||||
match std::str::from_utf8(&nix_locate_output.stderr) {
|
||||
Ok(stderr) => eprintln!("nix-locate failed with: {stderr}"),
|
||||
Err(_) => eprint!("nix-locate failed"),
|
||||
@ -165,7 +164,12 @@ struct Opt {
|
||||
#[clap(short = 'P', long, env = "COMMA_PICKER", default_value = "fzy")]
|
||||
picker: String,
|
||||
|
||||
#[clap(short = 'F', long, env = "COMMA_NIXPKGS_FLAKE", default_value = "nixpkgs")]
|
||||
#[clap(
|
||||
short = 'F',
|
||||
long,
|
||||
env = "COMMA_NIXPKGS_FLAKE",
|
||||
default_value = "nixpkgs"
|
||||
)]
|
||||
nixpkgs_flake: String,
|
||||
|
||||
/// DEPRECATED Update nix-index database
|
||||
|
Loading…
Reference in New Issue
Block a user