@@ -5,14 +5,15 @@ pub mod recognition;
55pub mod transformation;
66
77use crate :: input:: EventFileReader ;
8+ use crate :: intercept:: Envelope ;
89use crate :: output:: OutputWriter ;
910use crate :: { args, config} ;
1011use anyhow:: Context ;
12+ use crossbeam_channel:: Receiver ;
1113use intercept:: { InterceptEnvironment , InterceptService } ;
1214use recognition:: Recognition ;
1315use std:: io:: BufWriter ;
1416use std:: process:: ExitCode ;
15- use std:: thread;
1617use transformation:: Transformation ;
1718
1819/// The mode trait is used to run the application in different modes.
@@ -56,43 +57,38 @@ impl Intercept {
5657 config,
5758 }
5859 }
60+
61+ fn write_to_file (
62+ output_file_name : String ,
63+ envelopes : Receiver < Envelope > ,
64+ ) -> anyhow:: Result < ( ) > {
65+ let mut writer = std:: fs:: File :: create ( & output_file_name)
66+ . map ( BufWriter :: new)
67+ . with_context ( || format ! ( "Failed to create output file: {:?}" , & output_file_name) ) ?;
68+ for envelope in envelopes. iter ( ) {
69+ envelope
70+ . write_into ( & mut writer)
71+ . with_context ( || "Failed to write the envelope" ) ?;
72+ }
73+ Ok ( ( ) )
74+ }
5975}
6076
6177impl Mode for Intercept {
6278 fn run ( self ) -> anyhow:: Result < ExitCode > {
63- match & self . config {
64- config:: Intercept :: Wrapper { .. } => {
65- let service = InterceptService :: new ( )
66- . with_context ( || "Failed to create the intercept service" ) ?;
67- let environment = InterceptEnvironment :: new ( & self . config , service. address ( ) )
68- . with_context ( || "Failed to create the intercept environment" ) ?;
69-
70- // start writer thread
71- let writer_thread = thread:: spawn ( move || {
72- let file = std:: fs:: File :: create ( & self . output . file_name ) . expect (
73- format ! ( "Failed to create output file: {:?}" , self . output. file_name)
74- . as_str ( ) ,
75- ) ;
76- let mut writer = BufWriter :: new ( file) ;
77- for envelope in service. receiver ( ) . iter ( ) {
78- envelope
79- . write_into ( & mut writer)
80- . expect ( "Failed to write the envelope" ) ;
81- }
82- } ) ;
83-
84- let status = environment. execute_build_command ( self . command ) ;
85-
86- writer_thread
87- . join ( )
88- . expect ( "Failed to join the writer thread" ) ;
89-
90- status
91- }
92- config:: Intercept :: Preload { .. } => {
93- todo ! ( )
94- }
95- }
79+ let output_file_name = self . output . file_name . clone ( ) ;
80+ let service = InterceptService :: new ( move |envelopes| {
81+ Self :: write_to_file ( output_file_name, envelopes)
82+ } )
83+ . with_context ( || "Failed to create the intercept service" ) ?;
84+ let environment = InterceptEnvironment :: new ( & self . config , service. address ( ) )
85+ . with_context ( || "Failed to create the intercept environment" ) ?;
86+
87+ let status = environment
88+ . execute_build_command ( self . command )
89+ . with_context ( || "Failed to execute the build command" ) ?;
90+
91+ Ok ( status)
9692 }
9793}
9894
0 commit comments