rustit/01_phase_rust_src/sv-rustit/src/main.rs
2024-04-13 19:57:16 +01:00

90 lines
2.2 KiB
Rust

mod parser;
mod executor;
pub mod status_obj;
use std::env;
use std::process::exit;
fn main() {
// def env variables.
let svdir_env = env::var("SVDIR");
let svwait_env = env::var("SVWAIT");
let svdir_var = match svdir_env {
Ok(var) => var,
Err(..) => "/run/runit/service".to_string(),
};
let mut svwait_var = match svwait_env {
Ok(env) => match env.parse::<i32>() {
Ok(var) => var,
Err(..) => 7,
},
Err(..) => 7,
};
let mut verbose: i8 = 0;
// run the parser
let args: Vec<String> = env::args().collect();
let arg_parser: Vec<String> = parser::parse_args(args);
let mut i = 0;
let mut command = "INVALID";
let mut services: Vec<String> = Vec::new();
// make data from parser.
for val in arg_parser.iter() {
if i == 0 {
if val == "-v" {
verbose = 1;
}
else if val == "-w" {
i = 3;
// take next parametter before returning to i = 0.
}
else if val == "--" {
i += 1;
}
else {
if val.len() >= 2 {
let mut j = 0;
for ch in val.chars() {
if j == 1 {
println!("sv: illegal option -- {}", ch );
}
j += 1;
}
}
parser::misc::usage();
exit(100);
}
}
else if i == 1 {
// Set the command as first arguments.
command = val;
i += 1;
}
else if i == 2 {
// add services to control.
services.push(val.to_string());
}
else if i == 3 {
// change svwait after flag -w
svwait_var = match val.parse::<i32>() {
Ok(val) => val,
Err(..) => svwait_var,
};
i = 0;
}
}
// Exit after all services are correctly executed.
exit(executor::execute(svdir_var, svwait_var, verbose, command, services));
}