@@ -13,7 +13,7 @@ use cache::Cache;
13
13
use clap:: crate_version;
14
14
use clap:: Parser ;
15
15
16
- fn pick ( picker : & str , derivations : & [ & str ] ) -> Option < String > {
16
+ fn pick ( picker : & str , derivations : & [ String ] ) -> Option < String > {
17
17
let mut picker_process = Command :: new ( picker)
18
18
. stdin ( Stdio :: piped ( ) )
19
19
. stdout ( Stdio :: piped ( ) )
@@ -39,7 +39,7 @@ fn pick(picker: &str, derivations: &[&str]) -> Option<String> {
39
39
)
40
40
}
41
41
42
- fn index_database ( command : & str , picker : & str ) -> Option < String > {
42
+ fn index_database ( command : & str ) -> Option < Box < [ String ] > > {
43
43
index:: check_database_updated ( ) ;
44
44
45
45
let nix_locate_output = Command :: new ( "nix-locate" )
@@ -63,11 +63,18 @@ fn index_database(command: &str, picker: &str) -> Option<String> {
63
63
return None ;
64
64
}
65
65
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) ?;
71
78
72
79
if attrs. len ( ) > 1 {
73
80
pick ( picker, & attrs)
@@ -144,14 +151,32 @@ fn main() -> ExitCode {
144
151
}
145
152
}
146
153
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
+
147
172
let derivation = match cache {
148
173
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| {
150
175
cache. update ( command, & derivation) ;
151
176
derivation
152
177
} )
153
178
} ) ,
154
- Err ( _) => index_database ( command, & args. picker ) ,
179
+ Err ( _) => index_database_pick ( command, & args. picker ) ,
155
180
} ;
156
181
157
182
let derivation = match derivation {
@@ -167,13 +192,6 @@ fn main() -> ExitCode {
167
192
}
168
193
. contains ( "nixpkgs=" ) ;
169
194
170
- if args. print_package {
171
- println ! (
172
- "Package that contains executable /bin/{}: {}" ,
173
- command, basename
174
- ) ;
175
- } ;
176
-
177
195
if args. install {
178
196
Command :: new ( "nix-env" )
179
197
. args ( [ "-f" , "<nixpkgs>" , "-iA" , basename] )
@@ -239,8 +257,8 @@ struct Opt {
239
257
update : bool ,
240
258
241
259
/// 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 ,
244
262
245
263
/// Print the absolute path to the executable in the nix store
246
264
#[ clap( short = 'x' , long = "print-path" ) ]
0 commit comments