Skip to content
Myr

command.rs

This file

Path

src/exec/command.rs

110 lines · 3.1 KiB · Rust

Last changed by

Use to_string_lossy to avoid relying on Debug-outputsMarie Katrine Ekebergbefore adoptiond6b1d16

History of this file

Take it

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

Update dependenciesDavid Peterbefore adoption67aaf82

lines 1–4why, one step back
1use std::io;
2use std::io::Write;3use std::sync::Mutex;4

exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e

lines 5–6why, one step back
5use argmax::Command;
6

Update dependenciesDavid Peterbefore adoption67aaf82

lines 7–9why, one step back
7use crate::error::print_error;
8use crate::exit_codes::ExitCode;9

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

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

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

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

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

line 21why, one step back
21 Self {

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

line 22why, one step back
22 output_permission,

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 23–37why, one step back
23 outputs: Vec::new(),
24 }25 }2627 fn push(&mut self, stdout: Vec<u8>, stderr: Vec<u8>) {28 self.outputs.push(Outputs { stdout, stderr });29 }3031 fn write(self) {32 // avoid taking the lock if there is nothing to do33 if self.outputs.is_empty() {34 return;35 }36 // While this lock is active, this thread will be the only thread allowed37 // to write its outputs.

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 39–52why, one step back
39
40 let stdout = io::stdout();41 let stderr = io::stderr();4243 let mut stdout = stdout.lock();44 let mut stderr = stderr.lock();4546 for output in self.outputs.iter() {47 let _ = stdout.write_all(&output.stdout);48 let _ = stderr.write_all(&output.stderr);49 }50 }51}52

Update dependenciesDavid Peterbefore adoption67aaf82

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

exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e

line 54why, one step back
54pub fn execute_commands<I: Iterator<Item = io::Result<Command>>>(

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

line 55why, one step back
55 cmds: I,

Update dependenciesDavid Peterbefore adoption67aaf82

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

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e

lines 60–65why, one step back
60 for result in cmds {
61 let mut cmd = match result {62 Ok(cmd) => cmd,63 Err(e) => return handle_cmd_error(None, e),64 };65

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 66–73why, one step back
66 // Spawn the supplied command.
67 let output = if enable_output_buffering {68 cmd.output()69 } else {70 // If running on only one thread, don't buffer output71 // Allows for viewing and interacting with intermediate command output72 cmd.spawn().and_then(|c| c.wait_with_output())73 };

Update dependenciesDavid Peterbefore adoption67aaf82

line 74why, one step back
74

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 75–78why, one step back
75 // Then wait for the command to exit, if it was spawned.
76 match output {77 Ok(output) => {78 if enable_output_buffering {

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

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

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 83–84why, one step back
83 return ExitCode::GeneralError;
84 }

Update dependenciesDavid Peterbefore adoption67aaf82

line 85why, one step back
85 }

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

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

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e

line 88why, one step back
88 return handle_cmd_error(Some(&cmd), why);

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

line 89why, one step back
89 }

Update dependenciesDavid Peterbefore adoption67aaf82

line 90why, one step back
90 }

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

line 91why, one step back
91 }

Use full names in command.rsThayne McCombsbefore adoption5a12a5e

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

Group together output from multi exec commandsThayne McCombsbefore adoption9fb0c5d

lines 93–95why, one step back
93 ExitCode::Success
94}95

exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e

lines 96–98why, one step back
96pub fn handle_cmd_error(cmd: Option<&Command>, err: io::Error) -> ExitCode {
97 match (cmd, err) {98 (Some(cmd), err) if err.kind() == io::ErrorKind::NotFound => {

Use to_string_lossy to avoid relying on Debug-outputsMarie Katrine Ekebergbefore adoptiond6b1d16

lines 99–102why, one step back
99 print_error(format!(
100 "Command not found: {}",101 cmd.get_program().to_string_lossy()102 ));

exec: Execute batches before they get too longTavian Barnesbefore adoption40b368e

lines 103–108why, one step back
103 ExitCode::GeneralError
104 }105 (_, err) => {106 print_error(format!("Problem while executing command: {}", err));107 ExitCode::GeneralError108 }

Update dependenciesDavid Peterbefore adoption67aaf82

lines 109–110why, one step back
109 }
110}