Skip to content

Commit a91efa3

Browse files
committed
process cleanup: simplify wait structure
use CmdChildren for waiting
1 parent e21ed6c commit a91efa3

File tree

4 files changed

+29
-74
lines changed

4 files changed

+29
-74
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,13 @@ println!("get result: {}", run_fun!(my_cmd)?);
227227
#### Low-level process spawning macros
228228

229229
`spawn!()` macro executes the whole command as a child process, returning a handle to it. By
230-
default, stdin, stdout and stderr are inherited from the parent. To capture the output, you
231-
can use `spawn_with_output!()` macro instead.
230+
default, stdin, stdout and stderr are inherited from the parent.
232231

233-
To get result, you can call `wait_result()` to get CmdResult/FunResult.
232+
To get result, you can call `wait_cmd()` or `wait_fun()` to get CmdResult/FunResult.
234233

235234
```rust
236-
spawn!(ping -c 10 192.168.0.1)?.wait_result()?;
237-
let output = spawn_with_output!(/bin/cat file.txt | sed s/a/b/)?.wait_result();
235+
spawn!(ping -c 10 192.168.0.1)?.wait_cmd()?;
236+
let output = spawn!(/bin/cat file.txt | sed s/a/b/)?.wait_fun()?;
238237
```
239238

240239

macros/src/lib.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ pub fn run_fun(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
167167
}
168168

169169
/// Run commands with/without pipes as a child process, returning a handle to check the final
170-
/// status
170+
/// result
171171
/// ```no_run
172172
/// # use cmd_lib::*;
173173
///
174174
/// let handle = spawn!(ping -c 10 192.168.0.1)?;
175175
/// // ...
176-
/// if handle.wait_result().is_err() {
176+
/// if handle.wait_cmd().is_err() {
177177
/// // ...
178178
/// }
179179
/// # Ok::<(), std::io::Error>(())
@@ -187,34 +187,6 @@ pub fn spawn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
187187
.into()
188188
}
189189

190-
/// Run commands with/without pipes as a child process, returning a handle to capture the
191-
/// final output
192-
/// ```no_run
193-
/// # use cmd_lib::*;
194-
/// // from examples/dd_test.rs:
195-
/// let mut procs = vec![];
196-
/// for _ in 0..4 {
197-
/// let proc = spawn_with_output!(
198-
/// sudo bash -c "dd if=$file of=/dev/null bs=$block_size skip=$off count=$cnt 2>&1"
199-
/// )?;
200-
/// }
201-
///
202-
/// for (i, mut proc) in procs.into_iter().enumerate() {
203-
/// let output = proc.wait_result()?;
204-
/// run_cmd!(info "thread $i bandwidth: $bandwidth MB/s")?;
205-
/// }
206-
/// # Ok::<(), std::io::Error>(())
207-
/// ```
208-
#[proc_macro]
209-
#[proc_macro_error]
210-
pub fn spawn_with_output(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
211-
let cmds = lexer::Lexer::new(input.into()).scan().parse(true);
212-
quote! ({
213-
#cmds.spawn_with_output()
214-
})
215-
.into()
216-
}
217-
218190
/// Logs a message at the error level with interpolation support
219191
#[proc_macro]
220192
#[proc_macro_error]

src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,14 @@
252252
//! ### Low-level process spawning macros
253253
//!
254254
//! `spawn!()` macro executes the whole command as a child process, returning a handle to it. By
255-
//! default, stdin, stdout and stderr are inherited from the parent. To capture the output, you
256-
//! can use `spawn_with_output!()` macro instead.
255+
//! default, stdin, stdout and stderr are inherited from the parent.
257256
//!
258-
//! To get result, you can call `wait_result()` to get CmdResult/FunResult.
257+
//! To get result, you can call `wait_cmd()` or `wait_fun()` to get CmdResult/FunResult.
259258
//!
260259
//! ```no_run
261-
//! # use cmd_lib::{spawn, spawn_with_output};
262-
//! spawn!(ping -c 10 192.168.0.1)?.wait_result()?;
263-
//! let output = spawn_with_output!(/bin/cat file.txt | sed s/a/b/)?.wait_result();
260+
//! # use cmd_lib::spawn;
261+
//! spawn!(ping -c 10 192.168.0.1)?.wait_cmd()?;
262+
//! let output = spawn!(/bin/cat file.txt | sed s/a/b/)?.wait_fun()?;
264263
//! # Ok::<(), std::io::Error>(())
265264
//! ```
266265
//!
@@ -322,7 +321,7 @@
322321
323322
pub use cmd_lib_macros::{
324323
cmd_debug, cmd_die, cmd_echo, cmd_error, cmd_info, cmd_trace, cmd_warn, export_cmd, run_cmd,
325-
run_fun, spawn, spawn_with_output, use_builtin_cmd, use_custom_cmd,
324+
run_fun, spawn, use_builtin_cmd, use_custom_cmd,
326325
};
327326
/// Return type for run_fun!() macro
328327
pub type FunResult = std::io::Result<String>;

src/process.rs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl GroupCmds {
137137
}
138138
}
139139

