rename to action

This commit is contained in:
Michael 2018-08-16 17:38:11 +00:00
parent a71eaff19e
commit 92e5168e3e

View file

@ -11,10 +11,10 @@ use PROGRAMS;
pub struct Handler { pub struct Handler {
config: TomlValue, config: TomlValue,
program: Program, action: Action,
} }
pub enum Program { pub enum Action {
Command(String), Command(String),
Exec(PathBuf), Exec(PathBuf),
} }
@ -26,14 +26,14 @@ impl Handler {
.ok_or(err_msg("No 'type' found."))? .ok_or(err_msg("No 'type' found."))?
.as_str() .as_str()
.ok_or(err_msg("'type' is not a string."))?; .ok_or(err_msg("'type' is not a string."))?;
let program = match handler { let action = match handler {
"script" => { "command" => {
let command = config let command = config
.get("command") .get("command")
.ok_or(err_msg("No 'command' found"))? .ok_or(err_msg("No 'command' found"))?
.as_str() .as_str()
.ok_or(err_msg("'command' is not a string."))?; .ok_or(err_msg("'command' is not a string."))?;
Program::Command(command.to_owned()) Action::Command(command.to_owned())
} }
handler => { handler => {
let programs = PROGRAMS.lock().unwrap(); let programs = PROGRAMS.lock().unwrap();
@ -41,11 +41,11 @@ impl Handler {
.get(handler) .get(handler)
.ok_or(err_msg(format!("'{}' is not a valid executable", handler))) .ok_or(err_msg(format!("'{}' is not a valid executable", handler)))
.map(|value| value.clone())?; .map(|value| value.clone())?;
Program::Exec(program) Action::Exec(program)
} }
}; };
let config = config.clone(); let config = config.clone();
Ok(Handler { config, program }) Ok(Handler { config, action })
} }
pub fn run(&self, input: JsonValue) -> Result<JsonValue, Error> { pub fn run(&self, input: JsonValue) -> Result<JsonValue, Error> {
let config = { let config = {
@ -57,10 +57,14 @@ impl Handler {
String::from_utf8(buf).unwrap() String::from_utf8(buf).unwrap()
}; };
match &self.program { match &self.action {
Program::Command(ref cmd) => { Action::Command(ref cmd) => {
// TODO: allow some kind of simple variable replacement // TODO: allow some kind of simple variable replacement
let output = Command::new("/bin/bash").arg("-c").arg(cmd).output()?; let output = Command::new("/bin/bash")
.env("DIP_ROOT", "lol")
.arg("-c")
.arg(cmd)
.output()?;
if !output.status.success() { if !output.status.success() {
// TODO: get rid of unwraps // TODO: get rid of unwraps
return Err(err_msg(format!( return Err(err_msg(format!(
@ -72,7 +76,7 @@ impl Handler {
))); )));
} }
} }
Program::Exec(ref path) => { Action::Exec(ref path) => {
let mut child = Command::new(&path) let mut child = Command::new(&path)
.env("DIP_ROOT", "") .env("DIP_ROOT", "")
.arg("--config") .arg("--config")