Localization support added
Localization support added
This commit is contained in:
parent
bf96109742
commit
3ea4c96cfc
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -43,7 +43,7 @@ checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db"
|
||||
|
||||
[[package]]
|
||||
name = "cai-watchdog"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"exitcode",
|
||||
@ -51,6 +51,7 @@ dependencies = [
|
||||
"hashmap",
|
||||
"ini",
|
||||
"reqwest",
|
||||
"sys-locale",
|
||||
"sysinfo",
|
||||
"tokio",
|
||||
]
|
||||
@ -949,6 +950,19 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sys-locale"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3358acbb4acd4146138b9bda219e904a6bb5aaaa237f8eed06f4d6bc1580ecee"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"libc",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sysinfo"
|
||||
version = "0.26.7"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cai-watchdog"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
authors = ["Alexander I. Chebykin <alex.chebykin@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
@ -18,3 +18,4 @@ ini = "1.3.0"
|
||||
exitcode = "1.1.2"
|
||||
hashmap = "0.0.1"
|
||||
sysinfo = "0.26.7"
|
||||
sys-locale = "0.2.3"
|
||||
|
||||
89
src/locales.rs
Normal file
89
src/locales.rs
Normal file
@ -0,0 +1,89 @@
|
||||
pub struct Lang {
|
||||
pub cant_find_config_file: String,
|
||||
pub check_failed: String,
|
||||
pub check_is_ok: String,
|
||||
pub configuration_file: String,
|
||||
pub error: String,
|
||||
pub execute: String,
|
||||
pub exiting: String,
|
||||
pub failed_to_execute_process: String,
|
||||
pub is_not_running: String,
|
||||
pub is_not_running_now: String,
|
||||
pub is_offline: String,
|
||||
pub is_offline_now: String,
|
||||
pub is_online: String,
|
||||
pub is_online_now: String,
|
||||
pub is_running: String,
|
||||
pub is_running_now: String,
|
||||
pub process: String,
|
||||
pub service: String,
|
||||
pub service_started: String,
|
||||
pub state_changed_to_false: String,
|
||||
pub state_changed_to_true: String,
|
||||
pub this_help_message: String,
|
||||
pub usage: String,
|
||||
pub ver: String,
|
||||
pub version_info: String
|
||||
}
|
||||
|
||||
pub fn t(lang: String) -> Lang {
|
||||
let l:Lang = if lang == "ru-RU" {
|
||||
Lang{
|
||||
cant_find_config_file: "Не могу найти файл конфигурации".to_string(),
|
||||
check_failed: "Проверка завершилась неудачей".to_string(),
|
||||
check_is_ok: "проверка завершилась успешно".to_string(),
|
||||
configuration_file: "Конфигурационный файл".to_string(),
|
||||
error: "Ошибка".to_string(),
|
||||
execute: "Выполняю".to_string(),
|
||||
exiting: "Завершаю работу...".to_string(),
|
||||
failed_to_execute_process: "Не удалось выполнить процесс".to_string(),
|
||||
is_not_running: "не запущен".to_string(),
|
||||
is_not_running_now: "сейчас не запущен".to_string(),
|
||||
is_offline: "неактивна".to_string(),
|
||||
is_offline_now: "сейчас неактивна".to_string(),
|
||||
is_online: "активна".to_string(),
|
||||
is_online_now: "сейчас активна".to_string(),
|
||||
is_running: "запущен".to_string(),
|
||||
is_running_now: "сейчас запущен".to_string(),
|
||||
process: "Процесс".to_string(),
|
||||
service: "Служба".to_string(),
|
||||
service_started: "Служба запущена".to_string(),
|
||||
state_changed_to_false: "состояние изменилось на истину".to_string(),
|
||||
state_changed_to_true: "состояние изменилось на ложь".to_string(),
|
||||
this_help_message: "Это справочное сообщение".to_string(),
|
||||
usage: "Использование".to_string(),
|
||||
ver: "версия".to_string(),
|
||||
version_info: "Информация о версии".to_string()
|
||||
}
|
||||
} else {
|
||||
Lang{
|
||||
cant_find_config_file: "Can't find configuration file".to_string(),
|
||||
check_failed: "check failed".to_string(),
|
||||
check_is_ok: "check is ok".to_string(),
|
||||
configuration_file: "Configuration file".to_string(),
|
||||
error: "Error".to_string(),
|
||||
execute: "Execute".to_string(),
|
||||
exiting: "Exiting...".to_string(),
|
||||
failed_to_execute_process: "Can't execute process".to_string(),
|
||||
is_not_running: "is not running".to_string(),
|
||||
is_not_running_now: "is not running now".to_string(),
|
||||
is_offline: "is offline".to_string(),
|
||||
is_offline_now: "is offline now".to_string(),
|
||||
is_online: "is online".to_string(),
|
||||
is_online_now: "is online now".to_string(),
|
||||
is_running: "is running".to_string(),
|
||||
is_running_now: "is running now".to_string(),
|
||||
process: "Process".to_string(),
|
||||
service: "Service".to_string(),
|
||||
service_started: "Service started".to_string(),
|
||||
state_changed_to_false: "state changed to false".to_string(),
|
||||
state_changed_to_true: "state changed to true".to_string(),
|
||||
this_help_message: "This help message".to_string(),
|
||||
usage: "Usage".to_string(),
|
||||
ver: "ver".to_string(),
|
||||
version_info: "Version info".to_string()
|
||||
}
|
||||
};
|
||||
|
||||
return l;
|
||||
}
|
||||
171
src/main.rs
171
src/main.rs
@ -8,6 +8,9 @@ use std::process::Command;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use sysinfo::{System, SystemExt};
|
||||
use sys_locale::get_locale;
|
||||
|
||||
mod locales;
|
||||
|
||||
/// Rule description structure
|
||||
struct Rule {
|
||||
@ -131,19 +134,21 @@ fn debug_log(text: String) {
|
||||
/// execute("gedit".to_string());
|
||||
/// ```
|
||||
fn execute(command: String) {
|
||||
debug_log(format!("execute {}", command));
|
||||
let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
|
||||
|
||||
debug_log(format!("{} {}", locales::t(locale.clone()).execute, command));
|
||||
|
||||
let output = if cfg!(target_os = "windows") {
|
||||
Command::new("powershell")
|
||||
.args(["-NoLogo", "-NonInteractive", "-Command", &command])
|
||||
.output()
|
||||
.expect("failed to execute process")
|
||||
.expect(&locales::t(locale.clone()).failed_to_execute_process) //.expect("failed to execute process")
|
||||
} else {
|
||||
Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(&command)
|
||||
.output()
|
||||
.expect("failed to execute process")
|
||||
.expect(&locales::t(locale.clone()).failed_to_execute_process) //.expect("failed to execute process")
|
||||
};
|
||||
|
||||
let result = output.stdout;
|
||||
@ -164,16 +169,18 @@ fn execute(command: String) {
|
||||
/// print_help(args.clone());
|
||||
/// ```
|
||||
fn print_help(args: Vec<String>) {
|
||||
let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
|
||||
|
||||
if cfg!(windows) {
|
||||
if args.len() > 1 && (args[1].to_string() == "/help".to_string() || args[1].to_string() == "/?".to_string()) {
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
println!("CAI Watchdog ver {}", VERSION);
|
||||
println!("CAI Watchdog {} {}", locales::t(locale.clone()).ver, VERSION);
|
||||
println!("");
|
||||
println!("Usage: {} [/? | /help | /v | /ver | config_file]", &args[0]);
|
||||
println!(" /? | /help : This help message");
|
||||
println!(" /v | /ver : Version info");
|
||||
println!(" config_file : Configuration file");
|
||||
println!(" /? | /help : {}", locales::t(locale.clone()).this_help_message);
|
||||
println!(" /v | /ver : {}", locales::t(locale.clone()).version_info);
|
||||
println!(" config_file : {}", locales::t(locale.clone()).configuration_file);
|
||||
|
||||
std::process::exit(exitcode::OK);
|
||||
} else if args.len() > 1
|
||||
@ -181,7 +188,7 @@ fn print_help(args: Vec<String>) {
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
println!("CAI Watchdog ver {}", VERSION);
|
||||
println!("CAI Watchdog {} {}", locales::t(locale.clone()).ver, VERSION);
|
||||
|
||||
std::process::exit(exitcode::OK);
|
||||
}
|
||||
@ -189,19 +196,19 @@ fn print_help(args: Vec<String>) {
|
||||
if args.len() > 1 && (args[1].to_string() == "--help".to_string() || args[1].to_string() == "-h".to_string()) {
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
println!("CAI Watchdog ver {}", VERSION);
|
||||
println!("CAI Watchdog {} {}", locales::t(locale.clone()).ver, VERSION);
|
||||
println!("");
|
||||
println!("Usage: {} [-h | --help | -v | --ver | config_file]", &args[0]);
|
||||
println!(" -h | --help : This help message");
|
||||
println!(" -v | --ver : Version info");
|
||||
println!(" config_file : Configuration file");
|
||||
println!("{}: {} [-h | --help | -v | --ver | config_file]", locales::t(locale.clone()).usage, &args[0]);
|
||||
println!(" -h | --help : {}", locales::t(locale.clone()).this_help_message);
|
||||
println!(" -v | --ver : {}", locales::t(locale.clone()).version_info);
|
||||
println!(" config_file : {}", locales::t(locale.clone()).configuration_file);
|
||||
|
||||
std::process::exit(exitcode::OK);
|
||||
} else if args.len() > 1
|
||||
&& (args[1].to_string() == "--ver".to_string() || args[1].to_string() == "-v".to_string()) {
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
println!("CAI Watchdog ver {}", VERSION);
|
||||
println!("CAI Watchdog {} {}", locales::t(locale.clone()).ver, VERSION);
|
||||
|
||||
std::process::exit(exitcode::OK);
|
||||
}
|
||||
@ -210,6 +217,11 @@ fn print_help(args: Vec<String>) {
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
|
||||
|
||||
debug_log(format!("The current locale is {}", locale.clone()));
|
||||
|
||||
// println!("{}", locales::t(locale).usage);
|
||||
|
||||
print_help(args.clone());
|
||||
|
||||
@ -222,8 +234,11 @@ fn main() {
|
||||
};
|
||||
|
||||
if !Path::new(cfg_file).exists() {
|
||||
println!("\u{26a0} Error! Can't find configuration file.");
|
||||
println!("Exiting...");
|
||||
println!("\u{26a0} {}! {}.",
|
||||
locales::t(locale.clone()).error,
|
||||
locales::t(locale.clone()).cant_find_config_file
|
||||
);
|
||||
println!("{}", locales::t(locale.clone()).exiting);
|
||||
|
||||
std::process::exit(exitcode::CONFIG);
|
||||
}
|
||||
@ -314,13 +329,12 @@ fn main() {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
debug_log("\u{2139} Service started".to_string());
|
||||
debug_log(format!("\u{2139} {}", locales::t(locale.clone()).service_started));
|
||||
|
||||
if on_start_command.to_string() != "" {
|
||||
execute(on_start_command);
|
||||
}
|
||||
|
||||
let mut just_started_prc = true;
|
||||
let mut just_started_web = true;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
@ -333,51 +347,89 @@ fn main() {
|
||||
if check(&tasks_web[i]).await {
|
||||
if tasks_web[i].last_state != true || just_started_web {
|
||||
if tasks_web[i].command.to_string() == "".to_string() {
|
||||
println!("\u{2705} {} state changed to true", tasks_web[i].uri);
|
||||
println!("\u{2705} {} {}",
|
||||
tasks_web[i].uri,
|
||||
locales::t(locale.clone()).state_changed_to_true
|
||||
);
|
||||
} else {
|
||||
debug_log(format!("\u{2705} {} state changed to true", tasks_web[i].uri));
|
||||
debug_log(format!("\u{2705} {} {}",
|
||||
tasks_web[i].uri,
|
||||
locales::t(locale.clone()).state_changed_to_true)
|
||||
);
|
||||
|
||||
let shell_cmd =
|
||||
tasks_web[i].command.to_string()
|
||||
.replace("<email>", &tasks_web[i].email)
|
||||
.replace("<subject>", &format!("\"\u{2705} Service {} ({}) is online\"", tasks_web[i].service, tasks_web[i].uri))
|
||||
.replace("<message>", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks_web[i].service, tasks_web[i].uri))
|
||||
.replace("<subject>",
|
||||
&format!(
|
||||
"\"\u{2705} {} {} ({}) {}\"",
|
||||
locales::t(locale.clone()).service,
|
||||
tasks_web[i].service,
|
||||
tasks_web[i].uri,
|
||||
locales::t(locale.clone()).is_online
|
||||
)
|
||||
)
|
||||
.replace("<message>",
|
||||
&format!(
|
||||
"\"\u{2705} {} {} ({}) {}\"",
|
||||
locales::t(locale.clone()).service,
|
||||
tasks_web[i].service,
|
||||
tasks_web[i].uri,
|
||||
locales::t(locale.clone()).is_online_now
|
||||
)
|
||||
)
|
||||
.replace("<service>", &tasks_web[i].service)
|
||||
.replace("<uri>", &tasks_web[i].uri)
|
||||
.replace("<state>", "online");
|
||||
|
||||
debug_log(format!("execute {}", shell_cmd));
|
||||
debug_log(format!("{} {}", locales::t(locale.clone()).execute, shell_cmd));
|
||||
|
||||
execute(shell_cmd);
|
||||
}
|
||||
}
|
||||
|
||||
debug_log(format!("\u{2705} {} check is ok", tasks_web[i].uri));
|
||||
debug_log(format!("\u{2705} {} {}", tasks_web[i].uri, locales::t(locale.clone()).check_is_ok));
|
||||
|
||||
tasks_web[i].last_state = true;
|
||||
} else {
|
||||
if tasks_web[i].last_state != false || just_started_web {
|
||||
if tasks_web[i].command.to_string() == "".to_string() {
|
||||
println!("\u{274c} {} state changed to false", tasks_web[i].uri);
|
||||
println!("\u{274c} {} {}", tasks_web[i].uri, locales::t(locale.clone()).state_changed_to_false);
|
||||
} else {
|
||||
debug_log(format!("\u{274c} {} state changed to false", tasks_web[i].uri));
|
||||
debug_log(format!("\u{274c} {} {}", tasks_web[i].uri, locales::t(locale.clone()).state_changed_to_false));
|
||||
|
||||
let shell_cmd =
|
||||
tasks_web[i].command.to_string()
|
||||
.replace("<email>", &tasks_web[i].email)
|
||||
.replace("<subject>", &format!("\"\u{274c} Service {} ({}) is offline\"", tasks_web[i].service, tasks_web[i].uri))
|
||||
.replace("<message>", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks_web[i].service, tasks_web[i].uri))
|
||||
.replace("<subject>", &
|
||||
format!(
|
||||
"\"\u{274c} {} {} ({}) {}\"",
|
||||
locales::t(locale.clone()).service,
|
||||
tasks_web[i].service,
|
||||
tasks_web[i].uri,
|
||||
locales::t(locale.clone()).is_offline
|
||||
)
|
||||
)
|
||||
.replace("<message>", &
|
||||
format!(
|
||||
"\"\u{274c} {} {} ({}) {}\"",
|
||||
locales::t(locale.clone()).service,
|
||||
tasks_web[i].service,
|
||||
tasks_web[i].uri,
|
||||
locales::t(locale.clone()).is_offline_now
|
||||
)
|
||||
)
|
||||
.replace("<service>", &tasks_web[i].service)
|
||||
.replace("<uri>", &tasks_web[i].uri)
|
||||
.replace("<state>", "offline");
|
||||
|
||||
debug_log(format!("execute {}", shell_cmd));
|
||||
debug_log(format!("{} {}", locales::t(locale.clone()).execute, shell_cmd));
|
||||
|
||||
execute(shell_cmd);
|
||||
}
|
||||
}
|
||||
|
||||
debug_log(format!("\u{274c} {} check failed", tasks_web[i].uri));
|
||||
debug_log(format!("\u{274c} {} {}", tasks_web[i].uri, locales::t(locale.clone()).check_failed));
|
||||
|
||||
tasks_web[i].last_state = false;
|
||||
}
|
||||
@ -390,6 +442,9 @@ fn main() {
|
||||
}
|
||||
});
|
||||
|
||||
let mut just_started_prc = true;
|
||||
let locale_prc = get_locale().unwrap_or_else(|| String::from("en-US"));
|
||||
|
||||
loop {
|
||||
for i in 0..tasks_prc.len() {
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
@ -399,50 +454,82 @@ fn main() {
|
||||
if check(&tasks_prc[i]).await {
|
||||
if tasks_prc[i].last_state != true || just_started_prc {
|
||||
if tasks_prc[i].command.to_string() == "".to_string() {
|
||||
println!("\u{2705} {} state changed to true", tasks_prc[i].uri);
|
||||
println!("\u{2705} {} {}", tasks_prc[i].uri, locales::t(locale_prc.clone()).state_changed_to_true);
|
||||
} else {
|
||||
debug_log(format!("\u{2705} {} state changed to true", tasks_prc[i].uri));
|
||||
debug_log(format!("\u{2705} {} {}", tasks_prc[i].uri, locales::t(locale_prc.clone()).state_changed_to_true));
|
||||
|
||||
let shell_cmd =
|
||||
tasks_prc[i].command.to_string()
|
||||
.replace("<email>", &tasks_prc[i].email)
|
||||
.replace("<subject>", &format!("\"\u{2705} Process {} ({}) is running\"", tasks_prc[i].service, tasks_prc[i].process))
|
||||
.replace("<message>", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks_prc[i].service, tasks_prc[i].process))
|
||||
.replace("<subject>",
|
||||
&format!(
|
||||
"\"\u{2705} {} {} ({}) {}\"",
|
||||
locales::t(locale_prc.clone()).process,
|
||||
tasks_prc[i].service,
|
||||
tasks_prc[i].process,
|
||||
locales::t(locale_prc.clone()).is_running
|
||||
)
|
||||
)
|
||||
.replace("<message>",
|
||||
&format!(
|
||||
"\"\u{2705} {} {} ({}) {}\"",
|
||||
locales::t(locale_prc.clone()).process,
|
||||
tasks_prc[i].service,
|
||||
tasks_prc[i].process,
|
||||
locales::t(locale_prc.clone()).is_running_now
|
||||
)
|
||||
)
|
||||
.replace("<service>", &tasks_prc[i].service)
|
||||
.replace("<process>", &tasks_prc[i].process)
|
||||
.replace("<state>", "running");
|
||||
|
||||
debug_log(format!("execute {}", shell_cmd));
|
||||
debug_log(format!("{} {}", locales::t(locale_prc.clone()).execute, shell_cmd));
|
||||
|
||||
execute(shell_cmd);
|
||||
}
|
||||
}
|
||||
|
||||
debug_log(format!("\u{2705} {} is running", tasks_prc[i].process));
|
||||
debug_log(format!("\u{2705} {} {}", tasks_prc[i].process, locales::t(locale_prc.clone()).is_running));
|
||||
|
||||
tasks_prc[i].last_state = true;
|
||||
} else {
|
||||
if tasks_prc[i].last_state != false || just_started_prc {
|
||||
if tasks_prc[i].command.to_string() == "".to_string() {
|
||||
println!("\u{274c} {} state changed to false", tasks_prc[i].uri);
|
||||
println!("\u{274c} {} {}", tasks_prc[i].uri, locales::t(locale_prc.clone()).state_changed_to_false);
|
||||
} else {
|
||||
debug_log(format!("\u{274c} {} state changed to false", tasks_prc[i].uri));
|
||||
debug_log(format!("\u{274c} {} {}", tasks_prc[i].uri, locales::t(locale_prc.clone()).state_changed_to_false));
|
||||
|
||||
let shell_cmd =
|
||||
tasks_prc[i].command.to_string()
|
||||
.replace("<email>", &tasks_prc[i].email)
|
||||
.replace("<subject>", &format!("\"\u{274c} Process {} ({}) is not running\"", tasks_prc[i].service, tasks_prc[i].process))
|
||||
.replace("<message>", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks_prc[i].service, tasks_prc[i].process))
|
||||
.replace("<subject>",
|
||||
&format!(
|
||||
"\"\u{274c} {} {} ({}) {}\"",
|
||||
locales::t(locale_prc.clone()).process,
|
||||
tasks_prc[i].service,
|
||||
tasks_prc[i].process,
|
||||
locales::t(locale_prc.clone()).is_not_running
|
||||
)
|
||||
)
|
||||
.replace("<message>",
|
||||
&format!(
|
||||
"\"\u{274c} {} {} ({}) {}\"",
|
||||
locales::t(locale_prc.clone()).process,
|
||||
tasks_prc[i].service,
|
||||
tasks_prc[i].process,
|
||||
locales::t(locale_prc.clone()).is_not_running_now
|
||||
)
|
||||
)
|
||||
.replace("<service>", &tasks_prc[i].service)
|
||||
.replace("<process>", &tasks_prc[i].process)
|
||||
.replace("<state>", "stopped");
|
||||
|
||||
debug_log(format!("execute {}", shell_cmd));
|
||||
debug_log(format!("{} {}", locales::t(locale_prc.clone()).execute, shell_cmd));
|
||||
execute(shell_cmd);
|
||||
}
|
||||
}
|
||||
|
||||
debug_log(format!("\u{274c} {} is not running", tasks_prc[i].process));
|
||||
debug_log(format!("\u{274c} {} {}", tasks_prc[i].process, locales::t(locale_prc.clone()).is_not_running));
|
||||
|
||||
|
||||
tasks_prc[i].last_state = false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user