From 08b1ebe9348cc0b938f898759978b8cc64a18f46 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 23 Nov 2024 19:36:48 +0900 Subject: [PATCH] config: extract remainder of LayeredConfigs methods to free functions --- cli/src/config.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index a75a9b02b9..b08ed11bef 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -119,26 +119,14 @@ pub struct LayeredConfigs { impl LayeredConfigs { /// Initializes configs with infallible sources. pub fn from_environment(default: config::Config) -> Self { - let mut inner = StackedConfig::empty(); - inner.add_layer(ConfigLayer::with_data(ConfigSource::Default, default)); - inner.add_layer(ConfigLayer::with_data(ConfigSource::EnvBase, env_base())); - inner.add_layer(ConfigLayer::with_data( - ConfigSource::EnvOverrides, - env_overrides(), - )); + let inner = config_from_environment(default); LayeredConfigs { inner } } pub fn parse_config_args(&mut self, toml_strs: &[String]) -> Result<(), ConfigEnvError> { self.inner.remove_layers(ConfigSource::CommandArg); - let config = toml_strs - .iter() - .fold(config::Config::builder(), |builder, s| { - builder.add_source(config::File::from_str(s, config::FileFormat::Toml)) - }) - .build()?; - self.inner - .add_layer(ConfigLayer::with_data(ConfigSource::CommandArg, config)); + let layer = parse_config_args(toml_strs)?; + self.inner.add_layer(layer); Ok(()) } @@ -382,6 +370,18 @@ impl ConfigEnv { } } +/// Initializes stacked config with the given `default` and infallible sources. +pub fn config_from_environment(default: config::Config) -> StackedConfig { + let mut config = StackedConfig::empty(); + config.add_layer(ConfigLayer::with_data(ConfigSource::Default, default)); + config.add_layer(ConfigLayer::with_data(ConfigSource::EnvBase, env_base())); + config.add_layer(ConfigLayer::with_data( + ConfigSource::EnvOverrides, + env_overrides(), + )); + config +} + /// Environment variables that should be overridden by config values fn env_base() -> config::Config { let mut builder = config::Config::builder(); @@ -461,6 +461,17 @@ fn env_overrides() -> config::Config { builder.build().unwrap() } +/// Parses `--config-toml` arguments. +pub fn parse_config_args(toml_strs: &[String]) -> Result { + let config = toml_strs + .iter() + .fold(config::Config::builder(), |builder, s| { + builder.add_source(config::File::from_str(s, config::FileFormat::Toml)) + }) + .build()?; + Ok(ConfigLayer::with_data(ConfigSource::CommandArg, config)) +} + fn read_config(path: &Path) -> Result, CommandError> { let config_toml = std::fs::read_to_string(path).or_else(|err| { match err.kind() {