2018-12-31 13:26:17 +02:00
|
|
|
//! Utilities for command execution
|
2018-12-11 16:43:26 +02:00
|
|
|
use super::error::{Error, ErrorKind};
|
2018-08-26 16:12:59 +03:00
|
|
|
use super::utils::Check;
|
2018-12-11 16:43:26 +02:00
|
|
|
use failure::ResultExt;
|
2018-08-26 16:12:59 +03:00
|
|
|
use std::ffi::{OsStr, OsString};
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::process::{Child, Command, ExitStatus};
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
/// An enum telling whether Topgrade should perform dry runs or actually perform the steps.
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
|
pub enum RunType {
|
|
|
|
|
/// Executing commands will just print the command with its argument.
|
|
|
|
|
Dry,
|
|
|
|
|
|
|
|
|
|
/// Executing commands will perform actual execution.
|
|
|
|
|
Wet,
|
2018-08-26 16:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
impl RunType {
|
|
|
|
|
/// Create a new instance from a boolean telling whether to dry run.
|
2018-12-31 14:05:15 +02:00
|
|
|
pub fn new(dry_run: bool) -> Self {
|
2018-12-31 13:26:17 +02:00
|
|
|
if dry_run {
|
|
|
|
|
RunType::Dry
|
|
|
|
|
} else {
|
|
|
|
|
RunType::Wet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create an instance of `Executor` that should run `program`.
|
|
|
|
|
pub fn execute<S: AsRef<OsStr>>(self, program: S) -> Executor {
|
|
|
|
|
match self {
|
|
|
|
|
RunType::Dry => Executor::Dry(DryCommand {
|
2018-08-26 16:12:59 +03:00
|
|
|
program: program.as_ref().into(),
|
|
|
|
|
..Default::default()
|
2018-12-31 13:26:17 +02:00
|
|
|
}),
|
|
|
|
|
RunType::Wet => Executor::Wet(Command::new(program)),
|
2018-08-26 16:12:59 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
#[cfg(feature = "self-update")]
|
|
|
|
|
/// Tells whether we're performing a dry run.
|
|
|
|
|
pub fn dry(self) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
RunType::Dry => true,
|
|
|
|
|
RunType::Wet => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An enum providing a similar interface to `std::process::Command`.
|
|
|
|
|
/// If the enum is set to `Wet`, execution will be performed with `std::process::Command`.
|
|
|
|
|
/// If the enum is set to `Dry`, execution will just print the command with its arguments.
|
|
|
|
|
pub enum Executor {
|
|
|
|
|
Wet(Command),
|
|
|
|
|
Dry(DryCommand),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Executor {
|
|
|
|
|
/// See `std::process::Command::arg`
|
2018-08-26 16:12:59 +03:00
|
|
|
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Executor {
|
|
|
|
|
match self {
|
|
|
|
|
Executor::Wet(c) => {
|
|
|
|
|
c.arg(arg);
|
|
|
|
|
}
|
|
|
|
|
Executor::Dry(c) => {
|
|
|
|
|
c.args.push(arg.as_ref().into());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
/// See `std::process::Command::args`
|
2018-08-26 16:12:59 +03:00
|
|
|
pub fn args<I, S>(&mut self, args: I) -> &mut Executor
|
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = S>,
|
|
|
|
|
S: AsRef<OsStr>,
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Executor::Wet(c) => {
|
|
|
|
|
c.args(args);
|
|
|
|
|
}
|
|
|
|
|
Executor::Dry(c) => {
|
|
|
|
|
c.args.extend(args.into_iter().map(|arg| arg.as_ref().into()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
/// See `std::process::Command::current_dir`
|
2018-08-26 16:12:59 +03:00
|
|
|
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Executor {
|
|
|
|
|
match self {
|
|
|
|
|
Executor::Wet(c) => {
|
|
|
|
|
c.current_dir(dir);
|
|
|
|
|
}
|
|
|
|
|
Executor::Dry(c) => c.directory = Some(dir.as_ref().into()),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
/// See `std::process::Command::spawn`
|
2018-12-11 16:43:26 +02:00
|
|
|
pub fn spawn(&mut self) -> Result<ExecutorChild, Error> {
|
|
|
|
|
let result = match self {
|
|
|
|
|
Executor::Wet(c) => c.spawn().context(ErrorKind::ProcessExecution).map(ExecutorChild::Wet)?,
|
2018-08-26 16:12:59 +03:00
|
|
|
Executor::Dry(c) => {
|
|
|
|
|
print!(
|
|
|
|
|
"Dry running: {} {}",
|
|
|
|
|
c.program.to_string_lossy(),
|
|
|
|
|
c.args
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|a| String::from(a.to_string_lossy()))
|
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
|
.join(" ")
|
|
|
|
|
);
|
|
|
|
|
match &c.directory {
|
|
|
|
|
Some(dir) => println!(" in {}", dir.to_string_lossy()),
|
|
|
|
|
None => println!(),
|
|
|
|
|
};
|
2018-12-11 16:43:26 +02:00
|
|
|
ExecutorChild::Dry
|
2018-08-26 16:12:59 +03:00
|
|
|
}
|
2018-12-11 16:43:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
2018-08-26 16:12:59 +03:00
|
|
|
}
|
2018-12-31 22:00:34 +02:00
|
|
|
|
|
|
|
|
/// A convinence method for `spawn().wait().check()`.
|
|
|
|
|
/// Returns an error if something went wrong during the execution or if the
|
|
|
|
|
/// process exited with failure.
|
|
|
|
|
pub fn check_run(&mut self) -> Result<(), Error> {
|
|
|
|
|
self.spawn()?.wait()?.check()
|
|
|
|
|
}
|
2018-08-26 16:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
/// A struct represending a command. Trying to execute it will just print its arguments.
|
2018-08-26 16:12:59 +03:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct DryCommand {
|
|
|
|
|
program: OsString,
|
|
|
|
|
args: Vec<OsString>,
|
|
|
|
|
directory: Option<OsString>,
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
/// The Result of spawn. Contains an actual `std::process::Child` if executed by a wet command.
|
2018-08-26 16:12:59 +03:00
|
|
|
pub enum ExecutorChild {
|
|
|
|
|
Wet(Child),
|
|
|
|
|
Dry,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ExecutorChild {
|
2018-12-31 13:26:17 +02:00
|
|
|
/// See `std::process::Child::wait`
|
2018-12-11 16:43:26 +02:00
|
|
|
pub fn wait(&mut self) -> Result<ExecutorExitStatus, Error> {
|
|
|
|
|
let result = match self {
|
|
|
|
|
ExecutorChild::Wet(c) => c
|
|
|
|
|
.wait()
|
|
|
|
|
.context(ErrorKind::ProcessExecution)
|
|
|
|
|
.map(ExecutorExitStatus::Wet)?,
|
|
|
|
|
ExecutorChild::Dry => ExecutorExitStatus::Dry,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
2018-08-26 16:12:59 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
/// The Result of wait. Contains an actual `std::process::ExitStatus` if executed by a wet command.
|
2018-08-26 16:12:59 +03:00
|
|
|
pub enum ExecutorExitStatus {
|
|
|
|
|
Wet(ExitStatus),
|
|
|
|
|
Dry,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Check for ExecutorExitStatus {
|
2018-12-11 16:43:26 +02:00
|
|
|
fn check(self) -> Result<(), Error> {
|
2018-08-26 16:12:59 +03:00
|
|
|
match self {
|
|
|
|
|
ExecutorExitStatus::Wet(e) => e.check(),
|
|
|
|
|
ExecutorExitStatus::Dry => Ok(()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|