command.rs
This file
Path
src/exec/command.rs
42 lines · 1.3 KiB · Rust
Last changed by
Proper error handling within walk.rssharkdpbefore adoption4590ae8
History of this fileTake it
The walk stopped after 200 commits: lines older than 45d1b15 point at it.
Add support for batch execution of commandkimsnjbefore adoption45d1b15
lines 1–5why, one step back1use std::io;2use std::io::Write;3use std::process::Command;4use std::sync::Mutex;5Proper error handling within walk.rssharkdpbefore adoption4590ae8
line 6why, one step back6use crate::error::print_error;Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd
lines 7–8why, one step back7use crate::exit_codes::ExitCode;8Add support for batch execution of commandkimsnjbefore adoption45d1b15
line 9why, one step back9/// Executes a command.Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd
line 10why, one step back10pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {Add support for batch execution of commandkimsnjbefore adoption45d1b15
lines 11–25why, one step back11 // Spawn the supplied command.12 let output = cmd.output();1314 // Then wait for the command to exit, if it was spawned.15 match output {16 Ok(output) => {17 // While this lock is active, this thread will be the only thread allowed18 // to write its outputs.19 let _lock = out_perm.lock().unwrap();2021 let stdout = io::stdout();22 let stderr = io::stderr();2324 let _ = stdout.lock().write_all(&output.stdout);25 let _ = stderr.lock().write_all(&output.stderr);Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd
lines 26–31why, one step back2627 if output.status.code() == Some(0) {28 ExitCode::Success29 } else {30 ExitCode::GeneralError31 }Add support for batch execution of commandkimsnjbefore adoption45d1b15
lines 32–33why, one step back32 }33 Err(ref why) if why.kind() == io::ErrorKind::NotFound => {Proper error handling within walk.rssharkdpbefore adoption4590ae8
line 34why, one step back34 print_error(format!("Command not found: {:?}", cmd));Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd
line 35why, one step back35 ExitCode::GeneralErrorAdd support for batch execution of commandkimsnjbefore adoption45d1b15
lines 36–37why, one step back36 }37 Err(why) => {Proper error handling within walk.rssharkdpbefore adoption4590ae8
line 38why, one step back38 print_error(format!("Problem while executing command: {}", why));Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd
line 39why, one step back39 ExitCode::GeneralErrorAdd support for batch execution of commandkimsnjbefore adoption45d1b15
lines 40–42why, one step back40 }41 }42}