Skip to content
Myr

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 file

Take it

1use std::io;2use std::io::Write;3use std::process::Command;4use std::sync::Mutex;56use crate::error::print_error;7use crate::exit_codes::ExitCode;89struct Outputs {10    stdout: Vec<u8>,11    stderr: Vec<u8>,12}13struct OutputBuffer<'a> {14    output_permission: &'a Mutex<()>,15    outputs: Vec<Outputs>,16}1718impl<'a> OutputBuffer<'a> {19    fn new(output_permission: &'a Mutex<()>) -> Self {20        Self {21            output_permission,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.output_permission.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}5152/// Executes a command.53pub fn execute_commands<I: Iterator<Item = Command>>(54    cmds: I,55    out_perm: &Mutex<()>,56    enable_output_buffering: bool,57) -> ExitCode {58    let mut output_buffer = OutputBuffer::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        };6869        // Then wait for the command to exit, if it was spawned.70        match output {71            Ok(output) => {72                if enable_output_buffering {73                    output_buffer.push(output.stdout, output.stderr);74                }75                if output.status.code() != Some(0) {76                    output_buffer.write();77                    return ExitCode::GeneralError;78                }79            }80            Err(why) => {81                output_buffer.write();82                return handle_cmd_error(&cmd, why);83            }84        }85    }86    output_buffer.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::GeneralError97    }98}