@@ -3,53 +3,50 @@ use std::process::{Command, ExitStatus};
3
3
use crate :: errors:: * ;
4
4
5
5
pub trait CommandExt {
6
+ fn print_verbose ( & self , verbose : bool ) ;
7
+ fn status_result ( & self , status : ExitStatus ) -> Result < ( ) > ;
6
8
fn run ( & mut self , verbose : bool ) -> Result < ( ) > ;
7
9
fn run_and_get_status ( & mut self , verbose : bool ) -> Result < ExitStatus > ;
8
10
fn run_and_get_stdout ( & mut self , verbose : bool ) -> Result < String > ;
9
11
}
10
12
11
13
impl CommandExt for Command {
12
- /// Runs the command to completion
13
- fn run ( & mut self , verbose : bool ) -> Result < ( ) > {
14
- let status = self . run_and_get_status ( verbose) ?;
14
+ fn print_verbose ( & self , verbose : bool ) {
15
+ if verbose {
16
+ println ! ( "+ {:?}" , self ) ;
17
+ }
18
+ }
15
19
20
+ fn status_result ( & self , status : ExitStatus ) -> Result < ( ) > {
16
21
if status. success ( ) {
17
22
Ok ( ( ) )
18
23
} else {
19
- Err ( format ! ( "`{:?}` failed with exit code: {:?}" ,
20
- self ,
21
- status. code( ) ) ) ?
24
+ Err ( format ! ( "`{:?}` failed with exit code: {:?}" , self , status. code( ) ) . into ( ) )
22
25
}
23
26
}
24
27
25
28
/// Runs the command to completion
26
- fn run_and_get_status ( & mut self , verbose : bool ) -> Result < ExitStatus > {
27
- if verbose {
28
- println ! ( "+ {:?}" , self ) ;
29
- }
29
+ fn run ( & mut self , verbose : bool ) -> Result < ( ) > {
30
+ let status = self . run_and_get_status ( verbose ) ? ;
31
+ self . status_result ( status )
32
+ }
30
33
34
+ /// Runs the command to completion
35
+ fn run_and_get_status ( & mut self , verbose : bool ) -> Result < ExitStatus > {
36
+ self . print_verbose ( verbose) ;
31
37
self . status ( )
32
38
. chain_err ( || format ! ( "couldn't execute `{:?}`" , self ) )
33
39
}
34
40
35
41
/// Runs the command to completion and returns its stdout
36
42
fn run_and_get_stdout ( & mut self , verbose : bool ) -> Result < String > {
37
- if verbose {
38
- println ! ( "+ {:?}" , self ) ;
39
- }
40
-
43
+ self . print_verbose ( verbose) ;
41
44
let out = self . output ( )
42
45
. chain_err ( || format ! ( "couldn't execute `{:?}`" , self ) ) ?;
43
46
44
- if out. status . success ( ) {
45
- Ok ( String :: from_utf8 ( out. stdout ) . chain_err ( || {
46
- format ! ( "`{:?}` output was not UTF-8" ,
47
- self )
48
- } ) ?)
49
- } else {
50
- Err ( format ! ( "`{:?}` failed with exit code: {:?}" ,
51
- self ,
52
- out. status. code( ) ) ) ?
53
- }
47
+ self . status_result ( out. status ) ?;
48
+
49
+ Ok ( String :: from_utf8 ( out. stdout )
50
+ . chain_err ( || format ! ( "`{:?}` output was not UTF-8" , self ) ) ?)
54
51
}
55
52
}
0 commit comments