command.rs
This file
Path
src/exec/command.rs
50 lines · 1.6 KiB · Rust
Last changed by
Clarifying commentsMax Trianobefore adoption154cd6e
History of this fileTake it
The walk stopped after 200 commits: lines older than a85f33c point at it.
Clean up build.rssharkdpbefore adoptiona85f33c
lines 1–9why, 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;89/// Executes a command.No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
lines 10–11why, one step back10pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>, is_multithread: bool) -> ExitCode {11 let output;Clean up build.rssharkdpbefore adoptiona85f33c
line 12why, one step back12 // Spawn the supplied command.No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
lines 13–15why, one step back13 if is_multithread {14 output = cmd.output();15 } else {Clarifying commentsMax Trianobefore adoption154cd6e
lines 16–17why, one step back16 // If running on only one thread, don't buffer output17 // Allows for viewing and interacting with intermediate command outputNo buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
lines 18–21why, one step back18 let child = cmd.spawn().expect("Failed to execute command");19 output = child.wait_with_output();20 }21 Clean up build.rssharkdpbefore adoptiona85f33c
line 22why, one step back22 // Then wait for the command to exit, if it was spawned.No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
line 23why, one step back23 match output {Clean up build.rssharkdpbefore adoptiona85f33c
lines 24–31why, one step back24 Ok(output) => {25 // While this lock is active, this thread will be the only thread allowed26 // to write its outputs.27 let _lock = out_perm.lock().unwrap();2829 let stdout = io::stdout();30 let stderr = io::stderr();31No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
lines 32–33why, one step back32 let _ = stdout.lock().write_all(&output.stdout);33 let _ = stderr.lock().write_all(&output.stderr);Clean up build.rssharkdpbefore adoptiona85f33c
lines 34–48why, one step back3435 if output.status.code() == Some(0) {36 ExitCode::Success37 } else {38 ExitCode::GeneralError39 }40 }41 Err(ref why) if why.kind() == io::ErrorKind::NotFound => {42 print_error(format!("Command not found: {:?}", cmd));43 ExitCode::GeneralError44 }45 Err(why) => {46 print_error(format!("Problem while executing command: {}", why));47 ExitCode::GeneralError48 }No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967
line 49why, one step back49 }Clean up build.rssharkdpbefore adoptiona85f33c
line 50why, one step back50}