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
84
85
86
87
88
89
90
91
use log::LevelFilter;
use crate::read_text_file;
use std::{
io::{Error, ErrorKind},
path::Path,
};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
pub krecik_root: Option<String>,
pub log_file: Option<String>,
pub log_level: Option<String>,
pub ok_message: Option<String>,
pub notifiers: Option<Vec<Notifiers>>,
pub success_emoji: Option<String>,
pub failure_emoji: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Notifiers {
pub name: String,
pub slack_webhook: String,
}
impl Config {
pub fn load() -> Config {
let config_paths = [
"/etc/krecik/krecik.conf",
"/Services/Krecik/service.conf",
"/Projects/krecik/krecik.conf",
"krecik.conf",
];
let config: String = config_paths
.iter()
.filter(|file| Path::new(file).exists())
.take(1)
.cloned()
.collect();
read_text_file(&config)
.and_then(|file_contents| {
serde_json::from_str(&*file_contents).map_err(|err| {
let config_error = Error::new(ErrorKind::InvalidInput, err.to_string());
error!(
"Configuration error: {} in file: {}",
err.to_string(),
config
);
config_error
})
})
.unwrap_or_default()
}
pub fn get_log_level(&self) -> LevelFilter {
let level = self.log_level.clone().unwrap_or_default();
match &level[..] {
"OFF" => LevelFilter::Off,
"ERROR" => LevelFilter::Error,
"WARN" => LevelFilter::Warn,
"INFO" => LevelFilter::Info,
"DEBUG" => LevelFilter::Debug,
"TRACE" => LevelFilter::Trace,
_ => LevelFilter::Info,
}
}
}