mirror of
https://github.com/nix-community/comma.git
synced 2024-09-11 03:55:24 +03:00
Factor out database checks into a separate module.
This commit is contained in:
parent
9668ec3ef1
commit
f1cffeeabe
40
src/index.rs
Normal file
40
src/index.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use std::{
|
||||
os::unix::prelude::CommandExt,
|
||||
process::Command,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
/// Update the local nix-index database.
|
||||
pub fn update_database() {
|
||||
println!("Updating nix-index database, takes around 5 minutes.");
|
||||
Command::new("nix-index").exec();
|
||||
}
|
||||
|
||||
/// Prints warnings if the nix-index database is non-existent or out of date.
|
||||
pub fn check_database() {
|
||||
let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap();
|
||||
let cache_dir = base.get_cache_home();
|
||||
let database_file = cache_dir.join("files");
|
||||
if !database_file.exists() {
|
||||
println!("Warning: Nix-index database does not exist, try updating with `--update`.");
|
||||
} else if is_database_old(database_file) {
|
||||
println!(
|
||||
"Warning: Nix-index database is older than 30 days, try updating with `--update`."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Test whether the database is more than 30 days old
|
||||
fn is_database_old(database_file: std::path::PathBuf) -> bool {
|
||||
let modified = match database_file.metadata() {
|
||||
Ok(metadata) => metadata.modified().unwrap_or_else(|_| SystemTime::now()),
|
||||
Err(_) => return false,
|
||||
};
|
||||
let time_since_modified = SystemTime::now()
|
||||
.duration_since(modified)
|
||||
.unwrap_or(Duration::new(0, 0));
|
||||
if time_since_modified > Duration::from_secs(30 * 24 * 60 * 60) {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
35
src/main.rs
35
src/main.rs
@ -1,3 +1,4 @@
|
||||
mod index;
|
||||
use std::{
|
||||
env,
|
||||
io::Write,
|
||||
@ -7,7 +8,6 @@ use std::{
|
||||
|
||||
use clap::crate_version;
|
||||
use clap::Parser;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
fn pick(picker: &str, derivations: &[&str]) -> Option<String> {
|
||||
let mut picker_process = Command::new(&picker)
|
||||
@ -55,35 +55,6 @@ fn run_command(use_channel: bool, choice: &str, command: &str, trail: &[String])
|
||||
run_cmd.exec();
|
||||
}
|
||||
|
||||
/// Test whether the database is more than 30 days old
|
||||
fn is_database_old(database_file: std::path::PathBuf) -> bool {
|
||||
let modified = match database_file.metadata() {
|
||||
Ok(metadata) => metadata.modified().unwrap_or_else(|_| SystemTime::now()),
|
||||
Err(_) => return false,
|
||||
};
|
||||
let time_since_modified = SystemTime::now()
|
||||
.duration_since(modified)
|
||||
.unwrap_or(Duration::new(0, 0));
|
||||
if time_since_modified > Duration::from_secs(30 * 24 * 60 * 60) {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Prints warnings if the nix-index database is non-existent or out of date.
|
||||
fn check_database() {
|
||||
let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap();
|
||||
let cache_dir = base.get_cache_home();
|
||||
let database_file = cache_dir.join("files");
|
||||
if !database_file.exists() {
|
||||
println!("Warning: Nix-index database does not exist, try updating with `--update`.");
|
||||
} else if is_database_old(database_file) {
|
||||
println!(
|
||||
"Warning: Nix-index database is older than 30 days, try updating with `--update`."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let args = Opt::parse();
|
||||
|
||||
@ -92,10 +63,10 @@ fn main() -> ExitCode {
|
||||
|
||||
if args.update {
|
||||
println!("Updating nix-index database, takes around 5 minutes.");
|
||||
Command::new("nix-index").exec();
|
||||
index::update_database();
|
||||
}
|
||||
|
||||
check_database();
|
||||
index::check_database();
|
||||
|
||||
let attrs = Command::new("nix-locate")
|
||||
.args(["--top-level", "--minimal", "--at-root", "--whole-name"])
|
||||
|
Loading…
Reference in New Issue
Block a user