diff --git a/src/main.rs b/src/main.rs index 8041f30..820d5b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,23 +3,70 @@ mod config; use clap::Parser; use std::path::PathBuf; +const CONFIG_FILENAME: &str = "bot.yml"; +const APP_NAME: &str = "deltachat-remotecontrol-bot"; +const APP_DIR: &str = APP_NAME; + /// Delta Chat bot for remote control of local network machines. #[derive(Parser)] #[command(version, about)] struct Args { /// Path to the YAML configuration file. + /// If omitted, the following paths are tried in order: + /// ./bot.yml, ~/.config/deltachat-remotecontrol-bot/bot.yml, + /// /etc/deltachat-remotecontrol-bot/bot.yml #[arg(short = 'c', long = "config")] - config: PathBuf, + config: Option, +} + +/// Returns the ordered list of default config paths to try when `-c` is not given. +/// The home-directory entry is omitted if $HOME is not set. +fn default_config_paths() -> Vec { + let mut paths = vec![PathBuf::from(CONFIG_FILENAME)]; + + if let Ok(home) = std::env::var("HOME") { + paths.push( + PathBuf::from(home) + .join(".config") + .join(APP_DIR) + .join(CONFIG_FILENAME), + ); + } + + paths.push(PathBuf::from("/etc").join(APP_DIR).join(CONFIG_FILENAME)); + + paths } fn main() { let args = Args::parse(); - let _ = match config::read_config(&args.config) { + let config_path: PathBuf = match args.config { + Some(path) => path, + None => { + let defaults = default_config_paths(); + match defaults.iter().find(|p| p.exists()) { + Some(path) => path.clone(), + None => { + eprintln!("Error: no configuration file found. Tried:"); + for path in &defaults { + eprintln!(" {}", path.display()); + } + eprintln!("Use -c to specify a config file explicitly."); + std::process::exit(1); + } + } + } + }; + + let config = match config::read_config(&config_path) { Ok(c) => c, Err(e) => { eprintln!("Error: {e}"); std::process::exit(1); } }; + + // TODO: start the bot using `config`. + let _ = config; }