From e57f8f1796e3846a52df515ba989957819775894 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 22 Aug 2018 21:45:11 -0500 Subject: [PATCH] works i guess --- examples/github.rs | 8 ++++++-- src/handler.rs | 24 +++++++++++++++--------- src/hook.rs | 43 ++++--------------------------------------- 3 files changed, 25 insertions(+), 50 deletions(-) diff --git a/examples/github.rs b/examples/github.rs index 6e4452d..5656e0e 100644 --- a/examples/github.rs +++ b/examples/github.rs @@ -70,7 +70,6 @@ fn main() { .expect("Could not read from stdin"); let payload: Payload = serde_json::from_str(&payload) .expect(&format!("Could not parse stdin into json: '{}'", payload)); - println!("Read body: '{}'", payload.body); if !config.disable_hmac_verify { let secret = GenericArray::from_iter(config.secret.bytes()); @@ -92,7 +91,12 @@ fn main() { let left = SecStr::from(format!("sha1={}", signature)); let right = SecStr::from(auth.bytes().collect::>()); - assert!(left == right, "HMAC signature didn't match",); + assert!( + left == right, + "HMAC signature didn't match: {} vs. {}", + signature, + auth + ); } let payload: GithubPayload = diff --git a/src/handler.rs b/src/handler.rs index bfc707f..d303f08 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -3,8 +3,7 @@ use std::process::{Command, Stdio}; use failure::{err_msg, Error}; use futures::{ - future::{self, Either, FutureResult}, - sink::Sink, + future::{self, Either}, Future, }; use serde::Serialize; @@ -69,7 +68,6 @@ impl Handler { temp_path: PathBuf, input: JsonValue, ) -> impl Future { - println!("Running: {:?} :: {:?}", config, action); let config = { let mut buf: Vec = Vec::new(); { @@ -125,16 +123,24 @@ impl Handler { let input = format!("{}", input); let result = write_all(stdin, input) .and_then(|_| child.wait_with_output()) - .map(|output| { + .map_err(|err| err_msg(format!("error: {}", err))) + .and_then(|output| { let stdout = String::from_utf8(output.stdout).unwrap_or_else(|_| String::new()); let stderr = String::from_utf8(output.stderr).unwrap_or_else(|_| String::new()); - json!({ - "stdout": stdout, - "stderr": stderr, - }) - }).map_err(|err| err_msg(format!("error: {}", err))); + if output.status.success() { + Either::A(future::ok(json!({ + "stdout": stdout, + "stderr": stderr, + }))) + } else { + Either::B(future::err(err_msg(format!( + "Failed, stdout: '{}', stderr: '{}'", + stdout, stderr + )))) + } + }); Box::new(result) } diff --git a/src/hook.rs b/src/hook.rs index 26b2b84..068454e 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -58,47 +58,12 @@ impl Hook { .collect::>(); let st = stream::iter_ok::<_, Error>(handlers.into_iter()) .fold((temp_path, req), |(path, prev), (config, action)| { - println!("executing in {:?}", &path); Handler::run(config, action, path, prev) }).map(|_| ()) - .map_err(|_: Error| ()); + .map_err(|err: Error| { + println!("Error from stream: {}", err); + }); tokio::executor::spawn(st); - // let it = stream::iter_ok::<_, Error>(handlers.iter()); - // let s = it.fold((temp_path, req), move |(path, prev), handler| { - // let result = handler.run(path.clone(), prev.clone()); - // result - // }).map(|_|()).map_err(|_| ()); - // tokio::executor::spawn(s); Ok("success".to_owned()) - // let s = stream::iter_ok(self.handlers.iter()).fold((temp_path, req), move |prev, handler| { - // let (path, prev) = prev; - // handler.run(path, prev) - // }); - // .fold(future::ok(req), |prev, handler| { - // prev.and_then(|val| handler.run(temp_path, val)) - // }); - // let s = s.map(|_| ()).map_err(|_: Error| ()); - // tokio::executor::spawn(s); - // Ok("success".to_owned()) - /* - Ok(self.iter() - .fold(Ok(req), |prev, handler| { - prev.and_then(|val| { - println!("Running {}...", handler.config()); - let result = handler.run(&temp_path, val); - 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)))) - */ } + } }