From 55dd39871dde030c3efaa87c2b3b1b104a2be097 Mon Sep 17 00:00:00 2001 From: David Houston Date: Tue, 20 Sep 2022 19:11:55 -0400 Subject: [PATCH 1/6] Index.rs: Add db writeability check Signed-off-by: David Houston --- src/index.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/index.rs b/src/index.rs index 508b80f..70749e4 100644 --- a/src/index.rs +++ b/src/index.rs @@ -26,15 +26,16 @@ pub fn check_database() { /// 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()), + let metadata = match database_file.metadata() { + Ok(metadata) => metadata, Err(_) => return false, }; - let time_since_modified = SystemTime::now() - .duration_since(modified) + + let time_since_modified = metadata + .modified() + .unwrap_or_else(|_| SystemTime::now()) + .elapsed() .unwrap_or(Duration::new(0, 0)); - if time_since_modified > Duration::from_secs(30 * 24 * 60 * 60) { - return true; - } - false + + time_since_modified > Duration::from_secs(30 * 24 * 60 * 60) && !metadata.permissions().readonly() } From 1fbd317b4a739c96accb27ff6ca4db2e5b7c7cfb Mon Sep 17 00:00:00 2001 From: Artturin Date: Sun, 20 Nov 2022 17:57:03 +0200 Subject: [PATCH 2/6] Show error and exit upon nix-locate failure Co-authored-by: gabriel-doriath-dohler --- src/main.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index f70be66..3a7456a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,12 +72,21 @@ fn main() -> ExitCode { index::check_database(); - let attrs = Command::new("nix-locate") + let nix_locate_output = Command::new("nix-locate") .args(["--top-level", "--minimal", "--at-root", "--whole-name"]) .arg(format!("/bin/{}", command)) .output() - .expect("failed to execute nix-locate") - .stdout; + .expect("failed to execute nix-locate"); + + if !nix_locate_output.status.success() { + match std::str::from_utf8(&nix_locate_output.stderr) { + Ok(stderr) => eprintln!("nix-locate failed with: {}", stderr), + Err(_) => eprint!("nix-locate failed"), + } + return ExitCode::FAILURE; + } + + let attrs = nix_locate_output.stdout; if attrs.is_empty() { eprintln!("No executable `{}` found in nix-index database.", command); From 78456e7c5f320897130c93e16cbc5655dbddcc4e Mon Sep 17 00:00:00 2001 From: Artturin Date: Sun, 20 Nov 2022 18:12:32 +0200 Subject: [PATCH 3/6] add .direnv to gitignore --- .gitignore | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7b02cb9..fa7e753 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,4 @@ # ignore nix-build result folder result - - -# Added by cargo - /target +.direnv From 34ba5a05bcfd13cf8b1776ff2a2a8068adb45d07 Mon Sep 17 00:00:00 2001 From: Artturin Date: Sun, 20 Nov 2022 18:49:19 +0200 Subject: [PATCH 4/6] add --print-package requested in https://github.com/nix-community/comma/issues/38 --- src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main.rs b/src/main.rs index 3a7456a..d3efd78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,6 +114,14 @@ fn main() -> ExitCode { } .contains("nixpkgs"); + if args.print_package { + println!( + "Package that contains executable /bin/{}: {}", + command, + &choice.rsplit('.').last().unwrap() + ); + }; + if args.install { Command::new("nix-env") .args(["-f", "", "-iA", choice.rsplit('.').last().unwrap()]) @@ -140,6 +148,10 @@ struct Opt { #[clap(short, long)] update: bool, + /// Print the package containing the executable + #[clap(long = "print-package")] + print_package: bool, + /// Command to run #[clap(required_unless_present = "update", name = "cmd")] cmd: Vec, From 8857f79a426dc04adb8dd730ab87f8ca79f54e93 Mon Sep 17 00:00:00 2001 From: Artturin Date: Mon, 21 Nov 2022 15:25:12 +0200 Subject: [PATCH 5/6] run cargo fmt --- src/index.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.rs b/src/index.rs index 70749e4..391db10 100644 --- a/src/index.rs +++ b/src/index.rs @@ -37,5 +37,6 @@ fn is_database_old(database_file: std::path::PathBuf) -> bool { .elapsed() .unwrap_or(Duration::new(0, 0)); - time_since_modified > Duration::from_secs(30 * 24 * 60 * 60) && !metadata.permissions().readonly() + time_since_modified > Duration::from_secs(30 * 24 * 60 * 60) + && !metadata.permissions().readonly() } From 4b81e6002aea480152e1d37c31886cba6a50d325 Mon Sep 17 00:00:00 2001 From: Artturin Date: Mon, 21 Nov 2022 15:26:00 +0200 Subject: [PATCH 6/6] release version 1.4.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c46674..8da911a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,7 +72,7 @@ dependencies = [ [[package]] name = "comma" -version = "1.2.3" +version = "1.4.0" dependencies = [ "clap", "xdg", diff --git a/Cargo.toml b/Cargo.toml index 224ce65..2666c17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "comma" description = "runs programs without installing them" -version = "1.3.1" +version = "1.4.0" edition = "2021" authors = ["Artturin "] license = "MIT"