use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{Context, Result};
use clap::Parser;
use tracing::level_filters::LevelFilter;
use super::config::Configuration;
use super::console::Console;
use super::service::{CheckResult, Service};
use crate::console::clients::checker::config::parse_from_json;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(short, long, env = "TORRUST_CHECKER_CONFIG_PATH")]
config_path: Option<PathBuf>,
#[clap(env = "TORRUST_CHECKER_CONFIG", hide_env_values = true)]
config_content: Option<String>,
}
pub async fn run() -> Result<Vec<CheckResult>> {
tracing_stdout_init(LevelFilter::INFO);
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.context("it should run the check tasks")
}
fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).init();
tracing::debug!("Logging initialized");
}
fn setup_config(args: Args) -> Result<Configuration> {
match (args.config_path, args.config_content) {
(Some(config_path), _) => load_config_from_file(&config_path),
(_, Some(config_content)) => parse_from_json(&config_content).context("invalid config format"),
_ => Err(anyhow::anyhow!("no configuration provided")),
}
}
fn load_config_from_file(path: &PathBuf) -> Result<Configuration> {
let file_content = std::fs::read_to_string(path).with_context(|| format!("can't read config file {path:?}"))?;
parse_from_json(&file_content).context("invalid config format")
}