Skip to content

Commit

Permalink
refactor: move loading of configuration in its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
albertotirla committed Mar 31, 2024
1 parent 2b849e6 commit ebd94b7
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions odilia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod events;
mod logging;
mod state;

use std::{fs, process::exit, sync::Arc, time::Duration};
use std::{fs, path::PathBuf, process::exit, sync::Arc, time::Duration};

use crate::cli::Args;
use crate::state::ScreenReaderState;
Expand Down Expand Up @@ -86,36 +86,7 @@ async fn main() -> eyre::Result<()> {
let tracker = TaskTracker::new();

tracing::debug!("Reading configuration");

// In order of prioritization, do environment variables, configuration via cli, then XDG_CONFIG_HOME, then /etc/odilia,
// Otherwise create it in XDG_CONFIG_HOME
//default configuration first, because that doesn't affect the priority outlined above
let figment = Figment::from(Serialized::defaults(ApplicationConfig::default()))
//environment variables
.join(Env::prefixed("ODILIA_").split("_"));
//cli override, if applicable
let figment =
if let Some(path) = args.config { figment.join(Toml::file(path)) } else { figment };
//create a config.toml file in `XDG_CONFIG_HOME`, to make it possible for the user to edit the default values, if it doesn't exist already
let xdg_dirs = xdg::BaseDirectories::with_prefix("odilia").expect(
"unable to find the odilia config directory according to the xdg dirs specification",
);

let config_path = xdg_dirs
.place_config_file("config.toml")
.expect("unable to place configuration file. Maybe your system is readonly?");

if !config_path.exists() {
let toml = toml::to_string(&ApplicationConfig::default())?;
fs::write(&config_path, toml).expect("Unable to create default config file.");
}
//next, the xdg configuration
let figment = figment
.join(Toml::file(&config_path))
//last, the configuration system wide, in /etc/odilia/config.toml
.join(Toml::file("/etc/odilia/config.toml"));
//realise the configuration and freeze it into place
let config: ApplicationConfig = figment.extract()?;
let config=load_configuration(args.config)?;
tracing::debug!(?config, "configuration loaded successfully");

// Make sure applications with dynamic accessibility support do expose their AT-SPI2 interfaces.

Check warning on line 92 in odilia/src/main.rs

View check run for this annotation

Codecov / codecov/patch

odilia/src/main.rs#L88-L92

Added lines #L88 - L92 were not covered by tests
Expand Down Expand Up @@ -181,3 +152,36 @@ async fn main() -> eyre::Result<()> {
.wrap_err("can not process interrupt signal");
Ok(())
}

fn load_configuration(cli_overide: Option<PathBuf>) -> Result<ApplicationConfig,eyre::Report> {
// In order, do environment variables, a configuration file specified via cli, XDG_CONFIG_HOME, the usual location for system wide configuration(/etc/odilia/config.toml)
// If XDG_CONFIG_HOME based configuration wasn't found, create one with default values for the user to alter, for the next run of odilia
//default configuration first, because that doesn't affect the priority outlined above
let figment = Figment::from(Serialized::defaults(ApplicationConfig::default()))
//environment variables
.join(Env::prefixed("ODILIA_").split("_"));

Check warning on line 162 in odilia/src/main.rs

View check run for this annotation

Codecov / codecov/patch

odilia/src/main.rs#L156-L162

Added lines #L156 - L162 were not covered by tests
//cli override, if applicable
let figment =
if let Some(path) = cli_overide { figment.join(Toml::file(path)) } else { figment };

Check warning on line 165 in odilia/src/main.rs

View check run for this annotation

Codecov / codecov/patch

odilia/src/main.rs#L164-L165

Added lines #L164 - L165 were not covered by tests
//create a config.toml file in `XDG_CONFIG_HOME`, to make it possible for the user to edit the default values, if it doesn't exist already
let xdg_dirs = xdg::BaseDirectories::with_prefix("odilia").expect(
"unable to find the odilia config directory according to the xdg dirs specification",
);

let config_path = xdg_dirs
.place_config_file("config.toml")
.expect("unable to place configuration file. Maybe your system is readonly?");

if !config_path.exists() {
let toml = toml::to_string(&ApplicationConfig::default())?;
fs::write(&config_path, toml).expect("Unable to create default config file.");
}

Check warning on line 178 in odilia/src/main.rs

View check run for this annotation

Codecov / codecov/patch

odilia/src/main.rs#L167-L178

Added lines #L167 - L178 were not covered by tests
//next, the xdg configuration
let figment = figment
.join(Toml::file(&config_path))
//last, the configuration system wide, in /etc/odilia/config.toml
.join(Toml::file("/etc/odilia/config.toml"));
//realise the configuration and freeze it into place
Ok(figment.extract()?)

Check warning on line 185 in odilia/src/main.rs

View check run for this annotation

Codecov / codecov/patch

odilia/src/main.rs#L180-L185

Added lines #L180 - L185 were not covered by tests

}

Check warning on line 187 in odilia/src/main.rs

View check run for this annotation

Codecov / codecov/patch

odilia/src/main.rs#L187

Added line #L187 was not covered by tests

0 comments on commit ebd94b7

Please sign in to comment.