add verbose, svwait and down command

This commit is contained in:
primardj 2024-04-13 23:24:59 +01:00
parent 6d9fb61ef8
commit 94f8aebd3a
3 changed files with 94 additions and 15 deletions

View file

@ -1,41 +1,86 @@
mod misc;
use crate::status_obj; use crate::status_obj;
use std::thread::sleep;
use std::time::Duration;
pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_obj::StatusObj) -> i32 { pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_obj::StatusObj) -> i32 {
// Return 0 in case everything worked fine, return 1 if timeout or error. // Return 0 in case everything worked fine, return 1 if timeout or error.
dbg!(svwait_var); dbg!(svwait_var);
dbg!(verbose); dbg!(verbose);
let mut time_wait = 0;
// execute command. // execute command.
if command == "s" { if command == "s" {
// case command is status // case command is status
let mut status_sv = sv.get_status_string(); return misc::print_status(sv);
let is_log = sv.get_log();
if is_log {
let log_option = sv.get_obj_log();
match log_option {
Some(mut log) => {
status_sv = status_sv + "; " + &log.get_status_string();
},
None => {// TODO: Do nothing for now, eventually, increase by one exit code.
},
};
}
println!("{}", status_sv);
return 0;
} }
if command == "u" { if command == "u" {
// case where command is up // case where command is up
if sv.sent_signal(b"u") { if sv.sent_signal(b"u") {
if verbose == 1 {
loop {
// TODO, Make a method update_sv
let sv_option = status_obj::StatusObj::new(sv.get_path(), sv.get_name());
let mut sv_new = match sv_option {
Some(service) => service,
None => return 1,
};
if sv_new.is_up() == true {
misc::print_status(sv_new);
return 0
}
if time_wait >= svwait_var {
println!("fail: {}: timeout", sv_new.get_name());
return 1
}
time_wait += 1;
sleep(Duration::from_secs(1));
}
} else {
return 0; return 0;
}
} else { } else {
return 1; return 1;
} }
} }
if command == "d" {
// case where command is up
if sv.sent_signal(b"d") {
if verbose == 1 {
loop {
// TODO: Use the update_service method instead of creating another sv.
let sv_option = status_obj::StatusObj::new(sv.get_path(), sv.get_name());
let mut sv_new = match sv_option {
Some(service) => service,
None => return 1,
};
if sv_new.is_up() == false {
misc::print_status(sv_new);
return 0
}
if time_wait >= svwait_var {
println!("fail: {}: timeout", sv_new.get_name());
return 1
}
time_wait += 1;
sleep(Duration::from_secs(1));
}
} else {
return 0;
}
} else {
return 1;
}
}
// TODO // TODO

View file

@ -0,0 +1,21 @@
use crate::status_obj;
pub fn print_status(mut sv: status_obj::StatusObj) -> i32 {
// The status code is the one who will be
let mut status_sv = sv.get_status_string();
let is_log = sv.get_log();
if is_log {
let log_option = sv.get_obj_log();
match log_option {
Some(mut log) => {
status_sv = status_sv + "; " + &log.get_status_string();
},
None => {
return 1
},
};
}
println!("{}", status_sv);
return 0;
}

View file

@ -8,6 +8,7 @@ use std::io::prelude::*;
use std::time::SystemTime; use std::time::SystemTime;
#[derive(Debug)]
pub struct StatusObj { pub struct StatusObj {
svname: String, // Service name svname: String, // Service name
svpath: String, // Service path svpath: String, // Service path
@ -177,6 +178,18 @@ impl StatusObj {
return misc::sent_signal(signal); return misc::sent_signal(signal);
} }
pub fn is_up(&mut self) -> bool {
return self.run_finish;
}
pub fn get_name(&mut self) -> String {
return self.svname.clone();
}
pub fn get_path(&mut self) -> String {
return self.svpath.clone();
}
pub fn get_log(&mut self) -> bool { pub fn get_log(&mut self) -> bool {
return self.log; return self.log;
} }