From a17ee534b2f7273ecd49706cd8d368f3f90b8829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Fri, 23 Apr 2021 14:32:26 +0200 Subject: [PATCH] chore: code cleanup --- CHANGELOG.md | 2 ++ src/config.rs | 15 ++++++++++++--- src/main.rs | 18 ++++++++---------- src/metrics.rs | 2 +- src/ping.rs | 6 +++--- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9666e6..a6db34d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- code cleanup ## [v0.4.0] - 2021-04-23 ### Changed diff --git a/src/config.rs b/src/config.rs index ae3c58b..f53b18e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,7 +29,7 @@ pub(crate) struct Config { pub(crate) hosts: HashMap, } -pub(crate) fn setup_clap() -> clap::ArgMatches<'static> { +fn setup_clap() -> clap::ArgMatches<'static> { clap_app!(myapp => (name: crate_name!()) (version: crate_version!()) @@ -42,7 +42,7 @@ pub(crate) fn setup_clap() -> clap::ArgMatches<'static> { .get_matches() } -pub(crate) fn setup_fern(level: u64) { +fn setup_fern(level: u64) { let level = match level { 0 => log::LevelFilter::Error, 1 => log::LevelFilter::Warn, @@ -70,7 +70,16 @@ pub(crate) fn setup_fern(level: u64) { } } -pub(crate) fn read_config(path: &str) -> Result { +fn read_config(path: &str) -> Result { let config_file_content = std::fs::read_to_string(path).context("Couldn't read config file")?; Ok(toml::from_str(&config_file_content).context("Couldn't parse config file")?) } + +pub(crate) fn setup_app() -> Result { + let clap = setup_clap(); + setup_fern(clap.occurrences_of("v")); + let config_path = clap + .value_of("config") + .context("Got no config file. clap should've catched this")?; + Ok(read_config(config_path).context("Couldn't read config file!")?) +} diff --git a/src/main.rs b/src/main.rs index 273ca02..abd8244 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,25 +17,23 @@ * You should have received a copy of the GNU Affero General Public License * * along with this program. If not, see . * ********************************************************************************/ -use anyhow::{Context, Result}; +use anyhow::Result; +use async_anyhow_logger::catch; mod config; mod metrics; mod ping; -use crate::config::{read_config, setup_clap, setup_fern}; -use crate::metrics::start_serving_metrics; -use crate::ping::start_pinging_hosts; +use config::setup_app; +use metrics::start_serving_metrics; +use ping::start_pinging_hosts; #[tokio::main] async fn main() -> Result<()> { - let clap = setup_clap(); - setup_fern(clap.occurrences_of("v")); - let config = - read_config(clap.value_of("config").unwrap()).context("Couldn't read config file!")?; + let config = setup_app()?; - let ping_fut = start_pinging_hosts(config.clone()); - let serve_fut = start_serving_metrics(config.clone()); + let ping_fut = catch(start_pinging_hosts(&config)); + let serve_fut = catch(start_serving_metrics(&config)); futures::pin_mut!(ping_fut); futures::pin_mut!(serve_fut); diff --git a/src/metrics.rs b/src/metrics.rs index d6289e3..4112f1f 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -95,7 +95,7 @@ async fn serve_not_found() -> Result> { .context("Couldn't build not found response")?) } -pub(crate) async fn start_serving_metrics(config: Config) -> Result<()> { +pub(crate) async fn start_serving_metrics(config: &Config) -> Result<()> { let serve_future = Server::bind(&config.listener).serve(make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(serve_req)) })); diff --git a/src/ping.rs b/src/ping.rs index 51b6384..e457b69 100644 --- a/src/ping.rs +++ b/src/ping.rs @@ -41,15 +41,15 @@ lazy_static! { .unwrap(); } -pub(crate) async fn start_pinging_hosts(config: Config) -> Result<()> { +pub(crate) async fn start_pinging_hosts(config: &Config) -> Result<()> { let pinger = Pinger::new().await.context("Couldn't create pinger")?; let mut handles = vec![]; for (host, interval) in config.hosts.clone() { info!("Spawn ping task for {}", host); - handles.push(tokio::spawn(catch(ping_host(pinger.clone(), host, interval)))); + handles.push(tokio::spawn(ping_host(pinger.clone(), host, interval))); } let (result, _, _) = futures::future::select_all(handles).await; - result?; + result??; Ok(()) }