diff --git a/src/args.rs b/src/args.rs index 14076d9..c803f0e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -3,6 +3,9 @@ use clap::{Parser, Subcommand}; #[derive(Parser)] #[clap(author, version, about, long_about = None)] pub struct Args { + /// Path to the config file + #[clap(default_value = "config.json", long = "config")] + pub config_path: std::path::PathBuf, #[clap(subcommand)] pub subcommand: Option, } diff --git a/src/config.rs b/src/config.rs index 4d8b890..c270e80 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,8 +5,6 @@ use std::fs; use std::{collections::HashSet, sync::RwLock}; use tracing::info; -const CONFIG_FILE_NAME: &str = "config.json"; - #[derive(Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Config { @@ -27,19 +25,23 @@ pub struct Config { pub opt_out: DashMap, #[serde(rename = "adminAPIKey")] pub admin_api_key: Option, + #[serde(skip)] + config_path: Option, } impl Config { - pub fn load() -> anyhow::Result { - let contents = fs::read_to_string(CONFIG_FILE_NAME) - .with_context(|| format!("Failed to load config from {CONFIG_FILE_NAME}"))?; - serde_json::from_str(&contents).context("Config deserializtion error") + pub fn load(config_path: &std::path::Path) -> anyhow::Result { + let contents = fs::read_to_string(config_path) + .with_context(|| format!("Failed to load config from {}", config_path.display()))?; + let mut s: Self = serde_json::from_str(&contents).context("Config deserializtion error")?; + s.config_path = Some(config_path.to_owned()); + Ok(s) } pub fn save(&self) -> anyhow::Result<()> { info!("Updating config"); let json = serde_json::to_string_pretty(self)?; - fs::write(CONFIG_FILE_NAME, json)?; + fs::write(self.config_path.as_ref().expect("config path should always be available"), json)?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index b938cf4..8aac305 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,9 @@ async fn main() -> anyhow::Result<()> { .with_ansi(use_ansi) .init(); - let config = Config::load()?; + let args = Args::parse(); + + let config = Config::load(&args.config_path)?; let mut db = clickhouse::Client::default() .with_url(&config.clickhouse_url) .with_database(&config.clickhouse_db) @@ -72,8 +74,6 @@ async fn main() -> anyhow::Result<()> { db = db.with_password(password); } - let args = Args::parse(); - setup_db(&db, &config.clickhouse_db) .await .context("Could not run DB migrations")?;