diff --git a/src/main.rs b/src/main.rs index 324f500..de99b7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,8 @@ use eyre::{OptionExt, WrapErr}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::convert::TryFrom; -use std::time::{Duration, SystemTime}; use std::fmt; +use std::time::{Duration, SystemTime}; const RESTIC_ALARM_BUCKET: &str = "restic-alarm-state"; const RESTIC_ALARM_STATE_FILE: &str = "state.toml"; @@ -56,19 +56,19 @@ struct AlertStatus { impl fmt::Display for AlertStatus { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let days = |d: Duration| { - d.as_secs() / (3600 * 24) - }; - let hours = |d: Duration| { - (d.as_secs() / 3600) % 24 - }; - write!(f, "{}: inactivity: {} days, {} hours, last_alert: {}", - (if self.alert { "ALERT" } else { "no alert" }), - days(self.inactivity), hours(self.inactivity), - match self.last_alert { - None => "none".to_owned(), - Some(d) => format!("{} days, {} hours", days(d), hours(d)) - }) + let days = |d: Duration| d.as_secs() / (3600 * 24); + let hours = |d: Duration| (d.as_secs() / 3600) % 24; + write!( + f, + "{}: inactivity: {} days, {} hours, last_alert: {}", + (if self.alert { "ALERT" } else { "no alert" }), + days(self.inactivity), + hours(self.inactivity), + match self.last_alert { + None => "none".to_owned(), + Some(d) => format!("{} days, {} hours", days(d), hours(d)), + } + ) } } @@ -221,7 +221,11 @@ async fn repo_last_snapshot(client: &s3::Client, repo: &str) -> eyre::Result eyre::Result> { +async fn check_repo( + client: &s3::Client, + state: &mut State, + repo: &str, +) -> eyre::Result> { let config = read_repo_config(client, repo).await?; if let Some(inactivity) = repo_last_snapshot(client, repo).await? { let last_alert = state.last_alert(repo); @@ -232,7 +236,11 @@ async fn check_repo(client: &s3::Client, state: &mut State, repo: &str) -> eyre: state.update_last_alert(repo); write_state(client, state).await?; } - Ok(Some(AlertStatus { alert, inactivity, last_alert })) + Ok(Some(AlertStatus { + alert, + inactivity, + last_alert, + })) } else { Ok(None) } @@ -258,10 +266,10 @@ async fn main() -> eyre::Result<()> { match check_repo(&client, &mut state, &repo).await { Ok(None) => { println!("{}: no snapshot, skipping", &repo) - }, + } Ok(Some(status)) => { println!("{}: {}", &repo, status); - }, + } Err(err) => { // is this the best way to log the error? println!("Error ({}): {:?}", &repo, err)