diff --git a/crates/oyster_runtime/src/pipeline.rs b/crates/oyster_runtime/src/pipeline.rs index f677b94..347315b 100644 --- a/crates/oyster_runtime/src/pipeline.rs +++ b/crates/oyster_runtime/src/pipeline.rs @@ -101,32 +101,33 @@ impl Shell { &mut self, pipeline: &ast::Pipeline, ) -> Result { - let mut cmds = pipeline.0.iter().map(PreparedCommand::from); let mut pgid = Pid::from_raw(0); + let status = (|| { + let mut cmds = pipeline.0.iter().map(PreparedCommand::from); - let mut last_cmd = cmds.next().expect("pipelines need to have >1 commands"); - for mut cmd in cmds { - let (output, input) = create_pipe()?; - cmd.stdin = Some(output); + let mut last_cmd = cmds.next().expect("pipelines need to have >1 commands"); + for mut cmd in cmds { + let (output, input) = create_pipe()?; + cmd.stdin = Some(output); - match last_cmd.redirect { - Redirect::None => (), - Redirect::Stdout => last_cmd.stdout = Some(input), + match last_cmd.redirect { + Redirect::None => (), + Redirect::Stdout => last_cmd.stdout = Some(input), + } + + last_cmd.spawn(&mut pgid)?; + + last_cmd = cmd; } last_cmd.spawn(&mut pgid)?; - last_cmd = cmd; - } + wait_pgid(pgid) + })(); - last_cmd.spawn(&mut pgid)?; - - // TODO: kill children if error occured - - let status = wait_pgid(pgid)?; let _ = unistd::tcsetpgrp(libc::STDIN_FILENO, unistd::getpgid(None).unwrap()); - Ok(status) + status } }