Skip to content

Commit 1bab582

Browse files
committed
feat: [#656] Tracker Checker sopports env var for config
Run providing a config file path: ```text cargo run --bin tracker_checker -- --config-path "./share/default/config/tracker_checker.json" TORRUST_CHECKER_CONFIG_PATH="./share/default/config/tracker_checker.json" cargo run --bin tracker_checker ``` Run providing the configuration: ```text TORRUST_CHECKER_CONFIG=$(cat "./share/default/config/tracker_checker.json") cargo run --bin tracker_checker ```
1 parent 7f43fbd commit 1bab582

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

src/bin/tracker_checker.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
//! Program to run checks against running trackers.
22
//!
3+
//! Run providing a config file path:
4+
//!
5+
//! ```text
6+
//! cargo run --bin tracker_checker -- --config-path "./share/default/config/tracker_checker.json"
7+
//! TORRUST_CHECKER_CONFIG_PATH="./share/default/config/tracker_checker.json" cargo run --bin tracker_checker
8+
//! ```
9+
//!
10+
//! Run providing the configuration:
11+
//!
312
//! ```text
4-
//! cargo run --bin tracker_checker "./share/default/config/tracker_checker.json"
13+
//! TORRUST_CHECKER_CONFIG=$(cat "./share/default/config/tracker_checker.json") cargo run --bin tracker_checker
514
//! ```
615
use torrust_tracker::checker::app;
716

src/checker/app.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22
use std::sync::Arc;
33

4-
use anyhow::Context;
4+
use anyhow::{Context, Result};
55
use clap::Parser;
66

77
use super::config::Configuration;
@@ -12,16 +12,22 @@ use crate::checker::config::parse_from_json;
1212
#[derive(Parser, Debug)]
1313
#[clap(author, version, about, long_about = None)]
1414
struct Args {
15-
config_path: PathBuf,
15+
/// Path to the JSON configuration file.
16+
#[clap(short, long, env = "TORRUST_CHECKER_CONFIG_PATH")]
17+
config_path: Option<PathBuf>,
18+
19+
/// Direct configuration content in JSON.
20+
#[clap(env = "TORRUST_CHECKER_CONFIG", hide_env_values = true)]
21+
config_content: Option<String>,
1622
}
1723

1824
/// # Errors
1925
///
20-
/// Will return an error if it can't read or parse the configuration file.
21-
pub async fn run() -> anyhow::Result<Vec<CheckResult>> {
26+
/// Will return an error if the configuration was not provided.
27+
pub async fn run() -> Result<Vec<CheckResult>> {
2228
let args = Args::parse();
2329

24-
let config = setup_config(&args)?;
30+
let config = setup_config(args)?;
2531

2632
let console_printer = Console {};
2733

@@ -33,9 +39,16 @@ pub async fn run() -> anyhow::Result<Vec<CheckResult>> {
3339
Ok(service.run_checks().await)
3440
}
3541

36-
fn setup_config(args: &Args) -> anyhow::Result<Configuration> {
37-
let file_content =
38-
std::fs::read_to_string(&args.config_path).with_context(|| format!("can't read config file {:?}", args.config_path))?;
42+
fn setup_config(args: Args) -> Result<Configuration> {
43+
match (args.config_path, args.config_content) {
44+
(Some(config_path), _) => load_config_from_file(&config_path),
45+
(_, Some(config_content)) => parse_from_json(&config_content).context("invalid config format"),
46+
_ => Err(anyhow::anyhow!("no configuration provided")),
47+
}
48+
}
49+
50+
fn load_config_from_file(path: &PathBuf) -> Result<Configuration> {
51+
let file_content = std::fs::read_to_string(path).with_context(|| format!("can't read config file {path:?}"))?;
3952

4053
parse_from_json(&file_content).context("invalid config format")
4154
}

src/e2e/runner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ fn write_tracker_checker_config_file(config_file_path: &Path, config: &str) {
197197
/// Will panic if the config path is not a valid string.
198198
pub fn run_tracker_checker(config_path: &Path) -> io::Result<()> {
199199
info!(
200-
"Running Tracker Checker: cargo --bin tracker_checker {}",
200+
"Running Tracker Checker: cargo run --bin tracker_checker -- --config-path \"{}\"",
201201
config_path.display()
202202
);
203203

204204
let path = config_path.to_str().expect("The path should be a valid string");
205205

206206
let status = Command::new("cargo")
207-
.args(["run", "--bin", "tracker_checker", path])
207+
.args(["run", "--bin", "tracker_checker", "--", "--config-path", path])
208208
.status()?;
209209

210210
if status.success() {

0 commit comments

Comments
 (0)