Skip to content

Commit

Permalink
fix: handle stderr [#4]
Browse files Browse the repository at this point in the history
  • Loading branch information
soanvig committed Oct 19, 2024
1 parent 9b11fd5 commit dd7ef7c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,26 @@ fn spawn_command(command_with_args: Vec<String>, command_number: usize) {
let mut child = Command::new(command_with_args.get(0).expect("Command should be defined"))
.args(&command_with_args[1..])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to start process");

{
let stdout = child.stdout.as_mut().unwrap();
let stdout_reader = BufReader::new(stdout);
let stdout_lines = stdout_reader.lines();
let stdout = BufReader::new(child.stdout.take().expect("Cannot reference stdout"));
let stdout_handle = thread::spawn(move || {
for line in stdout.lines() {
println!("[{}]: {}", command_number, line.expect("stdout to be line"));
}
});

for line in stdout_lines {
let stderr = BufReader::new(child.stderr.take().expect("Cannot reference stderr"));
let stderr_handle = thread::spawn(move || {
for line in stderr.lines() {
println!("[{}]: {}", command_number, line.expect("stdout to be line"));
}
}
});

stdout_handle.join().unwrap();
stderr_handle.join().unwrap();

let status_code = child.wait().unwrap();

Expand Down

0 comments on commit dd7ef7c

Please sign in to comment.