command.rs
This file
Path
src/exec/command.rs
110 lines · 3.1 KiB · Rust
Last changed by
Use to_string_lossy to avoid relying on Debug-outputsMarie Katrine Ekebergbefore adoptiond6b1d16
History of this fileTake it
The walk stopped after 200 commits: lines older than 0f0fa76 point at it.
Just trying some things out, initial functionalityMax Trianobefore adoption0f0fa76
lines 1–4why, one step back1use std::io;2use std::io::Write;3use std::sync::Mutex;4exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e
lines 5–6why, one step back5use argmax::Command;6Just trying some things out, initial functionalityMax Trianobefore adoption0f0fa76
lines 7–9why, one step back7use crate::error::print_error;8use crate::exit_codes::ExitCode;9Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 10–13why, one step back10struct Outputs {11 stdout: Vec<u8>,12 stderr: Vec<u8>,13}Use full names in command.rsThayne McCombsbefore adoption5a12a5e
lines 14–15why, one step back14struct OutputBuffer<'a> {15 output_permission: &'a Mutex<()>,Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 16–18why, one step back16 outputs: Vec<Outputs>,17}18Use full names in command.rsThayne McCombsbefore adoption5a12a5e
lines 19–20why, one step back19impl<'a> OutputBuffer<'a> {20 fn new(output_permission: &'a Mutex<()>) -> Self {Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 21why, one step back21 Self {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 22why, one step back22 output_permission,Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 23–37why, one step back23 outputs: Vec::new(),24 }25 }2627 fn push(&mut self, stdout: Vec<u8>, stderr: Vec<u8>) {28 self.outputs.push(Outputs { stdout, stderr });29 }3031 fn write(self) {32 // avoid taking the lock if there is nothing to do33 if self.outputs.is_empty() {34 return;35 }36 // While this lock is active, this thread will be the only thread allowed37 // to write its outputs.Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 38why, one step back38 let _lock = self.output_permission.lock().unwrap();Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 39–52why, one step back3940 let stdout = io::stdout();41 let stderr = io::stderr();4243 let mut stdout = stdout.lock();44 let mut stderr = stderr.lock();4546 for output in self.outputs.iter() {47 let _ = stdout.write_all(&output.stdout);48 let _ = stderr.write_all(&output.stderr);49 }50 }51}52Just trying some things out, initial functionalityMax Trianobefore adoption0f0fa76
line 53why, one step back53/// Executes a command.exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e
line 54why, one step back54pub fn execute_commands<I: Iterator<Item = io::Result<Command>>>(Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 55why, one step back55 cmds: I,Address feedback for removing buffering when running on a single threadThayne McCombsbefore adoptione473060
lines 56–58why, one step back56 out_perm: &Mutex<()>,57 enable_output_buffering: bool,58) -> ExitCode {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 59why, one step back59 let mut output_buffer = OutputBuffer::new(out_perm);exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e
lines 60–65why, one step back60 for result in cmds {61 let mut cmd = match result {62 Ok(cmd) => cmd,63 Err(e) => return handle_cmd_error(None, e),64 };65Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 66–73why, one step back66 // Spawn the supplied command.67 let output = if enable_output_buffering {68 cmd.output()69 } else {70 // If running on only one thread, don't buffer output71 // Allows for viewing and interacting with intermediate command output72 cmd.spawn().and_then(|c| c.wait_with_output())73 };Address feedback for removing buffering when running on a single threadThayne McCombsbefore adoptione473060
line 74why, one step back74Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 75–78why, one step back75 // Then wait for the command to exit, if it was spawned.76 match output {77 Ok(output) => {78 if enable_output_buffering {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 79why, one step back79 output_buffer.push(output.stdout, output.stderr);Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 80–81why, one step back80 }81 if output.status.code() != Some(0) {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 82why, one step back82 output_buffer.write();Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 83–84why, one step back83 return ExitCode::GeneralError;84 }Just trying some things out, initial functionalityMax Trianobefore adoption0f0fa76
line 85why, one step back85 }Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 86why, one step back86 Err(why) => {Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 87why, one step back87 output_buffer.write();exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e
line 88why, one step back88 return handle_cmd_error(Some(&cmd), why);Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 89why, one step back89 }Just trying some things out, initial functionalityMax Trianobefore adoption0f0fa76
line 90why, one step back90 }Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
line 91why, one step back91 }Use full names in command.rsThayne McCombsbefore adoption5a12a5e
line 92why, one step back92 output_buffer.write();Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d
lines 93–95why, one step back93 ExitCode::Success94}95exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e
lines 96–98why, one step back96pub fn handle_cmd_error(cmd: Option<&Command>, err: io::Error) -> ExitCode {97 match (cmd, err) {98 (Some(cmd), err) if err.kind() == io::ErrorKind::NotFound => {Use to_string_lossy to avoid relying on Debug-outputsMarie Katrine Ekebergbefore adoptiond6b1d16
lines 99–102why, one step back99 print_error(format!(100 "Command not found: {}",101 cmd.get_program().to_string_lossy()102 ));exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e
lines 103–108why, one step back103 ExitCode::GeneralError104 }105 (_, err) => {106 print_error(format!("Problem while executing command: {}", err));107 ExitCode::GeneralError108 }No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
line 109why, one step back109 }Just trying some things out, initial functionalityMax Trianobefore adoption0f0fa76
line 110why, one step back110}