Skip to content

Commit fd493af

Browse files
committed
process cleanup: move CmdChildren to child.rs
1 parent a91efa3 commit fd493af

File tree

2 files changed

+98
-94
lines changed

2 files changed

+98
-94
lines changed

src/child.rs

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,104 @@
1-
use crate::CmdResult;
2-
use log::info;
3-
use os_pipe::{self, PipeReader};
1+
use crate::{CmdResult, FunResult};
2+
use log::{error, info};
3+
use os_pipe::PipeReader;
44
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Result};
55
use std::process::{Child, ExitStatus};
66
use std::thread::JoinHandle;
77
use CmdChild::{ProcChild, SyncChild, ThreadChild};
88

9+
pub struct CmdChildren(Vec<CmdChild>);
10+
impl CmdChildren {
11+
pub fn from(children: Vec<CmdChild>) -> Self {
12+
Self(children)
13+
}
14+
15+
pub fn wait_cmd(&mut self) -> CmdResult {
16+
let ret = self.wait_cmd_nolog();
17+
if let Err(ref err) = ret {
18+
error!(
19+
"Running {} failed, Error: {}",
20+
CmdChild::get_full_cmd(&self.0),
21+
err
22+
);
23+
}
24+
ret
25+
}
26+
27+
pub(crate) fn wait_cmd_nolog(&mut self) -> CmdResult {
28+
// wait last process result
29+
let handle = self.0.pop().unwrap();
30+
handle.wait(true)?;
31+
Self::wait_children(&mut self.0)
32+
}
33+
34+
fn wait_children(children: &mut Vec<CmdChild>) -> CmdResult {
35+
while !children.is_empty() {
36+
let child_handle = children.pop().unwrap();
37+
child_handle.wait(false)?;
38+
}
39+
Ok(())
40+
}
41+
42+
pub fn wait_raw_result(&mut self) -> Result<Vec<u8>> {
43+
let ret = self.wait_raw_result_nolog();
44+
if let Err(ref err) = ret {
45+
error!(
46+
"Running {} failed, Error: {}",
47+
CmdChild::get_full_cmd(&self.0),
48+
err
49+
);
50+
}
51+
ret
52+
}
53+
54+
fn wait_raw_result_nolog(&mut self) -> Result<Vec<u8>> {
55+
let handle = self.0.pop().unwrap();
56+
let wait_last = handle.wait_with_output();
57+
match wait_last {
58+
Err(e) => {
59+
let _ = Self::wait_children(&mut self.0);
60+
Err(e)
61+
}
62+
Ok(output) => {
63+
Self::wait_children(&mut self.0)?;
64+
Ok(output)
65+
}
66+
}
67+
}
68+
69+
pub fn wait_fun(&mut self) -> FunResult {
70+
let ret = self.wait_fun_nolog();
71+
if let Err(ref err) = ret {
72+
error!(
73+
"Running {} failed, Error: {}",
74+
CmdChild::get_full_cmd(&self.0),
75+
err
76+
);
77+
}
78+
ret
79+
}
80+
81+
pub(crate) fn wait_fun_nolog(&mut self) -> FunResult {
82+
// wait last process result
83+
let handle = self.0.pop().unwrap();
84+
let wait_last = handle.wait_with_output();
85+
match wait_last {
86+
Err(e) => {
87+
let _ = CmdChildren::wait_children(&mut self.0);
88+
Err(e)
89+
}
90+
Ok(output) => {
91+
let mut ret = String::from_utf8_lossy(&output).to_string();
92+
if ret.ends_with('\n') {
93+
ret.pop();
94+
}
95+
CmdChildren::wait_children(&mut self.0)?;
96+
Ok(ret)
97+
}
98+
}
99+
}
100+
}
101+
9102
#[derive(Debug)]
10103
pub enum CmdChild {
11104
ProcChild {

src/process.rs

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::child::CmdChild;
1+
use crate::child::{CmdChild, CmdChildren};
22
use crate::io::{CmdIn, CmdOut};
33
use crate::{builtin_true, CmdResult, FunResult};
44
use faccess::{AccessMode, PathExt};
@@ -192,7 +192,7 @@ impl Cmds {
192192
children.push(child);
193193
}
194194

195-
Ok(CmdChildren(children))
195+
Ok(CmdChildren::from(children))
196196
}
197197

198198
fn run_cmd(&mut self, current_dir: &mut String) -> CmdResult {
@@ -204,95 +204,6 @@ impl Cmds {
204204
}
205205
}
206206

207-
pub struct CmdChildren(Vec<CmdChild>);
208-
impl CmdChildren {
209-
pub fn wait_cmd(&mut self) -> CmdResult {
210-
let ret = self.wait_cmd_nolog();
211-
if let Err(ref err) = ret {
212-
error!(
213-
"Running {} failed, Error: {}",
214-
CmdChild::get_full_cmd(&self.0),
215-
err
216-
);
217-
}
218-
ret
219-
}
220-
221-
fn wait_cmd_nolog(&mut self) -> CmdResult {
222-
// wait last process result
223-
let handle = self.0.pop().unwrap();
224-
handle.wait(true)?;
225-
Self::wait_children(&mut self.0)
226-
}
227-
228-
fn wait_children(children: &mut Vec<CmdChild>) -> CmdResult {
229-
while !children.is_empty() {
230-
let child_handle = children.pop().unwrap();
231-
child_handle.wait(false)?;
232-
}
233-
Ok(())
234-
}
235-
236-
pub fn wait_raw_result(&mut self) -> Result<Vec<u8>> {
237-
let ret = self.wait_raw_result_nolog();
238-
if let Err(ref err) = ret {
239-
error!(
240-
"Running {} failed, Error: {}",
241-
CmdChild::get_full_cmd(&self.0),
242-
err
243-
);
244-
}
245-
ret
246-
}
247-
248-
fn wait_raw_result_nolog(&mut self) -> Result<Vec<u8>> {
249-
let handle = self.0.pop().unwrap();
250-
let wait_last = handle.wait_with_output();
251-
match wait_last {
252-
Err(e) => {
253-
let _ = Self::wait_children(&mut self.0);
254-
Err(e)
255-
}
256-
Ok(output) => {
257-
Self::wait_children(&mut self.0)?;
258-
Ok(output)
259-
}
260-
}
261-
}
262-
263-
pub fn wait_fun(&mut self) -> FunResult {
264-
let ret = self.wait_fun_nolog();
265-
if let Err(ref err) = ret {
266-
error!(
267-
"Running {} failed, Error: {}",
268-
CmdChild::get_full_cmd(&self.0),
269-
err
270-
);
271-
}
272-
ret
273-
}
274-
275-
fn wait_fun_nolog(&mut self) -> FunResult {
276-
// wait last process result
277-
let handle = self.0.pop().unwrap();
278-
let wait_last = handle.wait_with_output();
279-
match wait_last {
280-
Err(e) => {
281-
let _ = CmdChildren::wait_children(&mut self.0);
282-
Err(e)
283-
}
284-
Ok(output) => {
285-
let mut ret = String::from_utf8_lossy(&output).to_string();
286-
if ret.ends_with('\n') {
287-
ret.pop();
288-
}
289-
CmdChildren::wait_children(&mut self.0)?;
290-
Ok(ret)
291-
}
292-
}
293-
}
294-
}
295-
296207
#[doc(hidden)]
297208
pub enum Redirect {
298209
FileToStdin(String),

0 commit comments

Comments
 (0)