basic support for running commands directly
This commit is contained in:
parent
f7d015fab5
commit
87a8486c37
1 changed files with 68 additions and 34 deletions
|
@ -11,7 +11,12 @@ use PROGRAMS;
|
||||||
|
|
||||||
pub struct Handler {
|
pub struct Handler {
|
||||||
config: TomlValue,
|
config: TomlValue,
|
||||||
exec: PathBuf,
|
program: Program,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Program {
|
||||||
|
Command(String),
|
||||||
|
Exec(PathBuf),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Handler {
|
impl Handler {
|
||||||
|
@ -21,15 +26,26 @@ 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 exec = {
|
let program = match handler {
|
||||||
|
"script" => {
|
||||||
|
let command = config
|
||||||
|
.get("command")
|
||||||
|
.ok_or(err_msg("No 'command' found"))?
|
||||||
|
.as_str()
|
||||||
|
.ok_or(err_msg("'command' is not a string."))?;
|
||||||
|
Program::Command(command.to_owned())
|
||||||
|
}
|
||||||
|
handler => {
|
||||||
let programs = PROGRAMS.lock().unwrap();
|
let programs = PROGRAMS.lock().unwrap();
|
||||||
programs
|
let program = programs
|
||||||
.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)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let config = config.clone();
|
let config = config.clone();
|
||||||
Ok(Handler { config, exec })
|
Ok(Handler { config, program })
|
||||||
}
|
}
|
||||||
pub fn run(&self, input: JsonValue) -> Result<JsonValue, Error> {
|
pub fn run(&self, input: JsonValue) -> Result<JsonValue, Error> {
|
||||||
let config = {
|
let config = {
|
||||||
|
@ -41,7 +57,23 @@ impl Handler {
|
||||||
String::from_utf8(buf).unwrap()
|
String::from_utf8(buf).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut child = Command::new(&self.exec)
|
match &self.program {
|
||||||
|
Program::Command(ref cmd) => {
|
||||||
|
// TODO: allow some kind of simple variable replacement
|
||||||
|
let output = Command::new("/bin/bash").arg("-c").arg(cmd).output()?;
|
||||||
|
if !output.status.success() {
|
||||||
|
// TODO: get rid of unwraps
|
||||||
|
return Err(err_msg(format!(
|
||||||
|
"Command '{}' returned with a non-zero status code: {}\nstdout:\n{}\nstderr:\n{}",
|
||||||
|
cmd,
|
||||||
|
output.status,
|
||||||
|
String::from_utf8(output.stdout).unwrap_or_else(|_| String::new()),
|
||||||
|
String::from_utf8(output.stderr).unwrap_or_else(|_| String::new())
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Program::Exec(ref path) => {
|
||||||
|
let mut child = Command::new(&path)
|
||||||
.env("DIP_ROOT", "")
|
.env("DIP_ROOT", "")
|
||||||
.arg("--config")
|
.arg("--config")
|
||||||
.arg(config)
|
.arg(config)
|
||||||
|
@ -62,12 +94,14 @@ impl Handler {
|
||||||
// TODO: get rid of unwraps
|
// TODO: get rid of unwraps
|
||||||
return Err(err_msg(format!(
|
return Err(err_msg(format!(
|
||||||
"'{:?}' returned with a non-zero status code: {}\nstdout:\n{}\nstderr:\n{}",
|
"'{:?}' returned with a non-zero status code: {}\nstdout:\n{}\nstderr:\n{}",
|
||||||
self.exec,
|
path,
|
||||||
output.status,
|
output.status,
|
||||||
String::from_utf8(output.stdout).unwrap_or_else(|_| String::new()),
|
String::from_utf8(output.stdout).unwrap_or_else(|_| String::new()),
|
||||||
String::from_utf8(output.stderr).unwrap_or_else(|_| String::new())
|
String::from_utf8(output.stderr).unwrap_or_else(|_| String::new())
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
Ok(json!({}))
|
Ok(json!({}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue