add default config paths

This commit is contained in:
2026-03-19 11:51:53 +03:00
parent 5d45b97a70
commit 48ef5cb3b8

View File

@@ -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<PathBuf>,
}
/// 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<PathBuf> {
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 <path> 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;
}