@@ -6,9 +6,23 @@ pub fn run_command<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<Str
66 run_command_at ( args, & std:: env:: current_dir ( ) ?)
77}
88
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+
915pub fn run_command_at < ' a , Args : AsRef < [ & ' a str ] > > (
1016 args : Args ,
1117 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 ,
1226) -> anyhow:: Result < String > {
1327 let args = args. as_ref ( ) ;
1428
@@ -17,16 +31,32 @@ pub fn run_command_at<'a, Args: AsRef<[&'a str]>>(
1731 cmd. args ( & args[ 1 ..] ) ;
1832
1933 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+ }
2846 } 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+ }
3060 }
3161}
3262
0 commit comments