Skip to content

Commit 324c37b

Browse files
committed
Add argument for config path
1 parent e6726f5 commit 324c37b

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use clap::{Parser, Subcommand};
33
#[derive(Parser)]
44
#[clap(author, version, about, long_about = None)]
55
pub struct Args {
6+
/// Path to the config file
7+
#[clap(default_value = "config.json", long = "config")]
8+
pub config_path: std::path::PathBuf,
69
#[clap(subcommand)]
710
pub subcommand: Option<Command>,
811
}

src/config.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::fs;
55
use std::{collections::HashSet, sync::RwLock};
66
use tracing::info;
77

8-
const CONFIG_FILE_NAME: &str = "config.json";
9-
108
#[derive(Serialize, Deserialize)]
119
#[serde(rename_all = "camelCase")]
1210
pub struct Config {
@@ -27,19 +25,23 @@ pub struct Config {
2725
pub opt_out: DashMap<String, bool>,
2826
#[serde(rename = "adminAPIKey")]
2927
pub admin_api_key: Option<String>,
28+
#[serde(skip)]
29+
config_path: Option<std::path::PathBuf>,
3030
}
3131

3232
impl Config {
33-
pub fn load() -> anyhow::Result<Self> {
34-
let contents = fs::read_to_string(CONFIG_FILE_NAME)
35-
.with_context(|| format!("Failed to load config from {CONFIG_FILE_NAME}"))?;
36-
serde_json::from_str(&contents).context("Config deserializtion error")
33+
pub fn load(config_path: &std::path::Path) -> anyhow::Result<Self> {
34+
let contents = fs::read_to_string(config_path)
35+
.with_context(|| format!("Failed to load config from {}", config_path.display()))?;
36+
let mut s: Self = serde_json::from_str(&contents).context("Config deserializtion error")?;
37+
s.config_path = Some(config_path.to_owned());
38+
Ok(s)
3739
}
3840

3941
pub fn save(&self) -> anyhow::Result<()> {
4042
info!("Updating config");
4143
let json = serde_json::to_string_pretty(self)?;
42-
fs::write(CONFIG_FILE_NAME, json)?;
44+
fs::write(self.config_path.as_ref().expect("config path should always be available"), json)?;
4345

4446
Ok(())
4547
}

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ async fn main() -> anyhow::Result<()> {
5858
.with_ansi(use_ansi)
5959
.init();
6060

61-
let config = Config::load()?;
61+
let args = Args::parse();
62+
63+
let config = Config::load(&args.config_path)?;
6264
let mut db = clickhouse::Client::default()
6365
.with_url(&config.clickhouse_url)
6466
.with_database(&config.clickhouse_db)
@@ -72,8 +74,6 @@ async fn main() -> anyhow::Result<()> {
7274
db = db.with_password(password);
7375
}
7476

75-
let args = Args::parse();
76-
7777
setup_db(&db, &config.clickhouse_db)
7878
.await
7979
.context("Could not run DB migrations")?;

0 commit comments

Comments
 (0)