Executes a child with bounded retained output while draining both pipes.
(command: &mut Command, timeout: Duration)
| 457 | |
| 458 | /// Executes a child with bounded retained output while draining both pipes. |
| 459 | pub async fn command(command: &mut Command, timeout: Duration) -> Result<String> { |
| 460 | command.stdout(Stdio::piped()).stderr(Stdio::piped()).kill_on_drop(true); |
| 461 | let mut child = command.spawn()?; |
| 462 | let stdout = child.stdout.take().ok_or_else(|| eyre::eyre!("stdout unavailable"))?; |
| 463 | let stderr = child.stderr.take().ok_or_else(|| eyre::eyre!("stderr unavailable"))?; |
| 464 | let (status, out, err) = tokio::time::timeout(timeout, async { |
| 465 | let (out, err, status) = |
| 466 | tokio::try_join!(Self::drain(stdout), Self::drain(stderr), child.wait())?; |
| 467 | Ok::<_, std::io::Error>((status, out, err)) |
| 468 | }) |
| 469 | .await |
| 470 | .wrap_err("command deadline elapsed")??; |
| 471 | if !status.success() { |
| 472 | bail!( |
| 473 | "command failed ({status}): {}", |
| 474 | String::from_utf8_lossy(&err).chars().take(4000).collect::<String>() |
| 475 | ); |
| 476 | } |
| 477 | Ok(String::from_utf8_lossy(&out).into_owned()) |
| 478 | } |
| 479 | |
| 480 | /// Retains a bounded prefix and drains the remainder to avoid deadlocking a child. |
| 481 | pub async fn drain(mut input: impl tokio::io::AsyncRead + Unpin) -> std::io::Result<Vec<u8>> { |