From 342a03d07e1734bbb347865d1d383cc371638efc Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 18 Nov 2020 22:45:28 +0000 Subject: [PATCH] default config lookup Signed-off-by: Ian Jackson --- src/bin/otter.rs | 27 +++++++++++++++++++++++++++ src/config.rs | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 8d148149..14398df2 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -279,6 +279,33 @@ fn main() { }) })?; let gaccount = gaccount.unwrap_or_else(|| account.clone()); + let config_filename = config_filename.or_else(||{ + match match env::current_exe() + .map(|p| p.to_str().map(|s| s.to_string())) + { + Err(e) => Err(format!( + "could not find current executable ({})", &e + )), + Ok(None) => Err(format!( + "current executable has non-UTF8 filename!" + )), + Ok(Some(basedir)) if basedir.rsplit('/').nth(1) == Some("bin") => Ok( + format!("{}/etc/{}", basedir, DEFAULT_CONFIG_FILENAME) + ), + Ok(_) => Err(format!( + "current executable is not in a directory called bin" + )), + } { + Err(whynot) => { + if verbose > 1 { + eprintln!("{}: looking for {} in current directory", + &whynot, DEFAULT_CONFIG_FILENAME); + } + None + }, + Ok(f) => Some(f), + } + }); let config = Thunk::new(||{ ServerConfig::read(config_filename.as_ref().map(String::as_str)) .context("read config file")?; diff --git a/src/config.rs b/src/config.rs index 5c5303a5..7498dfa9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use crate::imports::*; -const DEFAULT_CONFIG_FILENAME : &str = "server.toml"; +pub const DEFAULT_CONFIG_FILENAME : &str = "server.toml"; const DEFAULT_SAVE_DIRECTORY : &str = "save"; const DEFAULT_COMMAND_SOCKET : &str = "command.socket"; // in save dir -- 2.30.2