Skip to content

Commit ae61728

Browse files
authored
Merge pull request #82 from diniamo/sensible-print
feat: redo the print-package option
2 parents 10e4dcc + 2b47848 commit ae61728

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/main.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use cache::Cache;
1313
use clap::crate_version;
1414
use clap::Parser;
1515

16-
fn pick(picker: &str, derivations: &[&str]) -> Option<String> {
16+
fn pick(picker: &str, derivations: &[String]) -> Option<String> {
1717
let mut picker_process = Command::new(picker)
1818
.stdin(Stdio::piped())
1919
.stdout(Stdio::piped())
@@ -39,7 +39,7 @@ fn pick(picker: &str, derivations: &[&str]) -> Option<String> {
3939
)
4040
}
4141

42-
fn index_database(command: &str, picker: &str) -> Option<String> {
42+
fn index_database(command: &str) -> Option<Box<[String]>> {
4343
index::check_database_updated();
4444

4545
let nix_locate_output = Command::new("nix-locate")
@@ -63,11 +63,18 @@ fn index_database(command: &str, picker: &str) -> Option<String> {
6363
return None;
6464
}
6565

66-
let attrs = std::str::from_utf8(&attrs)
67-
.expect("fail")
68-
.trim()
69-
.split('\n')
70-
.collect::<Box<[&str]>>();
66+
Some(
67+
std::str::from_utf8(&attrs)
68+
.expect("fail")
69+
.trim()
70+
.split('\n')
71+
.map(|s| s.to_owned())
72+
.collect(),
73+
)
74+
}
75+
76+
fn index_database_pick(command: &str, picker: &str) -> Option<String> {
77+
let attrs = index_database(command)?;
7178

7279
if attrs.len() > 1 {
7380
pick(picker, &attrs)
@@ -144,14 +151,32 @@ fn main() -> ExitCode {
144151
}
145152
}
146153

154+
if args.print_packages {
155+
match index_database(command) {
156+
Some(derivations) => {
157+
println!(
158+
"Packages that contain /bin/{command}:\n{}",
159+
derivations
160+
.iter()
161+
.map(|a| format!("- {a}"))
162+
.collect::<Box<[String]>>()
163+
.join("\n")
164+
);
165+
166+
return ExitCode::SUCCESS;
167+
}
168+
None => return ExitCode::FAILURE,
169+
}
170+
}
171+
147172
let derivation = match cache {
148173
Ok(mut cache) => cache.query(command).or_else(|| {
149-
index_database(command, &args.picker).map(|derivation| {
174+
index_database_pick(command, &args.picker).map(|derivation| {
150175
cache.update(command, &derivation);
151176
derivation
152177
})
153178
}),
154-
Err(_) => index_database(command, &args.picker),
179+
Err(_) => index_database_pick(command, &args.picker),
155180
};
156181

157182
let derivation = match derivation {
@@ -167,13 +192,6 @@ fn main() -> ExitCode {
167192
}
168193
.contains("nixpkgs=");
169194

170-
if args.print_package {
171-
println!(
172-
"Package that contains executable /bin/{}: {}",
173-
command, basename
174-
);
175-
};
176-
177195
if args.install {
178196
Command::new("nix-env")
179197
.args(["-f", "<nixpkgs>", "-iA", basename])
@@ -239,8 +257,8 @@ struct Opt {
239257
update: bool,
240258

241259
/// Print the package containing the executable
242-
#[clap(short = 'p', long = "print-package")]
243-
print_package: bool,
260+
#[clap(short = 'p', long = "print-packages")]
261+
print_packages: bool,
244262

245263
/// Print the absolute path to the executable in the nix store
246264
#[clap(short = 'x', long = "print-path")]

0 commit comments

Comments
 (0)