pub type Result = ::std::result::Result<String, Error>;
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
pub code: i32,
pub stdout: String,
pub stderr: String,
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
"Unix command failed."
}
}
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", self.stderr)
}
}
#[macro_export]
macro_rules! sh {
( $( $cmd:tt )* ) => {{
$crate::execute_with("sh", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! ash {
( $( $cmd:tt )* ) => {{
$crate::execute_with("ash", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! csh {
( $( $cmd:tt )* ) => {{
$crate::execute_with("csh", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! ksh {
( $( $cmd:tt )* ) => {{
$crate::execute_with("ksh", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! zsh {
( $( $cmd:tt )* ) => {{
$crate::execute_with("zsh", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! bash {
( $( $cmd:tt )* ) => {{
$crate::execute_with("bash", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! dash {
( $( $cmd:tt )* ) => {{
$crate::execute_with("dash", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! fish {
( $( $cmd:tt )* ) => {{
$crate::execute_with("fish", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! mksh {
( $( $cmd:tt )* ) => {{
$crate::execute_with("mksh", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! tcsh {
( $( $cmd:tt )* ) => {{
$crate::execute_with("tcsh", &format!($( $cmd )*))
}};
}
#[macro_export]
macro_rules! wrap_sh {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("sh", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_ash {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("ash", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_csh {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("csh", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_ksh {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("ksh", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_zsh {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("zsh", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_bash {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("bash", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_dash {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("dash", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_fish {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("fish", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_mksh {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("mksh", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[macro_export]
macro_rules! wrap_tcsh {
( $( $cmd:tt )* ) => {{
match $crate::execute_with("tcsh", &format!($( $cmd )*)) {
(0, stdout, _) => Ok(stdout),
(code, stdout, stderr) => {
Err($crate::Error {
code: code,
stdout: stdout,
stderr: stderr,
})
},
}
}};
}
#[doc(hidden)]
pub fn execute_with(shell: &str, cmd: &String) -> (i32, String, String) {
let mut command = {
let mut command = ::std::process::Command::new(shell);
command.arg("-c").arg(cmd);
command
};
match command.output() {
Ok(output) => {
(output.status.code().unwrap_or(if output.status.success() { 0 } else { 1 }),
String::from_utf8_lossy(&output.stdout[..]).into_owned(),
String::from_utf8_lossy(&output.stderr[..]).into_owned())
},
Err(e) => (126, String::new(), e.to_string()),
}
}