1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use dotenv;
use env_logger;
use std::env;

fn build_env_file_heirarchy(environment: String) -> Vec<String> {
    let mut heirarchy: Vec<String> = environment.split('.').map(String::from).collect();
    let length = heirarchy.len();

    for i in 0..length {
        for j in i + 1..length {
            heirarchy[i] = format!("{}.{}", heirarchy[j], heirarchy[i]);
        }
    }

    heirarchy.reverse();
    heirarchy
}

fn load_env_files() {
    let environment = env::var("REIGN_ENV").unwrap_or_else(|_| "development".to_string());

    dotenv::from_filename(".env").ok();
    dotenv::from_filename(".env.local").ok();

    for item in build_env_file_heirarchy(environment) {
        dotenv::from_filename(&format!(".env.{}", item)).ok();
        dotenv::from_filename(&format!(".env.{}.local", item)).ok();
    }
}

pub fn boot() {
    load_env_files();

    // TODO: Allow custom loggers by adding an option to exclude this call
    env_logger::from_env(env_logger::Env::default().default_filter_or("info"))
        .format_timestamp(None)
        .init();
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_env_file_heirarchy() {
        assert_eq!(
            build_env_file_heirarchy(String::from("joe.qa.staging")),
            ["staging", "staging.qa", "staging.qa.joe"]
        );
    }
}