@ -1,6 +1,7 @@
use crate ::config ::Hook ;
use crate ::config ::Hook ;
use crate ::errors ::Error ;
use crate ::errors ::Error ;
use handlebars ::Handlebars ;
use handlebars ::Handlebars ;
use log ::debug ;
use serde ::Serialize ;
use serde ::Serialize ;
use std ::fs ::File ;
use std ::fs ::File ;
use std ::io ::prelude ::* ;
use std ::io ::prelude ::* ;
@ -27,6 +28,7 @@ pub fn call_multiple<T: Serialize>(data: &T, hooks: &Vec<Hook>) -> Result<(), Er
}
}
pub fn call < T : Serialize > ( data : & T , hook : & Hook ) -> Result < ( ) , Error > {
pub fn call < T : Serialize > ( data : & T , hook : & Hook ) -> Result < ( ) , Error > {
debug ! ( "Calling hook: {}" , hook . name ) ;
let reg = Handlebars ::new ( ) ;
let reg = Handlebars ::new ( ) ;
let mut v = vec ! [ ] ;
let mut v = vec ! [ ] ;
let args = match & hook . args {
let args = match & hook . args {
@ -39,6 +41,8 @@ pub fn call<T: Serialize>(data: &T, hook: &Hook) -> Result<(), Error> {
}
}
None = > & [ ] ,
None = > & [ ] ,
} ;
} ;
debug ! ( "Hook {}: cmd: {}" , hook . name , hook . cmd ) ;
debug ! ( "Hook {}: args: {:?}" , hook . name , args ) ;
let mut cmd = Command ::new ( & hook . cmd )
let mut cmd = Command ::new ( & hook . cmd )
. args ( args )
. args ( args )
. stdout ( get_hook_output ! ( & hook . stdout , reg , data ) )
. stdout ( get_hook_output ! ( & hook . stdout , reg , data ) )
@ -50,8 +54,15 @@ pub fn call<T: Serialize>(data: &T, hook: &Hook) -> Result<(), Error> {
. spawn ( ) ? ;
. spawn ( ) ? ;
if hook . stdin . is_some ( ) {
if hook . stdin . is_some ( ) {
let data_in = reg . render_template ( & hook . stdin . to_owned ( ) . unwrap ( ) , data ) ? ;
let data_in = reg . render_template ( & hook . stdin . to_owned ( ) . unwrap ( ) , data ) ? ;
debug ! ( "Hook {}: stdin: {}" , hook . name , data_in ) ;
let stdin = cmd . stdin . as_mut ( ) . ok_or ( "stdin not found" ) ? ;
let stdin = cmd . stdin . as_mut ( ) . ok_or ( "stdin not found" ) ? ;
stdin . write_all ( data_in . as_bytes ( ) ) ? ;
stdin . write_all ( data_in . as_bytes ( ) ) ? ;
}
}
// TODO: add a timeout
let status = cmd . wait ( ) ? ;
match status . code ( ) {
Some ( code ) = > debug ! ( "Hook {}: exited with code {}" , hook . name , code ) ,
None = > debug ! ( "Hook {}: exited" , hook . name ) ,
} ;
Ok ( ( ) )
Ok ( ( ) )
}
}