Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Command>,
}
Expand Down
16 changes: 9 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,19 +25,23 @@ pub struct Config {
pub opt_out: DashMap<String, bool>,
#[serde(rename = "adminAPIKey")]
pub admin_api_key: Option<String>,
#[serde(skip)]
config_path: Option<std::path::PathBuf>,
}

impl Config {
pub fn load() -> anyhow::Result<Self> {
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<Self> {
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(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")?;
Expand Down
Loading