1
0
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:
Paul-Christian Volkmer 2023-01-14 21:24:41 +01:00
parent 139cc94bf7
commit 80f42de06a
4 changed files with 50 additions and 34 deletions

View File

@ -9,7 +9,7 @@ use serde_json::json;
pub use crate::checker::actuator::Checker as ActuatorChecker;
pub use crate::checker::http::Checker as HttpChecker;
pub use crate::checker::tcp::Checker as TcpChecker;
use crate::config::get_config;
use crate::config::Config;
pub struct CheckResult {
pub name: String,
@ -18,7 +18,7 @@ pub struct CheckResult {
impl Display for CheckResult {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let color_config = get_config().colors;
let color_config = Config::read().colors;
let color = match &self.state {
CheckState::Up => color_config.up,
CheckState::Warn => color_config.warn,

View File

@ -6,7 +6,7 @@ use regex::Regex;
use serde::de::{Error, Visitor};
use serde::{Deserialize, Deserializer};
#[derive(Deserialize)]
#[derive(Default, Deserialize)]
pub struct Config {
#[serde(default, deserialize_with = "deserialize_duration")]
pub interval: Option<Duration>,
@ -16,6 +16,29 @@ pub struct Config {
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)]
pub struct ColorConfig {
pub up: String,
@ -48,34 +71,6 @@ pub enum CheckType {
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>
where
D: Deserializer<'de>,
@ -211,6 +206,22 @@ mod tests {
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]
fn test_should_parse_durations() {
assert_eq!(parse_duration("1m30s"), Some(Duration::from_secs(90)));

View File

@ -6,7 +6,7 @@ use serde_json::json;
use tokio::task;
use crate::checker::{ActuatorChecker, CheckResult, HttpChecker, TcpChecker};
use crate::config::{get_config, CheckConfig, CheckType};
use crate::config::{CheckConfig, CheckType, Config};
mod checker;
mod config;
@ -41,7 +41,7 @@ async fn print_states(check_configs: &[CheckConfig]) {
}
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 {
return check.click_cmd;
}
@ -100,7 +100,7 @@ async fn main() {
let checks = task::spawn(async {
loop {
let config = get_config();
let config = Config::read();
print_states(&config.checks).await;
let interval = match config.interval {
Some(value) => value,

5
tests/testconfig1.toml Normal file
View File

@ -0,0 +1,5 @@
interval = "10s"
[[checks]]
name = "www"
url = "https://example.com"