diff --git a/cli-client/src/configuration.rs b/cli-client/src/configuration.rs index 01afc9b..9f8e744 100644 --- a/cli-client/src/configuration.rs +++ b/cli-client/src/configuration.rs @@ -2,6 +2,7 @@ use std::path::PathBuf; #[derive(serde::Deserialize)] pub struct Settings { + pub use_remote: bool, pub remote: Option, pub local: Option, } @@ -20,6 +21,7 @@ pub struct LocalSettings { impl Default for Settings { fn default() -> Self { Self { + use_remote: false, remote: None, local: { Some(LocalSettings { @@ -32,15 +34,18 @@ impl Default for Settings { } } -pub fn get_configuration(path: std::path::PathBuf) -> Result { +pub fn get_configuration(path: std::path::PathBuf) -> Settings { let settings = config::Config::builder() .add_source(config::File::new( path.to_str().unwrap(), config::FileFormat::Yaml, )) - .build()?; + .build(); - settings.try_deserialize::() + match settings { + Ok(settings) => settings.try_deserialize::().unwrap_or_default(), + Err(_) => Settings::default() + } } #[cfg(test)] @@ -51,20 +56,24 @@ mod tests { #[test] fn parse_valid_config() { let configuration = - get_configuration(PathBuf::from("tests/fixtures/valid_config.yaml")).unwrap(); + get_configuration(PathBuf::from("tests/fixtures/valid_config.yaml")); + assert_eq!(configuration.use_remote, true); assert_eq!(configuration.remote.as_ref().unwrap().api_key, "abc"); assert_eq!( configuration.remote.as_ref().unwrap().base_url, "https://example.com" ); + assert_eq!( + configuration.local.unwrap().db_path, + PathBuf::from("/home/user/.strikes") + ); } #[test] fn parse_default_config() { std::env::set_var("HOME", "/home/user"); - let configuration = get_configuration(PathBuf::from("tests/fixtures/empty_config.yaml")) - .unwrap_or_default(); + let configuration = get_configuration(PathBuf::from("tests/fixtures/empty_config.yaml")); assert_eq!( configuration.local.unwrap().db_path, @@ -73,8 +82,9 @@ mod tests { } #[test] - #[should_panic] fn parse_invalid_config() { - get_configuration(PathBuf::from("tests/fixtures/invalid_config.yaml")).unwrap(); + let configuration = get_configuration(PathBuf::from("tests/fixtures/invalid_config.yaml")); + + assert_eq!(configuration.use_remote, false); } } diff --git a/cli-client/src/main.rs b/cli-client/src/main.rs index 6d20723..4541cbd 100644 --- a/cli-client/src/main.rs +++ b/cli-client/src/main.rs @@ -46,7 +46,7 @@ async fn main() { let home = &std::env::var("HOME").unwrap(); let config_path = PathBuf::from(home).join(".strikes/configuration.yaml"); - let config = get_configuration(args.config_path.unwrap_or(config_path)).unwrap_or_default(); + let config = get_configuration(args.config_path.unwrap_or(config_path)); // check_health(config.base_url, config.api_key).await; let db_path = args.db_path.unwrap_or(config.local.map_or_else( diff --git a/cli-client/tests/fixtures/valid_config.yaml b/cli-client/tests/fixtures/valid_config.yaml index dccf0e2..ebf64c8 100644 --- a/cli-client/tests/fixtures/valid_config.yaml +++ b/cli-client/tests/fixtures/valid_config.yaml @@ -1,5 +1,6 @@ +use_remote: true remote: api_key: abc base_url: https://example.com local: - db_path: /Home/.strikes + db_path: /home/user/.strikes