mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 11:06:50 +00:00
Test reading config file
This commit is contained in:
parent
139cc94bf7
commit
80f42de06a
@ -9,7 +9,7 @@ use serde_json::json;
|
|||||||
pub use crate::checker::actuator::Checker as ActuatorChecker;
|
pub use crate::checker::actuator::Checker as ActuatorChecker;
|
||||||
pub use crate::checker::http::Checker as HttpChecker;
|
pub use crate::checker::http::Checker as HttpChecker;
|
||||||
pub use crate::checker::tcp::Checker as TcpChecker;
|
pub use crate::checker::tcp::Checker as TcpChecker;
|
||||||
use crate::config::get_config;
|
use crate::config::Config;
|
||||||
|
|
||||||
pub struct CheckResult {
|
pub struct CheckResult {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@ -18,7 +18,7 @@ pub struct CheckResult {
|
|||||||
|
|
||||||
impl Display for CheckResult {
|
impl Display for CheckResult {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
let color_config = get_config().colors;
|
let color_config = Config::read().colors;
|
||||||
let color = match &self.state {
|
let color = match &self.state {
|
||||||
CheckState::Up => color_config.up,
|
CheckState::Up => color_config.up,
|
||||||
CheckState::Warn => color_config.warn,
|
CheckState::Warn => color_config.warn,
|
||||||
|
@ -6,7 +6,7 @@ use regex::Regex;
|
|||||||
use serde::de::{Error, Visitor};
|
use serde::de::{Error, Visitor};
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Default, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[serde(default, deserialize_with = "deserialize_duration")]
|
#[serde(default, deserialize_with = "deserialize_duration")]
|
||||||
pub interval: Option<Duration>,
|
pub interval: Option<Duration>,
|
||||||
@ -16,6 +16,29 @@ pub struct Config {
|
|||||||
pub checks: Vec<CheckConfig>,
|
pub checks: Vec<CheckConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
fn get_config_file() -> String {
|
||||||
|
match env::args().nth(1) {
|
||||||
|
Some(config_file) => config_file,
|
||||||
|
None => format!(
|
||||||
|
"{}/.checkbar.toml",
|
||||||
|
dirs::home_dir().unwrap().to_str().unwrap_or("")
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read() -> Self {
|
||||||
|
Self::read_file(Self::get_config_file().as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_file(filename: &str) -> Self {
|
||||||
|
match fs::read_to_string(filename) {
|
||||||
|
Ok(config) => toml::from_str(config.as_str()).unwrap_or_default(),
|
||||||
|
Err(_) => Config::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct ColorConfig {
|
pub struct ColorConfig {
|
||||||
pub up: String,
|
pub up: String,
|
||||||
@ -48,34 +71,6 @@ pub enum CheckType {
|
|||||||
Tcp,
|
Tcp,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_config_file() -> String {
|
|
||||||
match env::args().nth(1) {
|
|
||||||
Some(config_file) => config_file,
|
|
||||||
None => format!(
|
|
||||||
"{}/.checkbar.toml",
|
|
||||||
dirs::home_dir().unwrap().to_str().unwrap_or("")
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_config() -> Config {
|
|
||||||
match fs::read_to_string(get_config_file()) {
|
|
||||||
Ok(config) => match toml::from_str(config.as_str()) {
|
|
||||||
Ok(config) => config,
|
|
||||||
Err(_e) => Config {
|
|
||||||
interval: None,
|
|
||||||
colors: ColorConfig::default(),
|
|
||||||
checks: vec![],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Err(_) => Config {
|
|
||||||
interval: None,
|
|
||||||
colors: ColorConfig::default(),
|
|
||||||
checks: vec![],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize_duration<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
|
fn deserialize_duration<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
@ -211,6 +206,22 @@ mod tests {
|
|||||||
assert_eq!(config.colors.down, "#FF0000".to_string());
|
assert_eq!(config.colors.down, "#FF0000".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_read_and_parse_file() {
|
||||||
|
let config = Config::read_file("./tests/testconfig1.toml");
|
||||||
|
assert_eq!(config.interval, Some(Duration::from_secs(10)));
|
||||||
|
assert_eq!(config.checks.len(), 1);
|
||||||
|
assert_eq!(config.checks[0].name, "www");
|
||||||
|
assert_eq!(config.checks[0].url, "https://example.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_return_default_if_no_config_file() {
|
||||||
|
let config = Config::read_file("./tests/no_testconfig.toml");
|
||||||
|
assert_eq!(config.interval, None);
|
||||||
|
assert_eq!(config.checks.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_should_parse_durations() {
|
fn test_should_parse_durations() {
|
||||||
assert_eq!(parse_duration("1m30s"), Some(Duration::from_secs(90)));
|
assert_eq!(parse_duration("1m30s"), Some(Duration::from_secs(90)));
|
||||||
|
@ -6,7 +6,7 @@ use serde_json::json;
|
|||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
use crate::checker::{ActuatorChecker, CheckResult, HttpChecker, TcpChecker};
|
use crate::checker::{ActuatorChecker, CheckResult, HttpChecker, TcpChecker};
|
||||||
use crate::config::{get_config, CheckConfig, CheckType};
|
use crate::config::{CheckConfig, CheckType, Config};
|
||||||
|
|
||||||
mod checker;
|
mod checker;
|
||||||
mod config;
|
mod config;
|
||||||
@ -41,7 +41,7 @@ async fn print_states(check_configs: &[CheckConfig]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn get_click_cmd(name: String) -> Option<String> {
|
async fn get_click_cmd(name: String) -> Option<String> {
|
||||||
for check in get_config().checks {
|
for check in Config::read().checks {
|
||||||
if check.name == name {
|
if check.name == name {
|
||||||
return check.click_cmd;
|
return check.click_cmd;
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ async fn main() {
|
|||||||
|
|
||||||
let checks = task::spawn(async {
|
let checks = task::spawn(async {
|
||||||
loop {
|
loop {
|
||||||
let config = get_config();
|
let config = Config::read();
|
||||||
print_states(&config.checks).await;
|
print_states(&config.checks).await;
|
||||||
let interval = match config.interval {
|
let interval = match config.interval {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
|
5
tests/testconfig1.toml
Normal file
5
tests/testconfig1.toml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
interval = "10s"
|
||||||
|
|
||||||
|
[[checks]]
|
||||||
|
name = "www"
|
||||||
|
url = "https://example.com"
|
Loading…
x
Reference in New Issue
Block a user