Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pvshvp-oss committed May 8, 2024
1 parent d4cfd3b commit 298cba2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ paxy = { path = "paxy" }
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = "0.3"
log = "0.4"

# Configuration
figment = "0.10"
Expand All @@ -57,7 +58,7 @@ serde-aux = "4.5"
serde_yaml = "0.9"
tracing-serde = "0.1"
speedy = "0.8"
log = "0.4"
itertools = "0.12"

# Internationalization
fluent = "0.16"
Expand Down
3 changes: 2 additions & 1 deletion paxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ edition.workspace = true
tracing = { workspace = true }
tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true }
log = { workspace = true, features = ["serde"] }

# Configuration
figment = { workspace = true, features = ["toml", "json", "yaml", "env"] }
Expand All @@ -37,7 +38,7 @@ serde-aux = { workspace = true }
serde_yaml = { workspace = true }
tracing-serde = { workspace = true }
speedy = { workspace = true }
log = { workspace = true, features = ["serde"] }
itertools = { workspace = true }

# Internationalization
fluent = { workspace = true }
Expand Down
32 changes: 20 additions & 12 deletions paxy/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ where
G: ui::GlobalArguments,
<G as ui::GlobalArguments>::L: clap_verbosity_flag::LogLevel,
{
let mut candidate_config_filepath_stubs: Vec<PathBuf> = Vec::new();
let mut candidate_config_filepaths: Vec<PathBuf> = Vec::new();

// Global directories
#[cfg(target_family = "unix")]
candidate_config_filepath_stubs.extend(["/etc/xdg".into(), "/etc".into()]);
candidate_config_filepaths.extend(["/etc/xdg".into(), "/etc".into()]);
#[cfg(target_os = "windows")]
candidate_config_filepath_stubs.extend([""]);

// Local directories
candidate_config_filepath_stubs.push(
candidate_config_filepaths.push(
directories::BaseDirs::new()
.context(RetreiveConfigUserAppBaseDirectoriesSnafu {})?
.config_dir()
.to_path_buf(),
);

// Append filename to create filepath stubs
candidate_config_filepath_stubs
candidate_config_filepaths
.iter_mut()
.for_each(|f| f.push(*app::APP_NAME));

candidate_config_filepaths = candidate_config_filepaths
.iter()
.cartesian_product(["toml", "TOML", "json", "JSON", "yaml", "YAML", "yml", "YML"]);

// Initialize configuration with app-wide defaults
let mut figment = Figment::from(Config::default());
let mut config = Config::new();

// Merge configuration values from global and local filepaths
figment = candidate_config_filepath_stubs
.iter()
.fold(figment, move |figment, candidate_config_filepath_stub| {
admerge_from_stub(candidate_config_filepath_stub, figment)
});
config = config.with_overriding_files(candidate_config_filepaths);

// Merge configuration values from environment variables
figment = figment.admerge(Env::prefixed(&format!("{}_", *app::APP_NAME)));
config = config.with_overriding_env(&format!("{}_", *app::APP_NAME));

// Merge configuration values from additional config filepaths (usually
// specified through CLI)
Expand Down Expand Up @@ -145,6 +145,13 @@ impl Config {
self
}

pub fn with_overriding_filepath_stub<P: Into<PathBuf>>(&mut self, filepath_stub: P) -> &mut Self {
let filepath_stub: PathBuf = filepath_stub.into();


self
}

pub fn with_overriding_files<P, I>(&mut self, filepaths: I) -> &mut Self
where
P: AsRef<Path>,
Expand All @@ -155,7 +162,7 @@ impl Config {
self
}

pub fn with_overriding_env<S: AsRef<str>>(prefix: S) -> &mut Self {
pub fn with_overriding_env<S: AsRef<str>>(&mut self, prefix: S) -> &mut Self {
let prefix = prefix.as_ref();
self.figment = self
.figment
Expand Down Expand Up @@ -233,6 +240,7 @@ use figment::{
Profile,
Provider,
};
use itertools::Itertools;
use log::LevelFilter;
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};
Expand Down

0 comments on commit 298cba2

Please sign in to comment.