@@ -6,9 +6,23 @@ pub fn run_command<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<Str
6
6
run_command_at ( args, & std:: env:: current_dir ( ) ?)
7
7
}
8
8
9
+ /// Run command while streaming stdout and stderr to the terminal.
10
+ pub fn stream_command < ' a , Args : AsRef < [ & ' a str ] > > ( args : Args ) -> anyhow:: Result < ( ) > {
11
+ run_command_inner ( args, & std:: env:: current_dir ( ) ?, false ) ?;
12
+ Ok ( ( ) )
13
+ }
14
+
9
15
pub fn run_command_at < ' a , Args : AsRef < [ & ' a str ] > > (
10
16
args : Args ,
11
17
workdir : & Path ,
18
+ ) -> anyhow:: Result < String > {
19
+ run_command_inner ( args, workdir, true )
20
+ }
21
+
22
+ fn run_command_inner < ' a , Args : AsRef < [ & ' a str ] > > (
23
+ args : Args ,
24
+ workdir : & Path ,
25
+ capture : bool ,
12
26
) -> anyhow:: Result < String > {
13
27
let args = args. as_ref ( ) ;
14
28
@@ -17,16 +31,32 @@ pub fn run_command_at<'a, Args: AsRef<[&'a str]>>(
17
31
cmd. args ( & args[ 1 ..] ) ;
18
32
19
33
eprintln ! ( "+ {cmd:?}" ) ;
20
- let out = cmd. output ( ) . expect ( "command failed" ) ;
21
- let stdout = String :: from_utf8_lossy ( out. stdout . trim_ascii ( ) ) . to_string ( ) ;
22
- let stderr = String :: from_utf8_lossy ( out. stderr . trim_ascii ( ) ) . to_string ( ) ;
23
- if !out. status . success ( ) {
24
- Err ( anyhow:: anyhow!(
25
- "Command `{cmd:?}` failed with exit code {:?}. STDOUT:\n {stdout}\n STDERR:\n {stderr}" ,
26
- out. status. code( )
27
- ) )
34
+ if capture {
35
+ let out = cmd. output ( ) . expect ( "command failed" ) ;
36
+ let stdout = String :: from_utf8_lossy ( out. stdout . trim_ascii ( ) ) . to_string ( ) ;
37
+ let stderr = String :: from_utf8_lossy ( out. stderr . trim_ascii ( ) ) . to_string ( ) ;
38
+ if !out. status . success ( ) {
39
+ Err ( anyhow:: anyhow!(
40
+ "Command `{cmd:?}` failed with exit code {:?}. STDOUT:\n {stdout}\n STDERR:\n {stderr}" ,
41
+ out. status. code( )
42
+ ) )
43
+ } else {
44
+ Ok ( stdout)
45
+ }
28
46
} else {
29
- Ok ( stdout)
47
+ let status = cmd
48
+ . spawn ( )
49
+ . expect ( "cannot spawn command" )
50
+ . wait ( )
51
+ . expect ( "command failed" ) ;
52
+ if !status. success ( ) {
53
+ Err ( anyhow:: anyhow!(
54
+ "Command `{cmd:?}` failed with exit code {:?}" ,
55
+ status. code( )
56
+ ) )
57
+ } else {
58
+ Ok ( String :: new ( ) )
59
+ }
30
60
}
31
61
}
32
62
0 commit comments