diff --git a/01_phase_rust_src/sv/src/executor.rs b/01_phase_rust_src/sv/src/executor.rs index 7c738f8..91ad357 100644 --- a/01_phase_rust_src/sv/src/executor.rs +++ b/01_phase_rust_src/sv/src/executor.rs @@ -4,8 +4,26 @@ use std::fs; use std::path::Path; use std::env::set_current_dir; -pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, services: Vec){ - // TODO: Return the exec code. +fn sent_signal(exit_code: i32, svwait_var: i32, verbose: i8, command: &str, sv: String) -> i32 { + + // Check if supervise/ok exist. + let supervise_ok = Path::new("supervise/ok"); + if ! supervise_ok.exists() { + println!("warning: {}: unable to open supervise/ok: file does not exist", sv.clone()); + if exit_code < 99 { + return exit_code + 1; + } else { + return exit_code; + } + } + // TODO + // sent the proper signal to supervise/control + // + // if verbose = 1, wait for the service to reach the proper state or timeout. + return exit_code; +} + +pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, services: Vec) -> i32{ println!("execute in exec"); dbg!(svdir_var.clone()); dbg!(svwait_var); @@ -35,9 +53,8 @@ pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, s let path_to_sv = Path::new(&path); if set_current_dir(path_to_sv).is_ok() { - println!("Set path okay."); - // TODO: make a function which do what's next. - // It will be called, sent_signal, will return 0 if no error, and 1 if an error. + // Call the change in the service. + exit_code = sent_signal(exit_code, svwait_var, verbose, command, sv); } else { println!("fail: {}: unable to change to service directory: file does not exist", sv); if exit_code < 99 { @@ -45,15 +62,8 @@ pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, s } } - // TODO - // 2. Check if supervise/ok exist - // - // if not, error => increase by one the exit code and print a beatifull message in Err - // standard. - // - // if yes, sent the proper signal to supervise/control - // - // if verbose = 1, wait for the service to reach the proper state or timeout. } + return exit_code + } diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs index b1498d0..1cad543 100644 --- a/01_phase_rust_src/sv/src/main.rs +++ b/01_phase_rust_src/sv/src/main.rs @@ -26,10 +26,6 @@ fn main() { let mut verbose: i8 = 0; - //dbg!(svdir_var); - //dbg!(svwait_var); - - // run the parser let args: Vec = env::args().collect(); @@ -46,7 +42,6 @@ fn main() { if i == 0 { if val == "-v" { verbose = 1; - //dbg!(verbose); } else if val == "-w" { i = 3; @@ -70,25 +65,25 @@ fn main() { } } else if i == 1 { + // Set the command as first arguments. command = val; - //dbg!(command); i += 1; } else if i == 2 { - // case services to control. - //dbg!(val); + // add services to control. services.push(val.to_string()); } else if i == 3 { + // change svwait after flag -w svwait_var = match val.parse::() { Ok(val) => val, Err(..) => svwait_var, }; - //dbg!(svwait_var); i = 0; } } - - executor::execute(svdir_var, svwait_var, verbose, command, services); + + // Exit after all services are correctly executed. + exit(executor::execute(svdir_var, svwait_var, verbose, command, services)); }