move hook iter impl to hook
This commit is contained in:
parent
2a25ad669b
commit
d009f0b81a
4 changed files with 36 additions and 28 deletions
|
@ -12,10 +12,9 @@ Express your webhooks in terms of composable blocks such as:
|
||||||
[[handlers]]
|
[[handlers]]
|
||||||
type = "github"
|
type = "github"
|
||||||
secret = "hunter2"
|
secret = "hunter2"
|
||||||
outdir = "/home/michael/dip"
|
|
||||||
|
|
||||||
[[handlers]]
|
[[handlers]]
|
||||||
type = "script"
|
type = "command"
|
||||||
command = "cargo build"
|
command = "cargo build"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -69,13 +69,17 @@ impl Handler {
|
||||||
let output = match &self.action {
|
let output = match &self.action {
|
||||||
Action::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")
|
let child = Command::new("/bin/bash")
|
||||||
.current_dir(&temp_path)
|
.current_dir(&temp_path)
|
||||||
.env("DIP_ROOT", "lol")
|
.env("DIP_ROOT", "lol")
|
||||||
.env("DIP_WORKDIR", temp_path)
|
.env("DIP_WORKDIR", temp_path)
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(cmd)
|
.arg(cmd)
|
||||||
.output()?;
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped())
|
||||||
|
.spawn()?;
|
||||||
|
let output = child.wait_with_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!(
|
||||||
|
|
32
src/hook.rs
32
src/hook.rs
|
@ -1,10 +1,11 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
use std::slice::Iter;
|
use std::slice::Iter;
|
||||||
|
|
||||||
use failure::{err_msg, Error};
|
use failure::{err_msg, Error};
|
||||||
use hyper::{Body, Request, Response};
|
use hyper::StatusCode;
|
||||||
|
use serde_json::Value as JsonValue;
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
use Handler;
|
use Handler;
|
||||||
|
@ -51,7 +52,30 @@ impl Hook {
|
||||||
pub fn iter(&self) -> Iter<Handler> {
|
pub fn iter(&self) -> Iter<Handler> {
|
||||||
self.handlers.iter()
|
self.handlers.iter()
|
||||||
}
|
}
|
||||||
pub fn handle(&self, _payload: &Request<Body>) -> Result<Response<Body>, Error> {
|
pub fn handle(
|
||||||
Ok(Response::new(Body::from("lol")))
|
&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))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,26 +60,7 @@ pub fn dip_service(req: Request<Body>) -> Box<Future<Item = Response<Body>, Erro
|
||||||
assert!(temp_path.exists());
|
assert!(temp_path.exists());
|
||||||
|
|
||||||
let hook = hooks.get(&name).unwrap();
|
let hook = hooks.get(&name).unwrap();
|
||||||
let (code, msg) = hook.iter()
|
let (code, msg) = hook.handle(req_obj, &temp_path).unwrap();
|
||||||
.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)));
|
|
||||||
|
|
||||||
temp_dir.release();
|
temp_dir.release();
|
||||||
Response::builder()
|
Response::builder()
|
||||||
|
|
Loading…
Reference in a new issue