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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use configparser::ini::Ini;
use std::collections::HashMap;
use std::error::Error;

fn load_config(
) -> Result<HashMap<String, HashMap<String, Option<String>>>, Box<dyn std::error::Error>> {
    let mut config = Ini::new();
    let map = config.load("/etc/fll-scoring/config.ini")?;

    Ok(map)
}

pub fn load_service_config(
    service_name: String,
) -> Result<HashMap<String, Option<String>>, Box<dyn std::error::Error>> {
    let config = load_config()?;
    let service_config = config[&service_name].clone();

    Ok(service_config)
}

pub fn load_global_config() -> Result<HashMap<String, Option<String>>, Box<dyn std::error::Error>> {
    let config = load_config()?;
    let global_config = config["fll-scoring"].clone();

    Ok(global_config)
}

/// Gets a global configuration value
pub fn get_global_value(key: &str, panic: bool) -> Result<String, Box<dyn Error>> {
    let config = load_global_config()?;
    let mut value = String::new();
    if panic {
        value = match config.get(key) {
            Some(opt) => match opt {
                Some(val) => val.clone(),
                None => {
                    panic!("{} not set in global configuration!", key);
                }
            },
            None => panic!("{} not set in global config!", key),
        }
    } else {
        if let Some(opt) = config.get(key) {
            if let Some(val) = opt {
                value = val.clone();
            }
        }
    }

    Ok(value)
}

/// Gets a service-specific config value, optionally panic!ing if it's not established
pub fn get_service_config_value(
    service_name: &str,
    config_key: &str,
    panic: bool,
) -> Result<String, Box<dyn Error>> {
    let config = load_service_config(service_name.to_string())?;
    let mut value = String::new();
    if panic {
        value = match config.get(config_key) {
            Some(opt) => match opt {
                Some(val) => val.clone(),
                None => {
                    panic!("{} not set in {} config!", config_key, service_name);
                }
            },
            None => {
                panic!("{} not set in {} config!", config_key, service_name);
            }
        }
    } else {
        if let Some(opt) = config.get(config_key) {
            if let Some(val) = opt {
                value = val.clone();
            }
        }
    }

    Ok(value)
}