Skip to content

Commit

Permalink
Refactor config to get default settings when not settings were found
Browse files Browse the repository at this point in the history
  • Loading branch information
tbsklg committed Aug 23, 2024
1 parent 76e34da commit d694bc7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
26 changes: 18 additions & 8 deletions cli-client/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

#[derive(serde::Deserialize)]
pub struct Settings {
pub use_remote: bool,
pub remote: Option<RemoteSettings>,
pub local: Option<LocalSettings>,
}
Expand All @@ -20,6 +21,7 @@ pub struct LocalSettings {
impl Default for Settings {
fn default() -> Self {
Self {
use_remote: false,
remote: None,
local: {
Some(LocalSettings {
Expand All @@ -32,15 +34,18 @@ impl Default for Settings {
}
}

pub fn get_configuration(path: std::path::PathBuf) -> Result<Settings, config::ConfigError> {
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::<Settings>()
match settings {
Ok(settings) => settings.try_deserialize::<Settings>().unwrap_or_default(),
Err(_) => Settings::default()
}
}

#[cfg(test)]
Expand All @@ -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,
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion cli-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion cli-client/tests/fixtures/valid_config.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d694bc7

Please sign in to comment.