cleanup Config::load()

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-04 19:10:20 +00:00 committed by strawberry
parent 2a59a56eaa
commit f67cfcd535
2 changed files with 18 additions and 26 deletions

View file

@ -1,3 +1,6 @@
pub mod check;
pub mod proxy;
use std::{ use std::{
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
fmt, fmt,
@ -22,10 +25,7 @@ use url::Url;
pub use self::check::check; pub use self::check::check;
use self::proxy::ProxyConfig; use self::proxy::ProxyConfig;
use crate::{error::Error, utils::sys, Err, Result}; use crate::{err, error::Error, utils::sys, Result};
pub mod check;
pub mod proxy;
/// all the config options for conduwuit /// all the config options for conduwuit
#[config_example_generator] #[config_example_generator]
@ -441,34 +441,26 @@ const DEPRECATED_KEYS: &[&str; 9] = &[
impl Config { impl Config {
/// Pre-initialize config /// Pre-initialize config
pub fn load(paths: &Option<Vec<PathBuf>>) -> Result<Figment> { pub fn load(paths: Option<&[PathBuf]>) -> Result<Figment> {
let raw_config = if let Some(config_file_env) = Env::var("CONDUIT_CONFIG") { let paths_files = paths.into_iter().flatten().map(Toml::file);
Figment::new().merge(Toml::file(config_file_env).nested())
} else if let Some(config_file_arg) = Env::var("CONDUWUIT_CONFIG") {
Figment::new().merge(Toml::file(config_file_arg).nested())
} else if let Some(config_file_args) = paths {
let mut figment = Figment::new();
for config in config_file_args { let envs = [Env::var("CONDUIT_CONFIG"), Env::var("CONDUWUIT_CONFIG")];
figment = figment.merge(Toml::file(config).nested()); let envs_files = envs.into_iter().flatten().map(Toml::file);
}
figment let config = envs_files
} else { .chain(paths_files)
Figment::new() .fold(Figment::new(), |config, file| config.merge(file.nested()))
};
Ok(raw_config
.merge(Env::prefixed("CONDUIT_").global().split("__")) .merge(Env::prefixed("CONDUIT_").global().split("__"))
.merge(Env::prefixed("CONDUWUIT_").global().split("__"))) .merge(Env::prefixed("CONDUWUIT_").global().split("__"));
Ok(config)
} }
/// Finalize config /// Finalize config
pub fn new(raw_config: &Figment) -> Result<Self> { pub fn new(raw_config: &Figment) -> Result<Self> {
let config = match raw_config.extract::<Self>() { let config = raw_config
Err(e) => return Err!("There was a problem with your configuration file: {e}"), .extract::<Self>()
Ok(config) => config, .map_err(|e| err!("There was a problem with your configuration file: {e}"))?;
};
// don't start if we're listening on both UNIX sockets and TCP at same time // don't start if we're listening on both UNIX sockets and TCP at same time
check::is_dual_listening(raw_config)?; check::is_dual_listening(raw_config)?;

View file

@ -24,7 +24,7 @@ pub(crate) struct Server {
impl Server { impl Server {
pub(crate) fn build(args: &Args, runtime: Option<&runtime::Handle>) -> Result<Arc<Self>, Error> { pub(crate) fn build(args: &Args, runtime: Option<&runtime::Handle>) -> Result<Arc<Self>, Error> {
let raw_config = Config::load(&args.config)?; let raw_config = Config::load(args.config.as_deref())?;
let raw_config = crate::clap::update(raw_config, args)?; let raw_config = crate::clap::update(raw_config, args)?;
let config = Config::new(&raw_config)?; let config = Config::new(&raw_config)?;