command.rs
This file
Path
src/exec/command.rs
98 lines · 2.7 KiB · Rust
Last changed by
Use full names in command.rsThayne McCombsbefore adoption5a12a5e
History of this fileTake it
The walk stopped after 200 commits: lines older than 9bc20e3 point at it.
fixed: typographic error in helpChathika Weerasuriyabefore adoption9bc20e3
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–12why, one step back9struct Outputs {10 stdout: Vec<u8>,11 stderr: Vec<u8>,12}Use full names in command.rsThayne McCombsbefore adoption5a12a5e
lines 13–14why, one step back13struct OutputBuffer<'a> {14 output_permission: &'a Mutex<()>,Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 15–17why, one step back15 outputs: Vec<Outputs>,16}17Use full names in command.rsThayne McCombsbefore adoption5a12a5e
lines 18–19why, one step back18impl<'a> OutputBuffer<'a> {19 fn new(output_permission: &'a Mutex<()>) -> Self {Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 20why, one step back20 Self {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 21why, one step back21 output_permission,Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 22–36why, one step back22 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.Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 37why, one step back37 let _lock = self.output_permission.lock().unwrap();Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 38–51why, one step back3839 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}51fixed: typographic error in helpChathika Weerasuriyabefore adoption9bc20e3
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 {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 58why, one step back58 let mut output_buffer = OutputBuffer::new(out_perm);Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 59–67why, one step back59 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–72why, 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 {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 73why, one step back73 output_buffer.push(output.stdout, output.stderr);Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 74–75why, one step back74 }75 if output.status.code() != Some(0) {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 76why, one step back76 output_buffer.write();Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 77–78why, one step back77 return ExitCode::GeneralError;78 }fixed: typographic error in helpChathika Weerasuriyabefore adoption9bc20e3
line 79why, one step back79 }Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 80why, one step back80 Err(why) => {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 81why, one step back81 output_buffer.write();Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 82–83why, one step back82 return handle_cmd_error(&cmd, why);83 }fixed: typographic error in helpChathika Weerasuriyabefore adoption9bc20e3
line 84why, one step back84 }Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 85why, one step back85 }Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 86why, one step back86 output_buffer.write();Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 87–96why, one step back87 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 }fixed: typographic error in helpChathika Weerasuriyabefore adoption9bc20e3
line 98why, one step back98}