mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-15 16:53:25 +00:00
config: extract remainder of LayeredConfigs methods to free functions
This commit is contained in:
parent
a890e1a7f8
commit
08b1ebe934
1 changed files with 26 additions and 15 deletions
|
@ -119,26 +119,14 @@ pub struct LayeredConfigs {
|
||||||
impl LayeredConfigs {
|
impl LayeredConfigs {
|
||||||
/// Initializes configs with infallible sources.
|
/// Initializes configs with infallible sources.
|
||||||
pub fn from_environment(default: config::Config) -> Self {
|
pub fn from_environment(default: config::Config) -> Self {
|
||||||
let mut inner = StackedConfig::empty();
|
let inner = config_from_environment(default);
|
||||||
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(),
|
|
||||||
));
|
|
||||||
LayeredConfigs { inner }
|
LayeredConfigs { inner }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_config_args(&mut self, toml_strs: &[String]) -> Result<(), ConfigEnvError> {
|
pub fn parse_config_args(&mut self, toml_strs: &[String]) -> Result<(), ConfigEnvError> {
|
||||||
self.inner.remove_layers(ConfigSource::CommandArg);
|
self.inner.remove_layers(ConfigSource::CommandArg);
|
||||||
let config = toml_strs
|
let layer = parse_config_args(toml_strs)?;
|
||||||
.iter()
|
self.inner.add_layer(layer);
|
||||||
.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));
|
|
||||||
Ok(())
|
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
|
/// Environment variables that should be overridden by config values
|
||||||
fn env_base() -> config::Config {
|
fn env_base() -> config::Config {
|
||||||
let mut builder = config::Config::builder();
|
let mut builder = config::Config::builder();
|
||||||
|
@ -461,6 +461,17 @@ fn env_overrides() -> config::Config {
|
||||||
builder.build().unwrap()
|
builder.build().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses `--config-toml` arguments.
|
||||||
|
pub fn parse_config_args(toml_strs: &[String]) -> Result<ConfigLayer, ConfigError> {
|
||||||
|
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<toml_edit::ImDocument<String>, CommandError> {
|
fn read_config(path: &Path) -> Result<toml_edit::ImDocument<String>, CommandError> {
|
||||||
let config_toml = std::fs::read_to_string(path).or_else(|err| {
|
let config_toml = std::fs::read_to_string(path).or_else(|err| {
|
||||||
match err.kind() {
|
match err.kind() {
|
||||||
|
|
Loading…
Reference in a new issue