From 14d36fba2d6e85591fd57eef83faf384763dd7a9 Mon Sep 17 00:00:00 2001 From: borbitter Date: Sun, 19 Apr 2026 16:34:04 +0200 Subject: [PATCH] add optional config path arg --- README.md | 5 +++-- src/cli.rs | 6 ++++++ src/config.rs | 18 ++++++++++-------- src/main.rs | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 90ad980..3c2da0e 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,9 @@ A desktop notification server, like [Dunst](https://dunst-project.org/) ### Configuration -The binary looks for the config file `bato.toml` located in -`$XDG_CONFIG_HOME/bato/` (default to `$HOME/.config/bato/`).\ +By default, bato looks for the config file `~/.config/bato/bato.toml` +( $XDG_CONFIG_HOME/bato/bato.toml ).\ +To use a custom file, run with the `--config` flag.\ If the config file is not found, bato prints an error and exits. All config options are detailed [here](https://github.com/doums/bato/blob/master/bato.toml). diff --git a/src/cli.rs b/src/cli.rs index 0357845..6e2effa 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,6 +2,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +use std::path::PathBuf; + use clap::{Parser, ValueEnum}; use serde::Deserialize; @@ -36,4 +38,8 @@ pub struct Cli { /// Set the log level #[arg(short = 'L', long)] pub log_level: Option, + + /// Use a custom config file + #[arg(short = 'c', long, value_name = "FILE")] + pub config: Option, } diff --git a/src/config.rs b/src/config.rs index d802a0a..dbb7094 100644 --- a/src/config.rs +++ b/src/config.rs @@ -61,15 +61,17 @@ pub struct Config { impl Config { #[instrument] - pub fn new() -> anyhow::Result { - let home = env::var("HOME")?; - let mut config_dir = env::var(XDG_CONFIG_HOME) - .map(PathBuf::from) - .unwrap_or_else(|_| Path::new(&home).join(".config")); - config_dir.push(APP_DIR); - util::check_dir_or_create(&config_dir)?; + pub fn new(config_path: Option) -> anyhow::Result { + let config_file = config_path.unwrap_or({ + let home = env::var("HOME")?; + let mut config_dir = env::var(XDG_CONFIG_HOME) + .map(PathBuf::from) + .unwrap_or_else(|_| Path::new(&home).join(".config")); + config_dir.push(APP_DIR); + util::check_dir_or_create(&config_dir)?; - let config_file = config_dir.join(CONFIG_FILE); + config_dir.join(CONFIG_FILE) + }); info!("config file: {:?}", config_file); let content = fs::read_to_string(&config_file).map_err(|e| { let error = format!( diff --git a/src/main.rs b/src/main.rs index 78def8a..d0f183d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ fn main() -> Result<()> { signal::catch_signals()?; - let config = Config::new()?; + let config = Config::new(cli.config)?; trace!("{:#?}", config); debug!("tick rate {}s", config.tick_rate); let tick = Duration::from_secs(config.tick_rate as u64);