Add an option to print the path instead of running it (#75)

* feat: add an option to print exe path

* chore: fmt

* style: add missing ;
This commit is contained in:
diniamo 2024-04-18 02:02:31 +02:00 committed by GitHub
parent 1085a5af29
commit efe30c16fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -17,7 +17,7 @@ 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(())
return Err(());
}
Ok(())
}

View File

@ -142,6 +142,17 @@ fn main() -> ExitCode {
} else if args.shell {
let shell_cmd = shell::select_shell_from_pid(process::id()).unwrap_or("bash".into());
run_command_or_open_shell(use_channel, &choice, &shell_cmd, &[], &args.nixpkgs_flake);
} else if args.print_path {
run_command_or_open_shell(
use_channel,
&choice,
"sh",
&[
String::from("-c"),
format!("printf '%s\n' \"$(realpath \"$(which {command})\")\""),
],
&args.nixpkgs_flake,
);
} else {
run_command_or_open_shell(use_channel, &choice, command, trail, &args.nixpkgs_flake);
}
@ -180,6 +191,10 @@ struct Opt {
#[clap(short = 'p', long = "print-package")]
print_package: bool,
/// Print the absolute path to the executable in the nix store
#[clap(short = 'x', long = "print-path")]
print_path: bool,
/// Command to run
#[clap(required_unless_present = "update", name = "cmd")]
cmd: Vec<String>,