Skip to content

Commit

Permalink
Made "Settings.toml" optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Nov 18, 2018
1 parent ec73ff5 commit 18e4e90
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use actix_web::{server as actix_server, App};
use tera::{Tera};
use rlua::prelude::*;
use std::collections::HashMap;
use std::path::Path;

pub mod bindings;
pub mod logger;
Expand Down Expand Up @@ -158,27 +159,34 @@ impl ApplicationBuilder {

let mut settings = config::Config::new();

match settings.merge(config::File::with_name("Settings.toml")) {
Err(err) => {
if let config::ConfigError::Foreign(err) = &err {
use std::io::{Error as IoErr, ErrorKind};

if let Some(err) = err.downcast_ref::<IoErr>() {
if let ErrorKind::NotFound = err.kind() {
println!("Error: Torchbear needs an app to run. Change to the directory containing your application and run torchbear again.");
std::process::exit(1);
let setting_file = Path::new("Settings.toml");
if setting_file.exists() {
match settings.merge(config::File::with_name("Settings.toml")) {
Err(err) => {
if let config::ConfigError::Foreign(err) = &err {
use std::io::{Error as IoErr, ErrorKind};

if let Some(err) = err.downcast_ref::<IoErr>() {
if let ErrorKind::NotFound = err.kind() {
println!("Error: Torchbear needs an app to run. Change to the directory containing your application and run torchbear again.");
std::process::exit(1);
};
};
};
};

println!("Error opening Settings.toml: {}", err);
std::process::exit(1);
},
_ => ()
};
settings.merge(config::Environment::with_prefix("torchbear")).unwrap();
println!("Error opening Settings.toml: {}", err);
std::process::exit(1);
},
_ => ()
};
settings.merge(config::Environment::with_prefix("torchbear")).unwrap();
}

let hashmap = settings.try_into::<HashMap<String, String>>().unwrap();
let hashmap = if setting_file.exists() {
settings.try_into::<HashMap<String, String>>().unwrap()
} else {
HashMap::new()
};

fn get_or (map: &HashMap<String, String>, key: &str, val: &str) -> String {
map.get(key).map(|s| s.to_string()).unwrap_or(String::from(val))
Expand Down

0 comments on commit 18e4e90

Please sign in to comment.