Skip to content

Commit 0252f30

Browse files
committed
feat: allow users not to provide config option with default values
Now, you are able to run the tracler like this: ``` TORRUST_TRACKER_CONFIG="" cargo run ``` Default values will be used for the missing values in the provided configuration. In that case, none of the values have been provided, so it will use default values for all options.
1 parent 43942ce commit 0252f30

File tree

1 file changed

+20
-2
lines changed
  • packages/configuration/src/v1

1 file changed

+20
-2
lines changed

packages/configuration/src/v1/mod.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ use std::fs;
239239
use std::net::IpAddr;
240240
use std::str::FromStr;
241241

242-
use figment::providers::{Env, Format, Toml};
242+
use figment::providers::{Env, Format, Serialized, Toml};
243243
use figment::Figment;
244244
use serde::{Deserialize, Serialize};
245245
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};
@@ -393,7 +393,7 @@ impl Configuration {
393393
///
394394
/// Will return `Err` if the environment variable does not exist or has a bad configuration.
395395
pub fn load(info: &Info) -> Result<Configuration, Error> {
396-
let figment = Figment::new()
396+
let figment = Figment::from(Serialized::defaults(Configuration::default()))
397397
.merge(Toml::string(&info.tracker_toml))
398398
.merge(Env::prefixed("TORRUST_TRACKER__").split("__"));
399399

@@ -523,6 +523,24 @@ mod tests {
523523
assert_eq!(contents, default_config_toml());
524524
}
525525

526+
#[test]
527+
fn configuration_should_use_the_default_values_when_an_empty_configuration_is_provided_by_the_user() {
528+
figment::Jail::expect_with(|_jail| {
529+
let empty_configuration = String::new();
530+
531+
let info = Info {
532+
tracker_toml: empty_configuration,
533+
api_admin_token: None,
534+
};
535+
536+
let configuration = Configuration::load(&info).expect("Could not load configuration from file");
537+
538+
assert_eq!(configuration, Configuration::default());
539+
540+
Ok(())
541+
});
542+
}
543+
526544
#[test]
527545
fn configuration_should_be_loaded_from_a_toml_config_file() {
528546
figment::Jail::expect_with(|_jail| {

0 commit comments

Comments
 (0)