1
1
use crate :: { CmdResult , FunResult } ;
2
2
use lazy_static:: lazy_static;
3
3
use std:: collections:: HashMap ;
4
+ use std:: fmt;
4
5
use std:: fs:: OpenOptions ;
5
6
use std:: io:: { Error , ErrorKind , Write } ;
6
7
use std:: process:: { Child , Command , ExitStatus , Stdio } ;
@@ -196,6 +197,19 @@ impl Cmds {
196
197
ret += " | " ;
197
198
}
198
199
ret += & format ! ( "{:?}" , cmd_arg. get_args( ) ) ;
200
+ let mut extra = String :: new ( ) ;
201
+ if !cmd_arg. get_envs ( ) . is_empty ( ) {
202
+ extra += & format ! ( "envs: {:?}" , cmd_arg. get_envs( ) ) ;
203
+ }
204
+ if !cmd_arg. get_redirects ( ) . is_empty ( ) {
205
+ if !extra. is_empty ( ) {
206
+ extra += ", " ;
207
+ }
208
+ extra += & format ! ( "redirects: {:?}" , cmd_arg. get_redirects( ) ) ;
209
+ }
210
+ if !extra. is_empty ( ) {
211
+ ret += & format ! ( " ({})" , extra) ;
212
+ }
199
213
}
200
214
ret
201
215
}
@@ -304,13 +318,13 @@ impl Cmds {
304
318
Ok ( ( ) )
305
319
}
306
320
307
- pub fn run_cmd ( & mut self , current_dir : & mut String ) -> CmdResult {
321
+ fn run_cmd ( & mut self , current_dir : & mut String ) -> CmdResult {
308
322
let mut handle = self . spawn ( current_dir) ?;
309
323
self . pipes . clear ( ) ; // to avoid wait deadlock
310
324
handle. wait_result ( )
311
325
}
312
326
313
- pub fn run_fun ( & mut self , current_dir : & mut String ) -> FunResult {
327
+ fn run_fun ( & mut self , current_dir : & mut String ) -> FunResult {
314
328
let mut handle = self . spawn_with_output ( current_dir) ?;
315
329
self . pipes . clear ( ) ; // to avoid wait deadlock
316
330
handle. wait_result ( )
@@ -339,6 +353,29 @@ pub enum Redirect {
339
353
StdoutToFile ( String , bool ) ,
340
354
StderrToFile ( String , bool ) ,
341
355
}
356
+ impl fmt:: Debug for Redirect {
357
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
358
+ match self {
359
+ Redirect :: FileToStdin ( path) => f. write_str ( & format ! ( "< {}" , path) ) ,
360
+ Redirect :: StdoutToStderr => f. write_str ( ">&2" ) ,
361
+ Redirect :: StderrToStdout => f. write_str ( "2>&1" ) ,
362
+ Redirect :: StdoutToFile ( path, append) => {
363
+ if * append {
364
+ f. write_str ( & format ! ( "1>> {}" , path) )
365
+ } else {
366
+ f. write_str ( & format ! ( "1> {}" , path) )
367
+ }
368
+ }
369
+ Redirect :: StderrToFile ( path, append) => {
370
+ if * append {
371
+ f. write_str ( & format ! ( "2>> {}" , path) )
372
+ } else {
373
+ f. write_str ( & format ! ( "2> {}" , path) )
374
+ }
375
+ }
376
+ }
377
+ }
378
+ }
342
379
343
380
#[ doc( hidden) ]
344
381
#[ derive( Default ) ]
@@ -368,11 +405,11 @@ impl Cmd {
368
405
self
369
406
}
370
407
371
- pub fn get_args ( & self ) -> & Vec < String > {
408
+ fn get_args ( & self ) -> & Vec < String > {
372
409
& self . args
373
410
}
374
411
375
- pub fn get_envs ( & self ) -> & HashMap < String , String > {
412
+ fn get_envs ( & self ) -> & HashMap < String , String > {
376
413
& self . envs
377
414
}
378
415
@@ -381,7 +418,11 @@ impl Cmd {
381
418
self
382
419
}
383
420
384
- pub fn gen_command ( & mut self ) -> Command {
421
+ fn get_redirects ( & self ) -> & Vec < Redirect > {
422
+ & self . redirects
423
+ }
424
+
425
+ fn gen_command ( & mut self ) -> Command {
385
426
let cmd_args: Vec < String > = self . get_args ( ) . to_vec ( ) ;
386
427
let mut cmd = Command :: new ( & cmd_args[ 0 ] ) ;
387
428
cmd. args ( & cmd_args[ 1 ..] ) ;
0 commit comments