diff --git a/examples/github.rs b/examples/github.rs index 0b933f0..6e4452d 100644 --- a/examples/github.rs +++ b/examples/github.rs @@ -17,7 +17,7 @@ use std::iter::FromIterator; use std::path::PathBuf; use std::process::Command; -use failure::{err_msg, Error}; +use failure::err_msg; use generic_array::GenericArray; use hmac::{Hmac, Mac}; use secstr::*; @@ -63,7 +63,6 @@ fn default_path() -> PathBuf { fn main() { let args = Opt::from_args(); let config: Config = serde_json::from_str(&args.config).expect("Could not parse config."); - println!("{:?}", config); let mut payload = String::new(); io::stdin() @@ -71,6 +70,7 @@ 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()); diff --git a/src/handler.rs b/src/handler.rs index 76b4ce8..2e9051c 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,14 +1,15 @@ -use std::io::Write; use std::path::PathBuf; -use std::process::{Command, Output, Stdio}; +use std::process::{Command, Stdio}; use failure::{err_msg, Error}; use futures::{ - future::{self, FutureResult}, + future::{self, Either, FutureResult}, + sink::Sink, Future, }; use serde::Serialize; use serde_json::{Serializer as JsonSerializer, Value as JsonValue}; +use tokio::io::write_all; use tokio_process::CommandExt; use toml::Value as TomlValue; @@ -113,39 +114,72 @@ impl Handler { .env("DIP_WORKDIR", &temp_path) .arg("--config") .arg(config) - .stdin(Stdio::piped()) + .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn_async() .expect("could not spawn child"); - match child.stdin() { - Some(ref mut stdin) => { - println!("writing input: '{}'", input); - write!(stdin, "{}", input); - } - None => (), - }; - let result = child - .wait_with_output() - .and_then(|output| { - if output.status.success() { - future::ok(output) - } else { - // TODO: change this - future::ok(output) - } - }).map(|output| { + + let stdin = child.stdin().take().unwrap(); + + let input = format!("{}", input); + let result = write_all(stdin, input) + .and_then(|_| child.wait_with_output()) + .map(|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()); - println!("stdout: {}, stderr: {}", stdout, stderr); json!({ - "stdout": stdout, - "stderr": stderr, - }) - }).map_err(|err| err_msg(format!("could not get output: {}", err))); + "stdout": stdout, + "stderr": stderr, + }) + }).map_err(|err| err_msg(format!("error: {}", err))); + + // let _result: Either<_, FutureResult<(), Error>> = { + // match child.stdin() { + // Some(ref mut stdin) => Either::A(write_all(stdin, input.as_bytes())), + // None => Either::B(future::err(err_msg("rip"))), + // } + // }; Box::new(result) + + // let input_s = format!("{}", input); + // let result: Box + Send> = { + // let rf = child.clone().lock().unwrap(); + // match rf.stdin() { + // Some(ref mut stdin) => Box::new( + // write_all(stdin, input_s.as_bytes()) + // .map(|_| ()) + // .map_err(|err| err_msg(format!("error: {}", err))), + // ), + // None => Box::new(future::err(err_msg("Failed to acquire child stdin"))), + // } + // }; + // { + // let rf = child.clone().lock().unwrap(); + // let result = rf + // .wait_with_output() + // .and_then(|output| { + // if output.status.success() { + // future::ok(output) + // } else { + // // TODO: change this + // future::ok(output) + // } + // }).map(|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()); + // println!("stdout: {}, stderr: {}", stdout, stderr); + // json!({ + // "stdout": stdout, + // "stderr": stderr, + // }) + // }).map_err(|err| err_msg(format!("could not get output: {}", err))); + // }); + // .and_then(move |output| { // if output.status.success() { // future::ok(output) diff --git a/src/hook.rs b/src/hook.rs index 7cd897a..26b2b84 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -1,17 +1,14 @@ use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; -use std::slice::Iter; -use std::sync::Arc; use failure::{err_msg, Error}; -use futures::{future, stream, Future}; +use futures::{stream, Future}; use serde_json::Value as JsonValue; use tokio::{self, prelude::*}; use toml::Value; use Handler; -use HANDLERS; pub struct Hook { name: String, diff --git a/src/lib.rs b/src/lib.rs index ff1e795..81a1ed4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ //! # Dip -#[macro_use] extern crate failure; extern crate futures; extern crate hyper; @@ -55,8 +54,6 @@ lazy_static! { static ref HOOKS: Arc>> = Arc::new(Mutex::new(HashMap::new())); } -// const NOTFOUND: &str = "

Looks like you took a wrong turn!

There's nothing to see here.

"; - fn watch

(root: P) -> notify::Result<()> where P: AsRef, @@ -72,7 +69,7 @@ where // TODO: don't do this config::load_config(root.as_ref()) } - Err(e) => println!("watch error: {:?}", e), + Err(e) => eprintln!("watch error: {:?}", e), } } } diff --git a/src/service.rs b/src/service.rs index 9ec7803..36fdbc4 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::thread; use futures::{future, Future, Stream}; use hyper::{Body, Error, Request, Response, StatusCode};