Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than a85f33c point at it.

Clean up build.rssharkdpbefore adoptiona85f33c

lines 1–9why, one step back
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;89/// Executes a command.

No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967

lines 10–11why, one step back
10pub 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 back
12 // Spawn the supplied command.

No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967

lines 13–15why, one step back
13 if is_multithread {
14 output = cmd.output();15 } else {

Clarifying commentsMax Trianobefore adoption154cd6e

lines 16–17why, one step back
16 // If running on only one thread, don't buffer output
17 // Allows for viewing and interacting with intermediate command output

No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967

lines 18–21why, one step back
18 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 back
22 // 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 back
23 match output {

Clean up build.rssharkdpbefore adoptiona85f33c

lines 24–31why, one step back
24 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();31

No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967

lines 32–33why, one step back
32 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 back
34
35 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 back
49 }

Clean up build.rssharkdpbefore adoptiona85f33c

line 50why, one step back
50}