Skip to content

Commit

Permalink
Factor out database checks into a separate module.
Browse files Browse the repository at this point in the history
  • Loading branch information
arcuru committed Jul 11, 2022
1 parent 9668ec3 commit f1cffee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
40 changes: 40 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod index;
use std::{
env,
io::Write,
Expand All @@ -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)
Expand Down Expand Up @@ -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();

Expand All @@ -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"])
Expand Down

0 comments on commit f1cffee

Please sign in to comment.