@@ -4,10 +4,13 @@ use std::process::{Command, ExitStatus, Output};
4
4
5
5
use crate :: errors:: * ;
6
6
7
+ pub const STRIPPED_BINS : & [ & str ] = & [ crate :: docker:: DOCKER , crate :: docker:: PODMAN , "cargo" ] ;
8
+
7
9
pub trait CommandExt {
8
10
fn print_verbose ( & self , verbose : bool ) ;
9
11
fn status_result (
10
12
& self ,
13
+ verbose : bool ,
11
14
status : ExitStatus ,
12
15
output : Option < & Output > ,
13
16
) -> Result < ( ) , CommandError > ;
@@ -19,44 +22,57 @@ pub trait CommandExt {
19
22
) -> Result < ExitStatus , CommandError > ;
20
23
fn run_and_get_stdout ( & mut self , verbose : bool ) -> Result < String > ;
21
24
fn run_and_get_output ( & mut self , verbose : bool ) -> Result < std:: process:: Output > ;
22
- fn command_pretty ( & self ) -> String ;
25
+ fn command_pretty ( & self , verbose : bool , strip : impl for < ' a > Fn ( & ' a str ) -> bool ) -> String ;
23
26
}
24
27
25
28
impl CommandExt for Command {
26
- fn command_pretty ( & self ) -> String {
29
+ fn command_pretty ( & self , verbose : bool , strip : impl for < ' a > Fn ( & ' a str ) -> bool ) -> String {
27
30
// a dummy implementor of display to avoid using unwraps
28
- struct C < ' c > ( & ' c Command ) ;
29
- impl std:: fmt:: Display for C < ' _ > {
31
+ struct C < ' c , F > ( & ' c Command , bool , F ) ;
32
+ impl < F > std:: fmt:: Display for C < ' _ , F >
33
+ where
34
+ F : for < ' a > Fn ( & ' a str ) -> bool ,
35
+ {
30
36
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
31
37
let cmd = self . 0 ;
32
- write ! ( f, "{}" , cmd. get_program( ) . to_string_lossy( ) ) ?;
38
+ write ! (
39
+ f,
40
+ "{}" ,
41
+ // if verbose, never strip, if not, let user choose
42
+ crate :: file:: pretty_path( cmd. get_program( ) , move |c| if self . 1 {
43
+ false
44
+ } else {
45
+ self . 2 ( c)
46
+ } )
47
+ ) ?;
33
48
let args = cmd. get_args ( ) ;
34
49
if args. len ( ) > 1 {
35
- write ! ( f, " " ) ?;
36
50
write ! (
37
51
f,
38
- "{}" ,
52
+ " {}" ,
39
53
shell_words:: join( args. map( |o| o. to_string_lossy( ) ) )
40
54
) ?;
41
55
}
42
56
Ok ( ( ) )
43
57
}
44
58
}
45
- format ! ( "{}" , C ( self ) )
59
+ format ! ( "{}" , C ( self , verbose , strip ) )
46
60
}
47
61
48
62
fn print_verbose ( & self , verbose : bool ) {
63
+ // TODO: introduce verbosity levels, v = 1, strip cmd, v > 1, don't strip cmd
49
64
if verbose {
50
65
if let Some ( cwd) = self . get_current_dir ( ) {
51
- println ! ( "+ {:?} {}" , cwd, self . command_pretty( ) ) ;
66
+ println ! ( "+ {:?} {}" , cwd, self . command_pretty( true , |_| false ) ) ;
52
67
} else {
53
- println ! ( "+ {}" , self . command_pretty( ) ) ;
68
+ println ! ( "+ {}" , self . command_pretty( true , |_| false ) ) ;
54
69
}
55
70
}
56
71
}
57
72
58
73
fn status_result (
59
74
& self ,
75
+ verbose : bool ,
60
76
status : ExitStatus ,
61
77
output : Option < & Output > ,
62
78
) -> Result < ( ) , CommandError > {
@@ -65,7 +81,8 @@ impl CommandExt for Command {
65
81
} else {
66
82
Err ( CommandError :: NonZeroExitCode {
67
83
status,
68
- command : self . command_pretty ( ) ,
84
+ command : self
85
+ . command_pretty ( verbose, |ref cmd| STRIPPED_BINS . iter ( ) . any ( |f| f == cmd) ) ,
69
86
stderr : output. map ( |out| out. stderr . clone ( ) ) . unwrap_or_default ( ) ,
70
87
stdout : output. map ( |out| out. stdout . clone ( ) ) . unwrap_or_default ( ) ,
71
88
} )
@@ -75,7 +92,7 @@ impl CommandExt for Command {
75
92
/// Runs the command to completion
76
93
fn run ( & mut self , verbose : bool , silence_stdout : bool ) -> Result < ( ) , CommandError > {
77
94
let status = self . run_and_get_status ( verbose, silence_stdout) ?;
78
- self . status_result ( status, None )
95
+ self . status_result ( verbose , status, None )
79
96
}
80
97
81
98
/// Runs the command to completion
@@ -91,15 +108,16 @@ impl CommandExt for Command {
91
108
self . status ( )
92
109
. map_err ( |e| CommandError :: CouldNotExecute {
93
110
source : Box :: new ( e) ,
94
- command : self . command_pretty ( ) ,
111
+ command : self
112
+ . command_pretty ( verbose, |ref cmd| STRIPPED_BINS . iter ( ) . any ( |f| f == cmd) ) ,
95
113
} )
96
114
. map_err ( Into :: into)
97
115
}
98
116
99
117
/// Runs the command to completion and returns its stdout
100
118
fn run_and_get_stdout ( & mut self , verbose : bool ) -> Result < String > {
101
119
let out = self . run_and_get_output ( verbose) ?;
102
- self . status_result ( out. status , Some ( & out) )
120
+ self . status_result ( verbose , out. status , Some ( & out) )
103
121
. map_err ( CommandError :: to_section_report) ?;
104
122
out. stdout ( ) . map_err ( Into :: into)
105
123
}
@@ -114,7 +132,8 @@ impl CommandExt for Command {
114
132
self . output ( ) . map_err ( |e| {
115
133
CommandError :: CouldNotExecute {
116
134
source : Box :: new ( e) ,
117
- command : self . command_pretty ( ) ,
135
+ command : self
136
+ . command_pretty ( verbose, |ref cmd| STRIPPED_BINS . iter ( ) . any ( |f| f == cmd) ) ,
118
137
}
119
138
. to_section_report ( )
120
139
} )
0 commit comments