From 17a94457967c306f415ffbba7962ce295c873fdf Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Sat, 24 Sep 2022 10:22:17 -0400 Subject: [PATCH] Allow driver_url to be configurable --- src/conf.rs | 30 +++++++++++++++++++----------- src/main.rs | 7 +------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/conf.rs b/src/conf.rs index 4b7202c..9bb13b1 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -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 "#); diff --git a/src/main.rs b/src/main.rs index 31bbcfa..a70b9b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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();