Skip to content

Commit

Permalink
Update custom command API signature
Browse files Browse the repository at this point in the history
To allow spawning process with stdin/stdout redirection.
  • Loading branch information
tao-guo committed Nov 25, 2023
1 parent 787e35d commit 53b6b1f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn main(
/// ```
/// # use cmd_lib::*;
/// fn my_cmd(env: &mut CmdEnv) -> CmdResult {
/// let msg = format!("msg from foo(), args: {:?}\n", env.args());
/// writeln!(env.stderr(), "{}", msg)?;
/// let msg = format!("msg from foo(), args: {:?}", env.get_args());
/// writeln!(env.stderr(), "{msg}")?;
/// writeln!(env.stdout(), "bar")
/// }
///
Expand Down
10 changes: 10 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ impl From<CmdIn> for Stdio {
}
}

impl CmdIn {
pub fn try_clone(&self) -> Result<Self> {
match self {
CmdIn::Null => Ok(CmdIn::Null),
CmdIn::File(file) => file.try_clone().map(CmdIn::File),
CmdIn::Pipe(pipe) => pipe.try_clone().map(CmdIn::Pipe),
}
}
}

pub enum CmdOut {
Null,
File(File),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ pub type FunResult = std::io::Result<String>;
/// Return type for [`run_cmd!()`] macro.
pub type CmdResult = std::io::Result<()>;
pub use child::{CmdChildren, FunChildren};
pub use io::{CmdIn, CmdOut};
#[doc(hidden)]
pub use log as inner_log;
#[doc(hidden)]
Expand Down
8 changes: 4 additions & 4 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::fs::{File, OpenOptions};
use std::io::{Error, ErrorKind, Read, Result, Write};
use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;
Expand Down Expand Up @@ -50,17 +50,17 @@ impl CmdEnv {
}

/// Returns a new handle to the standard input for this command.
pub fn stdin(&mut self) -> impl Read + '_ {
pub fn stdin(&mut self) -> &mut CmdIn {
&mut self.stdin
}

/// Returns a new handle to the standard output for this command.
pub fn stdout(&mut self) -> impl Write + '_ {
pub fn stdout(&mut self) -> &mut CmdOut {
&mut self.stdout
}

/// Returns a new handle to the standard error for this command.
pub fn stderr(&mut self) -> impl Write + '_ {
pub fn stderr(&mut self) -> &mut CmdOut {
&mut self.stderr
}
}
Expand Down

0 comments on commit 53b6b1f

Please sign in to comment.