-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Tracker Checker: use clap and anyhow
- Loading branch information
1 parent
75a502f
commit 8543190
Showing
3 changed files
with
33 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,41 @@ | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
|
||
use anyhow::Context; | ||
use clap::Parser; | ||
|
||
use super::config::Configuration; | ||
use super::console::Console; | ||
use super::service::{CheckError, Service}; | ||
use super::service::{CheckResult, Service}; | ||
use crate::checker::config::parse_from_json; | ||
|
||
pub const NUMBER_OF_ARGUMENTS: usize = 2; | ||
#[derive(Parser, Debug)] | ||
#[clap(author, version, about, long_about = None)] | ||
struct Args { | ||
config_path: PathBuf, | ||
} | ||
|
||
/// # Errors | ||
/// | ||
/// If some checks fails it will return a vector with all failing checks. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if: | ||
/// | ||
/// - It can't read the json configuration file. | ||
/// - The configuration file is invalid. | ||
pub async fn run() -> Result<(), Vec<CheckError>> { | ||
let args = parse_arguments(); | ||
let config = setup_config(&args); | ||
/// Will return an error if it can't read or parse the configuration file. | ||
pub async fn run() -> anyhow::Result<Vec<CheckResult>> { | ||
let args = Args::parse(); | ||
|
||
let config = setup_config(&args)?; | ||
|
||
let console_printer = Console {}; | ||
|
||
let service = Service { | ||
config: Arc::new(config), | ||
console: console_printer, | ||
}; | ||
|
||
service.run_checks().await | ||
} | ||
|
||
pub struct Arguments { | ||
pub config_path: String, | ||
} | ||
|
||
fn parse_arguments() -> Arguments { | ||
let args: Vec<String> = std::env::args().collect(); | ||
|
||
if args.len() < NUMBER_OF_ARGUMENTS { | ||
eprintln!("Usage: cargo run --bin tracker_checker <PATH_TO_CONFIG_FILE>"); | ||
eprintln!("For example: cargo run --bin tracker_checker ./share/default/config/tracker_checker.json"); | ||
std::process::exit(1); | ||
} | ||
|
||
let config_path = &args[1]; | ||
|
||
Arguments { | ||
config_path: config_path.to_string(), | ||
} | ||
Ok(service.run_checks().await) | ||
} | ||
|
||
fn setup_config(args: &Arguments) -> Configuration { | ||
let file_content = std::fs::read_to_string(args.config_path.clone()) | ||
.unwrap_or_else(|_| panic!("Can't read config file {}", args.config_path)); | ||
fn setup_config(args: &Args) -> anyhow::Result<Configuration> { | ||
let file_content = | ||
std::fs::read_to_string(&args.config_path).with_context(|| format!("can't read config file {:?}", args.config_path))?; | ||
|
||
parse_from_json(&file_content).expect("Invalid config format") | ||
parse_from_json(&file_content).context("invalid config format") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters