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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,4 +38,8 @@ pub struct Cli {
/// Set the log level
#[arg(short = 'L', long)]
pub log_level: Option<LogLevel>,

/// Use a custom config file
#[arg(short = 'c', long, value_name = "FILE")]
pub config: Option<PathBuf>,
}
18 changes: 10 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ pub struct Config {

impl Config {
#[instrument]
pub fn new() -> anyhow::Result<Self> {
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<PathBuf>) -> anyhow::Result<Self> {
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!(
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down