Skip to content

Commit

Permalink
feat: allow to change the config.toml file path
Browse files Browse the repository at this point in the history
Now you can change the deafult location for the config file with an env
var:

```
TORRUST_IDX_BACK_CONFIG_PATH="./storage/config.toml" cargo run
```

The default location is still `./config.toml`
  • Loading branch information
josecelano committed Aug 8, 2023
1 parent 0adf373 commit 702dd14
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,11 @@ impl Configuration {
if Path::new(path).exists() {
config = config_builder.add_source(File::with_name(path)).build()?;
} else {
warn!("No config file found.");
warn!("Creating config file..");
warn!("No config file found. Creating config file ...");

Check warning on line 515 in packages/configuration/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/lib.rs#L515

Added line #L515 was not covered by tests

let config = Configuration::default();
config.save_to_file(path)?;

return Err(Error::CreatedNewConfigHalt {
location: Location::caller(),
path: path.to_string(),
Expand Down
14 changes: 12 additions & 2 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! All environment variables are prefixed with `TORRUST_TRACKER_BACK_`.
use std::env;
use std::path::Path;

use torrust_tracker_configuration::Configuration;

Expand All @@ -11,6 +12,9 @@ use torrust_tracker_configuration::Configuration;
/// Even if the file is not on the default path.
const ENV_VAR_CONFIG: &str = "TORRUST_TRACKER_CONFIG";

/// The `config.toml` file location.
pub const ENV_VAR_CONFIG_PATH: &str = "TORRUST_IDX_BACK_CONFIG_PATH";

// Default values

const ENV_VAR_DEFAULT_CONFIG_PATH: &str = "./config.toml";
Expand All @@ -37,8 +41,14 @@ pub fn initialize_configuration() -> Configuration {

Configuration::load_from_env_var(ENV_VAR_CONFIG).unwrap()

Check warning on line 42 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L42

Added line #L42 was not covered by tests
} else {
println!("Loading configuration from config file {ENV_VAR_DEFAULT_CONFIG_PATH}");
let config_path = env::var(ENV_VAR_CONFIG_PATH).unwrap_or_else(|_| ENV_VAR_DEFAULT_CONFIG_PATH.to_string());

Check warning on line 44 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L44

Added line #L44 was not covered by tests

if Path::new(&config_path).is_file(){
println!("Loading configuration from config file: `{config_path}`");

Check warning on line 47 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L46-L47

Added lines #L46 - L47 were not covered by tests
} else {
println!("Creating default config file: `{config_path}`");

Check warning on line 49 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L49

Added line #L49 was not covered by tests
}

Configuration::load_from_file(ENV_VAR_DEFAULT_CONFIG_PATH).unwrap()
Configuration::load_from_file(&config_path).expect("Error loading configuration from file")
}
}

Check warning on line 54 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L52-L54

Added lines #L52 - L54 were not covered by tests

0 comments on commit 702dd14

Please sign in to comment.