Skip to content

Commit

Permalink
Use a join_handle and Result mapping to handle the Ok(...) for non-su…
Browse files Browse the repository at this point in the history
…ccess ExitStatus's
  • Loading branch information
jfreuden committed Feb 22, 2024
1 parent 8b890db commit a8c2b86
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/docker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Context, Ok, Result};
use anyhow::{anyhow, Context, Ok, Result};
use log::debug;
use std::{path::PathBuf, process::Stdio};
use tokio::{
Expand All @@ -23,20 +23,19 @@ async fn run_command(cmd: &mut Command) -> Result<()> {

// Ensure the child process is spawned in the runtime so it can
// make progress on its own while we await for any output.
tokio::spawn(async move {
let status = child
.wait()
.await
.expect("child process encountered an error");

println!("child status was: {}", status);
});
let join_handle = tokio::spawn(async move { child.wait().await });

while let Some(line) = reader.next_line().await? {
println!("Line: {}", line);
}

Ok(())
join_handle.await?.map_err(|err| anyhow!(err)).and_then(
|status| match status.success() {
true => Ok(()),
false => Err(anyhow!("child status was: {}", status))
}
)
}

pub fn ensure_availability() -> Result<()> {
Expand Down

0 comments on commit a8c2b86

Please sign in to comment.