Skip to content
Myr

command.rs

This file

Path

src/exec/command.rs

41 lines · 1.2 KiB · Rust

Last changed by

Remove outdated license headers (not strictly required by Apache-2.0)sharkdpbefore adoption4274075

History of this file

Take it

1use std::io;2use std::io::Write;3use std::process::Command;4use std::sync::Mutex;56use crate::exit_codes::ExitCode;78/// Executes a command.9pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {10    // Spawn the supplied command.11    let output = cmd.output();1213    // Then wait for the command to exit, if it was spawned.14    match output {15        Ok(output) => {16            // While this lock is active, this thread will be the only thread allowed17            // to write its outputs.18            let _lock = out_perm.lock().unwrap();1920            let stdout = io::stdout();21            let stderr = io::stderr();2223            let _ = stdout.lock().write_all(&output.stdout);24            let _ = stderr.lock().write_all(&output.stderr);2526            if output.status.code() == Some(0) {27                ExitCode::Success28            } else {29                ExitCode::GeneralError30            }31        }32        Err(ref why) if why.kind() == io::ErrorKind::NotFound => {33            print_error!("Command not found: {:?}", cmd);34            ExitCode::GeneralError35        }36        Err(why) => {37            print_error!("Problem while executing command: {}", why);38            ExitCode::GeneralError39        }40    }41}