1919
2020extern crate core;
2121
22- use std:: path:: PathBuf ;
23- use anyhow:: Result ;
22+ use anyhow:: { Context , Result } ;
2423use intercept:: reporter:: { Reporter , TcpReporter } ;
24+ use intercept:: KEY_DESTINATION ;
25+ use std:: path:: { Path , PathBuf } ;
2526
2627fn main ( ) -> Result < ( ) > {
28+ env_logger:: init ( ) ;
2729 // Find out what is the executable name the execution was started with
2830 let executable = std:: env:: args ( ) . next ( ) . unwrap ( ) ;
31+ log:: info!( "Executable as called: {:?}" , executable) ;
2932 // Read the PATH variable and find the next executable with the same name
30- let real_executable = std:: env:: var ( "PATH" ) ?
31- . split ( ':' )
32- . map ( |dir| std:: path:: Path :: new ( dir) . join ( & executable) )
33- . filter ( |path| path. exists ( ) )
34- . nth ( 1 )
35- . ok_or_else ( || anyhow:: anyhow!( "Cannot find the real executable" ) ) ?;
36- // TODO: ^ This is a very naive way to find the real executable.
37- // Make sure we don't call ourselves.
33+ let real_executable = next_in_path ( & executable) ?;
34+ log:: info!( "Executable to call: {:?}" , real_executable) ;
3835
3936 // Report the execution with the real executable
40- report_execution ( & real_executable) ;
37+ match into_execution ( & real_executable) . and_then ( report) {
38+ Ok ( _) => log:: info!( "Execution reported" ) ,
39+ Err ( e) => log:: error!( "Execution reporting failed: {}" , e) ,
40+ }
4141
4242 // Execute the real executable with the same arguments
4343 let status = std:: process:: Command :: new ( real_executable)
@@ -47,29 +47,44 @@ fn main() -> Result<()> {
4747 std:: process:: exit ( status. code ( ) . unwrap_or ( 1 ) ) ;
4848}
4949
50- // TODO: Current error handling is very basic, it just panics on any error.
51- // More sophisticated error handling can be: logging the error and return.
52- fn report_execution ( path_buf : & PathBuf ) {
53- // Get the reporter address from the environment
54- let reporter_address = std:: env:: var ( INTERCEPT_REPORTER_ADDRESS )
55- . expect ( format ! ( "${} is not set" , INTERCEPT_REPORTER_ADDRESS ) . as_str ( ) ) ;
56- // Create a new reporter
57- let reporter = TcpReporter :: new ( reporter_address)
58- . expect ( "Cannot create reporter" ) ;
50+ // TODO: This is a very naive way to find the real executable.
51+ // Make sure we don't call ourselves.
52+ fn next_in_path ( executable : & String ) -> Result < PathBuf > {
53+ let real_executable = std:: env:: var ( "PATH" ) ?
54+ . split ( ':' )
55+ . map ( |dir| Path :: new ( dir) . join ( & executable) )
56+ . filter ( |path| path. exists ( ) )
57+ . nth ( 1 )
58+ . ok_or_else ( || anyhow:: anyhow!( "Cannot find the real executable" ) ) ?;
59+ Ok ( real_executable)
60+ }
5961
60- // Report the execution
61- let execution = intercept:: Event {
62+ fn report ( execution : intercept :: Execution ) -> Result < ( ) > {
63+ let event = intercept:: Event {
6264 pid : intercept:: ProcessId ( std:: process:: id ( ) as u32 ) ,
63- execution : intercept:: Execution {
64- executable : path_buf. clone ( ) ,
65- arguments : std:: env:: args ( ) . collect ( ) ,
66- working_dir : std:: env:: current_dir ( ) . expect ( "Cannot get current directory" ) ,
67- environment : std:: env:: vars ( ) . collect ( ) ,
68- } ,
65+ execution,
6966 } ;
70- reporter. report ( execution)
71- . expect ( "Cannot report execution" ) ;
67+
68+ // Get the reporter address from the environment
69+ std:: env:: var ( KEY_DESTINATION )
70+ . with_context ( || format ! ( "${} is missing from the environment" , KEY_DESTINATION ) )
71+ // Create a new reporter
72+ . and_then ( |reporter_address| TcpReporter :: new ( reporter_address) )
73+ . with_context ( || "Cannot create TCP execution reporter" )
74+ // Report the execution
75+ . and_then ( |reporter| reporter. report ( event) )
76+ . with_context ( || "Sending execution failed" ) ?;
77+
78+ Ok ( ( ) )
7279}
7380
74- // declare a const string for the INTERCEPT_REPORTER_ADDRESS environment name
75- const INTERCEPT_REPORTER_ADDRESS : & str = "INTERCEPT_REPORTER_ADDRESS" ;
81+ fn into_execution ( path_buf : & Path ) -> Result < intercept:: Execution > {
82+ std:: env:: current_dir ( )
83+ . with_context ( || "Cannot get current directory" )
84+ . map ( |working_dir| intercept:: Execution {
85+ executable : path_buf. to_path_buf ( ) ,
86+ arguments : std:: env:: args ( ) . collect ( ) ,
87+ working_dir,
88+ environment : std:: env:: vars ( ) . collect ( ) ,
89+ } )
90+ }
0 commit comments