move hook iter impl to hook

This commit is contained in:
Michael 2018-08-16 22:39:58 +00:00
parent 2a25ad669b
commit d009f0b81a
4 changed files with 36 additions and 28 deletions

View file

@ -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"
```

View file

@ -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!(

View file

@ -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<Handler> {
self.handlers.iter()
}
pub fn handle(&self, _payload: &Request<Body>) -> Result<Response<Body>, 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))))
}
}

View file

@ -60,26 +60,7 @@ pub fn dip_service(req: Request<Body>) -> Box<Future<Item = Response<Body>, 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()