diff --git a/README.md b/README.md index d055b33..0a502c9 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,9 @@ Express your webhooks in terms of composable blocks such as: [[handlers]] type = "github" secret = "hunter2" -outdir = "/home/michael/dip" [[handlers]] -type = "script" +type = "command" command = "cargo build" ``` diff --git a/src/handler.rs b/src/handler.rs index f63b040..b96035a 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -69,13 +69,17 @@ impl Handler { let output = match &self.action { Action::Command(ref cmd) => { // TODO: allow some kind of simple variable replacement - let output = Command::new("/bin/bash") + let child = Command::new("/bin/bash") .current_dir(&temp_path) .env("DIP_ROOT", "lol") .env("DIP_WORKDIR", temp_path) .arg("-c") .arg(cmd) - .output()?; + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + let output = child.wait_with_output()?; if !output.status.success() { // TODO: get rid of unwraps return Err(err_msg(format!( diff --git a/src/hook.rs b/src/hook.rs index f1676a8..15673fb 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -1,10 +1,11 @@ use std::fs::File; use std::io::Read; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::slice::Iter; use failure::{err_msg, Error}; -use hyper::{Body, Request, Response}; +use hyper::StatusCode; +use serde_json::Value as JsonValue; use toml::Value; use Handler; @@ -51,7 +52,30 @@ impl Hook { pub fn iter(&self) -> Iter { self.handlers.iter() } - pub fn handle(&self, _payload: &Request) -> Result, Error> { - Ok(Response::new(Body::from("lol"))) + pub fn handle( + &self, + req: JsonValue, + temp_path: &PathBuf, + ) -> Result<(StatusCode, String), Error> { + Ok(self.iter() + .fold(Ok(req), |prev, handler| { + prev.and_then(|val| { + println!("Running {}...", handler.config()); + let result = handler.run(&temp_path, val); + println!("{:?}", result); + result + }) + }) + .map(|res| { + ( + StatusCode::ACCEPTED, + format!( + "stdout:\n{}\n\nstderr:\n{}", + res.get("stdout").and_then(|v| v.as_str()).unwrap_or(""), + res.get("stderr").and_then(|v| v.as_str()).unwrap_or(""), + ), + ) + }) + .unwrap_or_else(|err| (StatusCode::BAD_REQUEST, format!("Error: {:?}", err)))) } } diff --git a/src/service.rs b/src/service.rs index 70891f6..8afaa00 100644 --- a/src/service.rs +++ b/src/service.rs @@ -60,26 +60,7 @@ pub fn dip_service(req: Request) -> Box, Erro assert!(temp_path.exists()); let hook = hooks.get(&name).unwrap(); - let (code, msg) = hook.iter() - .fold(Ok(req_obj), |prev, handler| { - prev.and_then(|val| { - println!("Running {}...", handler.config()); - let result = handler.run(&temp_path, val); - println!("{:?}", result); - result - }) - }) - .map(|res| { - ( - StatusCode::ACCEPTED, - format!( - "stdout:\n{}\n\nstderr:\n{}", - res.get("stdout").and_then(|v| v.as_str()).unwrap_or(""), - res.get("stderr").and_then(|v| v.as_str()).unwrap_or(""), - ), - ) - }) - .unwrap_or_else(|err| (StatusCode::BAD_REQUEST, format!("Error: {:?}", err))); + let (code, msg) = hook.handle(req_obj, &temp_path).unwrap(); temp_dir.release(); Response::builder()