2021-07-20 15:07:55 +05:30
|
|
|
use anyhow::Result;
|
2021-10-18 21:29:17 +05:30
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::fmt;
|
|
|
|
use std::process;
|
2021-07-20 15:07:55 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-20 23:41:46 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! args {
|
|
|
|
( $( $arg: expr ),* ) => {
|
|
|
|
{
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let args = [$( OsStr::new($arg) ),*];
|
|
|
|
args
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:29:17 +05:30
|
|
|
/// Holds a set of arguments for a shell command
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Command {
|
|
|
|
program: OsString,
|
|
|
|
args: Vec<OsString>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! cmd {
|
|
|
|
( $program:expr $(, $arg:expr )* $(,)? ) => {
|
|
|
|
{
|
|
|
|
// use std::ffi::OsString;
|
|
|
|
let args: &[OsString] = &[$( Into::<OsString>::into($arg) ),*];
|
|
|
|
Command::new($program, args)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
pub fn new(program: OsString, args: Vec<OsString>) -> Self {
|
|
|
|
Command { program, args }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_expr(&self) -> duct::Expression {
|
|
|
|
duct::cmd(&self.program, &self.args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Command {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.program.clone().into_string().unwrap())?;
|
|
|
|
for a in &self.args {
|
|
|
|
write!(f, " {}", a.clone().into_string().unwrap())?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log_output(output: &process::Output) {
|
|
|
|
use std::str::from_utf8;
|
|
|
|
|
|
|
|
if output.stdout.len() > 0 {
|
2021-10-19 18:50:47 +05:30
|
|
|
eprintln!("stdout: \n{}<<END>>", from_utf8(&output.stdout).unwrap());
|
2021-10-18 21:29:17 +05:30
|
|
|
}
|
|
|
|
if output.stderr.len() > 0 {
|
2021-10-19 18:50:47 +05:30
|
|
|
eprintln!("stderr: \n{}<<END>>", from_utf8(&output.stderr).unwrap());
|
2021-10-18 21:29:17 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:07:55 +05:30
|
|
|
// Returns stdout. The command must return zero.
|
2021-10-18 21:29:17 +05:30
|
|
|
pub fn run_ok(command: Command) -> Result<String> {
|
|
|
|
eprintln!("run_ok: {}", command);
|
|
|
|
|
|
|
|
let command = command.to_expr().stdout_capture().stderr_capture();
|
2021-07-20 15:07:55 +05:30
|
|
|
let output = command.run()?;
|
2021-10-18 21:29:17 +05:30
|
|
|
|
|
|
|
log_output(&output);
|
2021-07-20 15:07:55 +05:30
|
|
|
assert!(output.status.success());
|
2021-10-18 21:29:17 +05:30
|
|
|
|
2021-07-20 15:07:55 +05:30
|
|
|
let stdout = std::str::from_utf8(&output.stdout[..])
|
|
|
|
.unwrap()
|
|
|
|
.trim_end_matches(|c| c == '\n' || c == '\r')
|
|
|
|
.to_string();
|
2021-10-11 16:37:26 +05:30
|
|
|
|
2021-07-20 15:07:55 +05:30
|
|
|
Ok(stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the entire output. The command must return zero.
|
2021-10-18 21:29:17 +05:30
|
|
|
pub fn run_ok_raw(command: Command) -> Result<std::process::Output> {
|
|
|
|
eprintln!("run_ok_raw: {}", command);
|
|
|
|
let command = command.to_expr().stdout_capture().stderr_capture();
|
2021-07-20 15:07:55 +05:30
|
|
|
let output = command.run()?;
|
2021-10-18 21:29:17 +05:30
|
|
|
log_output(&output);
|
2021-07-20 15:07:55 +05:30
|
|
|
assert!(output.status.success());
|
|
|
|
Ok(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns stderr, a non zero status must be returned
|
2021-10-18 21:29:17 +05:30
|
|
|
pub fn run_fail(command: Command) -> Result<String> {
|
|
|
|
eprintln!("run_fail: {}", command);
|
|
|
|
let command = command.to_expr().stdout_capture().stderr_capture();
|
2021-07-20 15:07:55 +05:30
|
|
|
let output = command.unchecked().run()?;
|
2021-10-18 21:29:17 +05:30
|
|
|
log_output(&output);
|
2021-07-20 15:07:55 +05:30
|
|
|
assert!(!output.status.success());
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr[..]).unwrap().to_string();
|
|
|
|
Ok(stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the entire output, a non zero status must be returned
|
2021-10-18 21:29:17 +05:30
|
|
|
pub fn run_fail_raw(command: Command) -> Result<std::process::Output> {
|
|
|
|
eprintln!("run_fail_raw: {}", command);
|
|
|
|
let command = command.to_expr().stdout_capture().stderr_capture();
|
2021-07-20 15:07:55 +05:30
|
|
|
let output = command.unchecked().run()?;
|
2021-10-18 21:29:17 +05:30
|
|
|
log_output(&output);
|
2021-07-20 15:07:55 +05:30
|
|
|
assert!(!output.status.success());
|
|
|
|
Ok(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|