chiark / gitweb /
default config lookup
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 18 Nov 2020 22:45:28 +0000 (22:45 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 18 Nov 2020 22:45:28 +0000 (22:45 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs
src/config.rs

index 8d148149da50fede876c2d3ab778e6d6cfb4849c..14398df2dfc65fe882a3cadf91322c83c308e069 100644 (file)
@@ -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")?;
index 5c5303a5fdac7e843a7012a5e616931f2a2c8605..7498dfa9ecd9f04c0ae3ea878cd7de6668f40589 100644 (file)
@@ -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