Allow driver_url to be configurable

This commit is contained in:
Kienan Stewart 2022-09-24 10:22:17 -04:00
parent 6fd8bf4304
commit 17a9445796
2 changed files with 20 additions and 17 deletions

View File

@ -6,9 +6,19 @@ pub struct Conf {
pub job_dir: String,
pub output_dir: String,
pub check_interval: Duration,
pub driver_url: String,
}
impl Conf {
pub fn get_default_conf() -> Conf {
return Conf {
job_dir: String::from_str("jobs.d").unwrap(),
output_dir: String::from_str("results.d").unwrap(),
check_interval: Duration::new(15*60, 0),
driver_url: String::from_str("http://localhost:4444").unwrap(),
};
}
pub fn update_from_file(&mut self, f: &str) {
let path = std::path::Path::new(f);
let mut file = match std::fs::File::open(&path) {
@ -58,6 +68,10 @@ impl Conf {
println!("{} changed from '{}' to '{}' by line in '{}'", key, self.check_interval.as_secs(), value, path.display());
self.check_interval = Duration::new(converted_value, 0);
},
"driver_url" => {
println!("{} changed from '{}' to '{}' by line in '{}'", key, self.driver_url, value, path.display());
self.driver_url = String::from_str(value).unwrap();
}
_ => {
println!("Unknown key '{}' in file '{}'", key, path.display());
}
@ -76,14 +90,6 @@ mod tests {
use super::*;
fn get_default_conf() -> Conf {
return Conf {
job_dir: String::from_str("jobs.d").unwrap(),
output_dir: String::from_str("results.d").unwrap(),
check_interval: Duration::new(15*60, 0),
};
}
fn write_conf_to_temp_file(content: &str) -> NamedTempFile {
let mut f = NamedTempFile::new().unwrap();
f.write_all(content.as_bytes());
@ -92,28 +98,30 @@ mod tests {
#[test]
fn test_not_existing() {
let mut conf = get_default_conf();
let mut conf = Conf::get_default_conf();
conf.update_from_file("/not/a/real/file");
}
#[test]
fn test_set_all_values() {
let mut conf = get_default_conf();
let mut conf = Conf::get_default_conf();
let tf = write_conf_to_temp_file(r#"
job_dir = /value
output_dir=/var/lib/output
check_interval=3600
driver_url = https://example.test:6666
"#);
let f = tf.path().to_str().unwrap();
conf.update_from_file(f);
assert!(String::from_str("/value").unwrap().eq(&conf.job_dir));
assert!(String::from_str("/var/lib/output").unwrap().eq(&conf.output_dir));
assert!(String::from_str("https://example.test:6666").unwrap().eq(&conf.driver_url));
assert_eq!(3600, conf.check_interval.as_secs());
}
#[test]
fn test_non_numeric_check_interval_fails() {
let mut conf = get_default_conf();
let mut conf = Conf::get_default_conf();
let tf = write_conf_to_temp_file(r#"
check_interval=d3600
"#);

View File

@ -45,12 +45,7 @@ impl ThreadJob<'_> {
}
fn main() {
let mut conf = Conf {
job_dir: String::from_str("jobs.d").unwrap(),
output_dir: String::from_str("results.d").unwrap(),
check_interval: Duration::new(15*60, 0),
};
let mut conf = Conf::get_default_conf();
conf.update_from_file("/etc/haunter/haunter.conf");
let mut args = env::args();