command.rs
This file
Path
src/exec/command.rs
98 lines · 2.7 KiB · Rust
Last changed by
Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
History of this fileTake it
The walk stopped after 200 commits: lines older than e97a1e9 point at it.
Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9
lines 1–8why, one step back1use std::io;2use std::io::Write;3use std::process::Command;4use std::sync::Mutex;56use crate::error::print_error;7use crate::exit_codes::ExitCode;8Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 9–51why, one step back9struct Outputs {10 stdout: Vec<u8>,11 stderr: Vec<u8>,12}13struct OutputBuf<'a> {14 out_perm: &'a Mutex<()>,15 outputs: Vec<Outputs>,16}1718impl<'a> OutputBuf<'a> {19 fn new(out_perm: &'a Mutex<()>) -> Self {20 Self {21 out_perm,22 outputs: Vec::new(),23 }24 }2526 fn push(&mut self, stdout: Vec<u8>, stderr: Vec<u8>) {27 self.outputs.push(Outputs { stdout, stderr });28 }2930 fn write(self) {31 // avoid taking the lock if there is nothing to do32 if self.outputs.is_empty() {33 return;34 }35 // While this lock is active, this thread will be the only thread allowed36 // to write its outputs.37 let _lock = self.out_perm.lock().unwrap();3839 let stdout = io::stdout();40 let stderr = io::stderr();4142 let mut stdout = stdout.lock();43 let mut stderr = stderr.lock();4445 for output in self.outputs.iter() {46 let _ = stdout.write_all(&output.stdout);47 let _ = stderr.write_all(&output.stderr);48 }49 }50}51Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9
line 52why, one step back52/// Executes a command.Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 53–54why, one step back53pub fn execute_commands<I: Iterator<Item = Command>>(54 cmds: I,Address feedback for removing buffering when running on a single threadThayne McCombsbefore adoptione473060
lines 55–57why, one step back55 out_perm: &Mutex<()>,56 enable_output_buffering: bool,57) -> ExitCode {Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 58–67why, one step back58 let mut out_buf = OutputBuf::new(out_perm);59 for mut cmd in cmds {60 // Spawn the supplied command.61 let output = if enable_output_buffering {62 cmd.output()63 } else {64 // If running on only one thread, don't buffer output65 // Allows for viewing and interacting with intermediate command output66 cmd.spawn().and_then(|c| c.wait_with_output())67 };Address feedback for removing buffering when running on a single threadThayne McCombsbefore adoptione473060
line 68why, one step back68Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 69–78why, one step back69 // Then wait for the command to exit, if it was spawned.70 match output {71 Ok(output) => {72 if enable_output_buffering {73 out_buf.push(output.stdout, output.stderr);74 }75 if output.status.code() != Some(0) {76 out_buf.write();77 return ExitCode::GeneralError;78 }Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9
line 79why, one step back79 }Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 80–83why, one step back80 Err(why) => {81 out_buf.write();82 return handle_cmd_error(&cmd, why);83 }Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9
line 84why, one step back84 }Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 85–96why, one step back85 }86 out_buf.write();87 ExitCode::Success88}8990pub fn handle_cmd_error(cmd: &Command, err: io::Error) -> ExitCode {91 if err.kind() == io::ErrorKind::NotFound {92 print_error(format!("Command not found: {:?}", cmd));93 ExitCode::GeneralError94 } else {95 print_error(format!("Problem while executing command: {}", err));96 ExitCode::GeneralErrorNo buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
line 97why, one step back97 }Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9
line 98why, one step back98}