extern crate std;
extern crate getopts;
use compiler::parser;
use subprocess;
use cmd;
#[derive(Clone)]
pub struct Hook {
name: String,
exec: String,
cwd: String,
args: Option<Vec<String>>,
}
impl Hook {
pub fn new(name: String, exec: String, cwd: String, args: Option<Vec<String>>) -> Hook {
Hook {
name: name,
exec: exec,
cwd: cwd,
args: args,
}
}
pub fn name_get(&self) -> &String {
&self.name
}
pub fn call(&self) -> Result<(), subprocess::Error> {
let mut cmd = std::process::Command::new(self.exec.as_str());
cmd.stdin(std::process::Stdio::null());
let cwd_path = std::path::Path::new(&self.cwd);
cmd.current_dir(cwd_path);
if let Some(ref args) = self.args {
cmd.args(args);
}
debug!("Running hook {}: {:?}", self.name, cmd);
subprocess::run(&mut cmd)?;
Ok(())
}
}
impl cmd::Cmd for Hook {
#[allow(unused)]
fn help(&self, opts: &getopts::Options) {
println!("User hook '{}' executing the command '{}'",
self.name, self.exec);
}
#[allow(unused)]
fn getopts_set(&self, opts: &mut getopts::Options) {
}
#[allow(unused)]
fn getopts_matches_use(&mut self, matches: &getopts::Matches) {
}
#[allow(unused)]
fn run(&self, parser_arg: &Option<parser::Parser>) -> Result<(), cmd::Error> {
self.call()?;
Ok(())
}
}