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

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

Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9

lines 1–8why, 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;8

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 9–12why, one step back
9struct Outputs {
10 stdout: Vec<u8>,11 stderr: Vec<u8>,12}

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

lines 13–14why, one step back
13struct OutputBuffer<'a> {
14 output_permission: &'a Mutex<()>,

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 15–17why, one step back
15 outputs: Vec<Outputs>,
16}17

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

lines 18–19why, one step back
18impl<'a> OutputBuffer<'a> {
19 fn new(output_permission: &'a Mutex<()>) -> Self {

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

line 20why, one step back
20 Self {

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 21why, one step back
21 output_permission,

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 22–36why, one step back
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.

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 37why, one step back
37 let _lock = self.output_permission.lock().unwrap();

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 38–51why, one step back
38
39 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}51

Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9

line 52why, one step back
52/// Executes a command.

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 53–54why, one step back
53pub fn execute_commands<I: Iterator<Item = Command>>(
54 cmds: I,

Address feedback for removing buffering when running on a single threadThayne McCombsbefore adoptione473060

lines 55–57why, one step back
55 out_perm: &Mutex<()>,
56 enable_output_buffering: bool,57) -> ExitCode {

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 58why, one step back
58 let mut output_buffer = OutputBuffer::new(out_perm);

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 59–67why, one step back
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 };

Address feedback for removing buffering when running on a single threadThayne McCombsbefore adoptione473060

line 68why, one step back
68

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 69–72why, one step back
69 // Then wait for the command to exit, if it was spawned.
70 match output {71 Ok(output) => {72 if enable_output_buffering {

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 73why, one step back
73 output_buffer.push(output.stdout, output.stderr);

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 74–75why, one step back
74 }
75 if output.status.code() != Some(0) {

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 76why, one step back
76 output_buffer.write();

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 77–78why, one step back
77 return ExitCode::GeneralError;
78 }

Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9

line 79why, one step back
79 }

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

line 80why, one step back
80 Err(why) => {

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 81why, one step back
81 output_buffer.write();

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 82–83why, one step back
82 return handle_cmd_error(&cmd, why);
83 }

Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9

line 84why, one step back
84 }

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

line 85why, one step back
85 }

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 86why, one step back
86 output_buffer.write();

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 87–96why, one step back
87 ExitCode::Success
88}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::GeneralError

No buffering cmd output when running on 1 threadMax Trianobefore adoption394e967

line 97why, one step back
97 }

Add exact size support to the --size filter (#669)Platon Pronkobefore adoptione97a1e9

line 98why, one step back
98}