Run a command with inherited stdio and return the result
(args: &[String])
| 29 | |
| 30 | /// Run a command with inherited stdio and return the result |
| 31 | pub fn run_command(args: &[String]) -> std::io::Result<CommandResult> { |
| 32 | if args.is_empty() { |
| 33 | return Err(std::io::Error::new( |
| 34 | std::io::ErrorKind::InvalidInput, |
| 35 | "No command provided", |
| 36 | )); |
| 37 | } |
| 38 | |
| 39 | let start = Instant::now(); |
| 40 | |
| 41 | // First arg is the program, rest are arguments |
| 42 | let mut cmd = Command::new(&args[0]); |
| 43 | if args.len() > 1 { |
| 44 | cmd.args(&args[1..]); |
| 45 | } |
| 46 | |
| 47 | // Inherit stdio so the command behaves normally |
| 48 | cmd.stdin(Stdio::inherit()) |
| 49 | .stdout(Stdio::inherit()) |
| 50 | .stderr(Stdio::inherit()); |
| 51 | |
| 52 | // Run and wait |
| 53 | let status: ExitStatus = cmd.spawn()?.wait()?; |
| 54 | |
| 55 | let duration = start.elapsed(); |
| 56 | |
| 57 | Ok(CommandResult { |
| 58 | exit_code: status.code(), |
| 59 | duration, |
| 60 | success: status.success(), |
| 61 | }) |
| 62 | } |