2016-09-07 00:13:11 -07:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate futures;
|
|
|
|
|
extern crate tokio_core;
|
2016-11-18 20:57:31 +01:00
|
|
|
extern crate mio;
|
2016-09-07 00:13:11 -07:00
|
|
|
|
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
use std::io;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::process::{self, ExitStatus};
|
|
|
|
|
|
|
|
|
|
use futures::{Future, Poll};
|
2016-09-07 22:15:13 -07:00
|
|
|
use tokio_core::reactor::Handle;
|
2016-09-07 00:13:11 -07:00
|
|
|
|
|
|
|
|
#[path = "unix.rs"]
|
2016-09-07 12:04:46 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
mod imp;
|
|
|
|
|
|
|
|
|
|
#[path = "windows.rs"]
|
|
|
|
|
#[cfg(windows)]
|
2016-09-07 00:13:11 -07:00
|
|
|
mod imp;
|
|
|
|
|
|
2016-11-18 20:57:31 +01:00
|
|
|
pub use imp::ChildStdin;
|
|
|
|
|
pub use imp::ChildStdout;
|
|
|
|
|
|
2016-09-07 00:13:11 -07:00
|
|
|
pub struct Command {
|
|
|
|
|
inner: process::Command,
|
2016-09-07 12:04:46 -07:00
|
|
|
#[allow(dead_code)]
|
2016-09-07 22:15:13 -07:00
|
|
|
handle: Handle,
|
2016-09-07 00:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Spawn {
|
|
|
|
|
inner: Box<Future<Item=Child, Error=io::Error>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Child {
|
|
|
|
|
inner: imp::Child,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Command {
|
2016-09-07 22:15:13 -07:00
|
|
|
pub fn new<T: AsRef<OsStr>>(exe: T, handle: &Handle) -> Command {
|
2016-09-07 00:13:11 -07:00
|
|
|
Command::_new(exe.as_ref(), handle)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:15:13 -07:00
|
|
|
fn _new(exe: &OsStr, handle: &Handle) -> Command {
|
2016-09-07 00:13:11 -07:00
|
|
|
Command {
|
|
|
|
|
inner: process::Command::new(exe),
|
|
|
|
|
handle: handle.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
|
|
|
|
|
self._arg(arg.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _arg(&mut self, arg: &OsStr) -> &mut Command {
|
|
|
|
|
self.inner.arg(arg);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command {
|
|
|
|
|
for arg in args {
|
|
|
|
|
self._arg(arg.as_ref());
|
|
|
|
|
}
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
|
|
|
|
|
where K: AsRef<OsStr>, V: AsRef<OsStr>
|
|
|
|
|
{
|
|
|
|
|
self._env(key.as_ref(), val.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _env(&mut self, key: &OsStr, val: &OsStr) -> &mut Command {
|
|
|
|
|
self.inner.env(key, val);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
|
|
|
|
|
self._env_remove(key.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _env_remove(&mut self, key: &OsStr) -> &mut Command {
|
|
|
|
|
self.inner.env_remove(key);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn env_clear(&mut self) -> &mut Command {
|
|
|
|
|
self.inner.env_clear();
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
|
|
|
|
|
self._current_dir(dir.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _current_dir(&mut self, dir: &Path) -> &mut Command {
|
|
|
|
|
self.inner.current_dir(dir);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 20:57:31 +01:00
|
|
|
pub fn stdin(&mut self, cfg: process::Stdio) -> &mut Self {
|
|
|
|
|
self.inner.stdin(cfg);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn stdout(&mut self, cfg: process::Stdio) -> &mut Self {
|
|
|
|
|
self.inner.stdout(cfg);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
pub fn stderr(&mut self, cfg: process::Stdio) -> &mut Self {
|
|
|
|
|
self.inner.stderr(cfg);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 00:13:11 -07:00
|
|
|
pub fn spawn(self) -> Spawn {
|
|
|
|
|
Spawn {
|
|
|
|
|
inner: Box::new(imp::spawn(self).map(|c| Child { inner: c })),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Future for Spawn {
|
|
|
|
|
type Item = Child;
|
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Child, io::Error> {
|
|
|
|
|
self.inner.poll()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Child {
|
|
|
|
|
pub fn id(&self) -> u32 {
|
|
|
|
|
self.inner.id()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn kill(&mut self) -> io::Result<()> {
|
|
|
|
|
self.inner.kill()
|
|
|
|
|
}
|
2016-11-18 20:57:31 +01:00
|
|
|
|
|
|
|
|
pub fn stdin(&mut self) -> &mut Option<imp::ChildStdin> {
|
|
|
|
|
&mut self.inner.stdin
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn stdout(&mut self) -> &mut Option<imp::ChildStdout> {
|
|
|
|
|
&mut self.inner.stdout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn stderr(&mut self) -> &mut Option<imp::ChildStderr> {
|
|
|
|
|
&mut self.inner.stderr
|
|
|
|
|
}
|
2016-09-07 00:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Future for Child {
|
|
|
|
|
type Item = ExitStatus;
|
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<ExitStatus, io::Error> {
|
|
|
|
|
self.inner.poll()
|
|
|
|
|
}
|
|
|
|
|
}
|