mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 19:16:50 +00:00
Add color configuration
This commit is contained in:
parent
188d06d27f
commit
300e2cc2b6
15
README.adoc
15
README.adoc
@ -33,6 +33,21 @@ You can optionally specify `check_type`:
|
|||||||
* `Html`: Default value, checks if a request is succeessful and returns HTTP OK - 200.
|
* `Html`: Default value, checks if a request is succeessful and returns HTTP OK - 200.
|
||||||
* `Actuator`: Like `Html`, but checks if _Actuator_ shows that the application is up and running.
|
* `Actuator`: Like `Html`, but checks if _Actuator_ shows that the application is up and running.
|
||||||
|
|
||||||
|
=== Colors
|
||||||
|
|
||||||
|
To change the colors, use the following configuration. As an example the colors of the default configuration are shown.
|
||||||
|
|
||||||
|
----
|
||||||
|
...
|
||||||
|
[colors]
|
||||||
|
up = "#00FF00"
|
||||||
|
warn = "#FFFF00"
|
||||||
|
down = "#FF0000"
|
||||||
|
...
|
||||||
|
----
|
||||||
|
|
||||||
|
The color configuration is optional. If used, all colors must be specified.
|
||||||
|
|
||||||
=== Execute commands
|
=== Execute commands
|
||||||
|
|
||||||
_This feature is still experimental._
|
_This feature is still experimental._
|
||||||
|
32
src/main.rs
32
src/main.rs
@ -8,9 +8,17 @@ use tokio::task;
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
interval: Option<u64>,
|
interval: Option<u64>,
|
||||||
|
colors: Option<ColorConfig>,
|
||||||
checks: Vec<CheckConfig>,
|
checks: Vec<CheckConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ColorConfig {
|
||||||
|
up: String,
|
||||||
|
warn: String,
|
||||||
|
down: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct CheckConfig {
|
struct CheckConfig {
|
||||||
name: String,
|
name: String,
|
||||||
@ -42,10 +50,18 @@ 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 = match get_config().colors {
|
||||||
|
Some(color_config) => color_config,
|
||||||
|
None => ColorConfig {
|
||||||
|
up: String::from("#00FF00"),
|
||||||
|
warn: String::from("#FFFF00"),
|
||||||
|
down: String::from("#FF0000"),
|
||||||
|
},
|
||||||
|
};
|
||||||
let color = match &self.state {
|
let color = match &self.state {
|
||||||
CheckState::Up => "#00FF00",
|
CheckState::Up => color_config.up,
|
||||||
CheckState::Warn => "#FFFF00",
|
CheckState::Warn => color_config.warn,
|
||||||
CheckState::Down => "#FF0000",
|
CheckState::Down => color_config.down,
|
||||||
};
|
};
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
@ -105,7 +121,7 @@ async fn print_states(check_configs: &[CheckConfig]) {
|
|||||||
println!("{}],", entries.join(","));
|
println!("{}],", entries.join(","));
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_config() -> Config {
|
fn get_config() -> Config {
|
||||||
let home_dir = dirs::home_dir().unwrap();
|
let home_dir = dirs::home_dir().unwrap();
|
||||||
match fs::read_to_string(format!(
|
match fs::read_to_string(format!(
|
||||||
"{}/.checkbar.toml",
|
"{}/.checkbar.toml",
|
||||||
@ -115,18 +131,20 @@ async fn get_config() -> Config {
|
|||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
Err(_e) => Config {
|
Err(_e) => Config {
|
||||||
interval: None,
|
interval: None,
|
||||||
|
colors: None,
|
||||||
checks: vec![],
|
checks: vec![],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Err(_e) => Config {
|
Err(_) => Config {
|
||||||
interval: None,
|
interval: None,
|
||||||
|
colors: None,
|
||||||
checks: vec![],
|
checks: vec![],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_click_cmd(name: String) -> Option<String> {
|
async fn get_click_cmd(name: String) -> Option<String> {
|
||||||
for check in get_config().await.checks {
|
for check in get_config().checks {
|
||||||
if check.name == name {
|
if check.name == name {
|
||||||
return check.click_cmd;
|
return check.click_cmd;
|
||||||
}
|
}
|
||||||
@ -175,7 +193,7 @@ async fn main() {
|
|||||||
|
|
||||||
let checks = task::spawn(async {
|
let checks = task::spawn(async {
|
||||||
loop {
|
loop {
|
||||||
let config = get_config().await;
|
let config = get_config();
|
||||||
print_states(&config.checks).await;
|
print_states(&config.checks).await;
|
||||||
std::thread::sleep(Duration::from_secs(config.interval.unwrap_or(60)));
|
std::thread::sleep(Duration::from_secs(config.interval.unwrap_or(60)));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user