Skip to content

Commit 6f16a1b

Browse files
committed
more clean up
1 parent a5711c0 commit 6f16a1b

File tree

3 files changed

+59
-38
lines changed

3 files changed

+59
-38
lines changed

examples/rust_cookbook.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() -> CmdResult {
1919
println!(
2020
"Top 10 biggest files and directories in '{}':\n{}",
2121
directory.display(),
22-
run_fun!(du -ah . | sort -hr | head -n 10).unwrap()
22+
run_fun!(du -ah . | sort -hr | head -n 10)?
2323
);
2424

2525
// Redirect both stdout and stderr of child process to the same file

src/child.rs

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use std::thread::JoinHandle;
1010
///
1111
/// Calling `spawn!` macro will return `Result<CmdChildren>`
1212
pub struct CmdChildren {
13-
children: Vec<CmdChild>,
13+
children: Vec<Result<CmdChild>>,
1414
ignore_error: bool,
1515
}
1616

1717
impl CmdChildren {
18-
pub(crate) fn new(children: Vec<CmdChild>, ignore_error: bool) -> Self {
18+
pub(crate) fn new(children: Vec<Result<CmdChild>>, ignore_error: bool) -> Self {
1919
Self {
2020
children,
2121
ignore_error,
@@ -32,19 +32,32 @@ impl CmdChildren {
3232
pub fn wait(&mut self) -> CmdResult {
3333
// wait for the last child result
3434
let handle = self.children.pop().unwrap();
35-
if let Err(e) = handle.wait(true) {
36-
let _ = Self::wait_children(&mut self.children);
37-
return Err(e);
35+
match handle {
36+
Err(e) => {
37+
let _ = Self::wait_children(&mut self.children);
38+
return Err(e);
39+
}
40+
Ok(handle) => {
41+
if let Err(e) = handle.wait(true) {
42+
let _ = Self::wait_children(&mut self.children);
43+
return Err(e);
44+
}
45+
}
3846
}
3947
Self::wait_children(&mut self.children)
4048
}
4149

42-
fn wait_children(children: &mut Vec<CmdChild>) -> CmdResult {
50+
fn wait_children(children: &mut Vec<Result<CmdChild>>) -> CmdResult {
4351
let mut ret = Ok(());
4452
while !children.is_empty() {
4553
let child_handle = children.pop().unwrap();
46-
if let Err(e) = child_handle.wait(false) {
47-
ret = Err(e);
54+
match child_handle {
55+
Err(e) => ret = Err(e),
56+
Ok(child_handle) => {
57+
if let Err(e) = child_handle.wait(false) {
58+
ret = Err(e);
59+
}
60+
}
4861
}
4962
}
5063
ret
@@ -56,40 +69,48 @@ impl CmdChildren {
5669
///
5770
/// Calling `spawn_with_output!` macro will return `Result<FunChildren>`
5871
pub struct FunChildren {
59-
children: Vec<CmdChild>,
72+
children: Vec<Result<CmdChild>>,
6073
ignore_error: bool,
6174
}
6275

6376
impl FunChildren {
6477
pub fn wait_with_output(&mut self) -> FunResult {
6578
// wait for the last child result
6679
let handle = self.children.pop().unwrap();
67-
let wait_last = handle.wait_with_output(self.ignore_error);
68-
match wait_last {
80+
match handle {
6981
Err(e) => {
7082
let _ = CmdChildren::wait_children(&mut self.children);
7183
Err(e)
7284
}
73-
Ok(output) => {
74-
let mut s = String::from_utf8_lossy(&output).to_string();
75-
if s.ends_with('\n') {
76-
s.pop();
77-
}
78-
let ret = CmdChildren::wait_children(&mut self.children);
79-
if let Err(e) = ret {
80-
if !self.ignore_error {
81-
return Err(e);
85+
Ok(handle) => {
86+
let wait_last = handle.wait_with_output(self.ignore_error);
87+
match wait_last {
88+
Err(e) => {
89+
let _ = CmdChildren::wait_children(&mut self.children);
90+
Err(e)
91+
}
92+
Ok(output) => {
93+
let mut s = String::from_utf8_lossy(&output).to_string();
94+
if s.ends_with('\n') {
95+
s.pop();
96+
}
97+
let ret = CmdChildren::wait_children(&mut self.children);
98+
if let Err(e) = ret {
99+
if !self.ignore_error {
100+
return Err(e);
101+
}
102+
}
103+
Ok(s)
82104
}
83105
}
84-
Ok(s)
85106
}
86107
}
87108
}
88109

89110
pub fn wait_with_pipe(&mut self, f: &mut dyn FnMut(Box<dyn Read>)) -> CmdResult {
90-
let child = self.children.pop().unwrap();
111+
let child = self.children.pop().unwrap()?;
91112
let polling_stderr = StderrLogging::new(&child.cmd, child.stderr);
92-
match child.handle? {
113+
match child.handle {
93114
CmdChildHandle::Proc(mut proc) => {
94115
if let Some(stdout) = child.stdout {
95116
f(Box::new(stdout));
@@ -113,15 +134,15 @@ impl FunChildren {
113134
}
114135

115136
pub(crate) struct CmdChild {
116-
handle: Result<CmdChildHandle>,
137+
handle: CmdChildHandle,
117138
cmd: String,
118139
stdout: Option<PipeReader>,
119140
stderr: Option<PipeReader>,
120141
}
121142

122143
impl CmdChild {
123144
pub(crate) fn new(
124-
handle: Result<CmdChildHandle>,
145+
handle: CmdChildHandle,
125146
cmd: String,
126147
stdout: Option<PipeReader>,
127148
stderr: Option<PipeReader>,
@@ -135,7 +156,7 @@ impl CmdChild {
135156
}
136157

137158
fn wait(self, is_last: bool) -> CmdResult {
138-
let res = self.handle?.wait_with_stderr(self.stderr, &self.cmd);
159+
let res = self.handle.wait_with_stderr(self.stderr, &self.cmd);
139160
if let Err(e) = res {
140161
if is_last || process::pipefail_enabled() {
141162
return Err(e);
@@ -158,7 +179,7 @@ impl CmdChild {
158179
vec![]
159180
}
160181
};
161-
let res = self.handle?.wait_with_stderr(self.stderr, &self.cmd);
182+
let res = self.handle.wait_with_stderr(self.stderr, &self.cmd);
162183
if let Err(e) = res {
163184
if !ignore_error {
164185
return Err(e);

src/process.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Cmds {
190190
}
191191

192192
// spawning all the sub-processes
193-
let mut children: Vec<CmdChild> = Vec::new();
193+
let mut children: Vec<Result<CmdChild>> = Vec::new();
194194
let len = self.cmds.len();
195195
let mut prev_pipe_in = None;
196196
for (i, cmd_opt) in self.cmds.iter_mut().enumerate() {
@@ -203,7 +203,7 @@ impl Cmds {
203203
} else {
204204
cmd.setup_redirects(&mut prev_pipe_in, None, with_output)?;
205205
}
206-
let child = cmd.spawn(current_dir, with_output)?;
206+
let child = cmd.spawn(current_dir, with_output);
207207
children.push(child);
208208
}
209209

@@ -364,9 +364,9 @@ impl Cmd {
364364
fn spawn(mut self, current_dir: &mut PathBuf, with_output: bool) -> Result<CmdChild> {
365365
let arg0 = self.arg0();
366366
if arg0 == CD_CMD {
367-
let child = self.run_cd_cmd(current_dir);
367+
let child = self.run_cd_cmd(current_dir)?;
368368
Ok(CmdChild::new(
369-
child.map(CmdChildHandle::SyncFn),
369+
CmdChildHandle::SyncFn(child),
370370
self.cmd_str(),
371371
self.stdout_logging,
372372
self.stderr_logging,
@@ -406,17 +406,17 @@ impl Cmd {
406406

407407
let internal_cmd = CMD_MAP.lock().unwrap()[&arg0];
408408
if pipe_out || with_output {
409-
let handle = thread::Builder::new().spawn(move || internal_cmd(&mut env));
409+
let handle = thread::Builder::new().spawn(move || internal_cmd(&mut env))?;
410410
Ok(CmdChild::new(
411-
handle.map(CmdChildHandle::Thread),
411+
CmdChildHandle::Thread(handle),
412412
cmd_str,
413413
self.stdout_logging,
414414
self.stderr_logging,
415415
))
416416
} else {
417-
let child = internal_cmd(&mut env);
417+
let child = internal_cmd(&mut env)?;
418418
Ok(CmdChild::new(
419-
child.map(CmdChildHandle::SyncFn),
419+
CmdChildHandle::SyncFn(child),
420420
cmd_str,
421421
self.stdout_logging,
422422
self.stderr_logging,
@@ -446,9 +446,9 @@ impl Cmd {
446446
}
447447

448448
// spawning process
449-
let child = cmd.spawn();
449+
let child = cmd.spawn()?;
450450
Ok(CmdChild::new(
451-
child.map(CmdChildHandle::Proc),
451+
CmdChildHandle::Proc(child),
452452
self.cmd_str(),
453453
self.stdout_logging,
454454
self.stderr_logging,

0 commit comments

Comments
 (0)