140-
pub fn spawn(mut self) -> Result<WaitCmd> {
140+
pub fn spawn(mut self) -> Result<CmdChildren> {
141141
assert_eq!(self.group_cmds.len(), 1);
142142
let mut cmds = self.group_cmds.pop().unwrap().0;
143143
let ret = cmds.spawn(&mut self.current_dir);
@@ -146,18 +146,6 @@ impl GroupCmds {
146146
}
147147
ret
148148
}
149-
150-
pub fn spawn_with_output(mut self) -> Result<WaitFun> {
151-
assert_eq!(self.group_cmds.len(), 1);
152-
let mut cmds = self.group_cmds.pop().unwrap().0;
153-
match cmds.spawn(&mut self.current_dir) {
154-
Ok(ret) => Ok(WaitFun(ret.0)),
155-
Err(err) => {
156-
error!("Spawning {} failed, Error: {}", cmds.get_full_cmds(), err);
157-
Err(err)
158-
}
159-
}
160-
}
161149
}
162150

163151
#[doc(hidden)]
@@ -181,7 +169,7 @@ impl Cmds {
181169
&self.full_cmds
182170
}
183171

184-
fn spawn(&mut self, current_dir: &mut String) -> Result<WaitCmd> {
172+
fn spawn(&mut self, current_dir: &mut String) -> Result<CmdChildren> {
185173
if std::env::var("CMD_LIB_DEBUG") == Ok("1".into()) {
186174
debug!("Running {} ...", self.get_full_cmds());
187175
}
@@ -204,22 +192,22 @@ impl Cmds {
204192
children.push(child);
205193
}
206194

207-
Ok(WaitCmd(children))
195+
Ok(CmdChildren(children))
208196
}
209197

210198
fn run_cmd(&mut self, current_dir: &mut String) -> CmdResult {
211-
self.spawn(current_dir)?.wait_result_nolog()
199+
self.spawn(current_dir)?.wait_cmd_nolog()
212200
}
213201

214202
fn run_fun(&mut self, current_dir: &mut String) -> FunResult {
215-
WaitFun(self.spawn(current_dir)?.0).wait_result_nolog()
203+
self.spawn(current_dir)?.wait_fun_nolog()
216204
}
217205
}
218206

219-
pub struct WaitCmd(Vec<CmdChild>);
220-
impl WaitCmd {
221-
pub fn wait_result(&mut self) -> CmdResult {
222-
let ret = self.wait_result_nolog();
207+
pub struct CmdChildren(Vec<CmdChild>);
208+
impl CmdChildren {
209+
pub fn wait_cmd(&mut self) -> CmdResult {
210+
let ret = self.wait_cmd_nolog();
223211
if let Err(ref err) = ret {
224212
error!(
225213
"Running {} failed, Error: {}",
@@ -230,7 +218,7 @@ impl WaitCmd {
230218
ret
231219
}
232220

233-
fn wait_result_nolog(&mut self) -> CmdResult {
221+
fn wait_cmd_nolog(&mut self) -> CmdResult {
234222
// wait last process result
235223
let handle = self.0.pop().unwrap();
236224
handle.wait(true)?;
@@ -244,10 +232,7 @@ impl WaitCmd {
244232
}
245233
Ok(())
246234
}
247-
}
248235

249-
pub struct WaitFun(Vec<CmdChild>);
250-
impl WaitFun {
251236
pub fn wait_raw_result(&mut self) -> Result<Vec<u8>> {
252237
let ret = self.wait_raw_result_nolog();
253238
if let Err(ref err) = ret {
@@ -265,18 +250,18 @@ impl WaitFun {
265250
let wait_last = handle.wait_with_output();
266251
match wait_last {
267252
Err(e) => {
268-
let _ = WaitCmd::wait_children(&mut self.0);
253+
let _ = Self::wait_children(&mut self.0);
269254
Err(e)
270255
}
271256
Ok(output) => {
272-
WaitCmd::wait_children(&mut self.0)?;
257+
Self::wait_children(&mut self.0)?;
273258
Ok(output)
274259
}
275260
}
276261
}
277262

278-
pub fn wait_result(&mut self) -> FunResult {
279-
let ret = self.wait_result_nolog();
263+
pub fn wait_fun(&mut self) -> FunResult {
264+
let ret = self.wait_fun_nolog();
280265
if let Err(ref err) = ret {
281266
error!(
282267
"Running {} failed, Error: {}",
@@ -287,21 +272,21 @@ impl WaitFun {
287272
ret
288273
}
289274

290-
fn wait_result_nolog(&mut self) -> FunResult {
275+
fn wait_fun_nolog(&mut self) -> FunResult {
291276
// wait last process result
292277
let handle = self.0.pop().unwrap();
293278
let wait_last = handle.wait_with_output();
294279
match wait_last {
295280
Err(e) => {
296-
let _ = WaitCmd::wait_children(&mut self.0);
281+
let _ = CmdChildren::wait_children(&mut self.0);
297282
Err(e)
298283
}
299284
Ok(output) => {
300285
let mut ret = String::from_utf8_lossy(&output).to_string();
301286
if ret.ends_with('\n') {
302287
ret.pop();
303288
}
304-
WaitCmd::wait_children(&mut self.0)?;
289+
CmdChildren::wait_children(&mut self.0)?;
305290
Ok(ret)
306291
}
307292
}

0 commit comments

Comments
 (0)