mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-15 16:53:25 +00:00
cli: extract function that reads user config without merging
The next commit will add a struct that keeps umerged configs, which will be used to insert repo config between user and override configs.
This commit is contained in:
parent
b3c792f7bf
commit
47e2e4d220
1 changed files with 35 additions and 29 deletions
|
@ -13,7 +13,7 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::{env, fmt};
|
use std::{env, fmt};
|
||||||
|
|
||||||
|
@ -124,38 +124,44 @@ fn env_overrides() -> config::Config {
|
||||||
builder.build().unwrap()
|
builder.build().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_config_path(config_path: &Path) -> Result<config::Config, config::ConfigError> {
|
||||||
|
let mut files = vec![];
|
||||||
|
if config_path.is_dir() {
|
||||||
|
if let Ok(read_dir) = config_path.read_dir() {
|
||||||
|
// TODO: Walk the directory recursively?
|
||||||
|
for dir_entry in read_dir.flatten() {
|
||||||
|
let path = dir_entry.path();
|
||||||
|
if path.is_file() {
|
||||||
|
files.push(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
files.sort();
|
||||||
|
} else {
|
||||||
|
files.push(config_path.to_owned());
|
||||||
|
}
|
||||||
|
|
||||||
|
files
|
||||||
|
.iter()
|
||||||
|
.fold(config::Config::builder(), |builder, path| {
|
||||||
|
// TODO: Accept other formats and/or accept only certain file extensions?
|
||||||
|
builder.add_source(
|
||||||
|
config::File::from(path.as_ref())
|
||||||
|
.required(false)
|
||||||
|
.format(config::FileFormat::Toml),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn read_config() -> Result<UserSettings, ConfigError> {
|
pub fn read_config() -> Result<UserSettings, ConfigError> {
|
||||||
let mut config_builder = config::Config::builder()
|
let mut config_builder = config::Config::builder()
|
||||||
.add_source(default_config())
|
.add_source(default_config())
|
||||||
.add_source(env_base());
|
.add_source(env_base());
|
||||||
|
if let Some(path) = config_path()? {
|
||||||
if let Some(config_path) = config_path()? {
|
config_builder = config_builder.add_source(read_config_path(&path)?);
|
||||||
let mut files = vec![];
|
}
|
||||||
if config_path.is_dir() {
|
let config = config_builder.add_source(env_overrides()).build().unwrap();
|
||||||
if let Ok(read_dir) = config_path.read_dir() {
|
|
||||||
// TODO: Walk the directory recursively?
|
|
||||||
for dir_entry in read_dir.flatten() {
|
|
||||||
let path = dir_entry.path();
|
|
||||||
if path.is_file() {
|
|
||||||
files.push(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
files.sort();
|
|
||||||
} else {
|
|
||||||
files.push(config_path);
|
|
||||||
}
|
|
||||||
for file in files {
|
|
||||||
// TODO: Accept other formats and/or accept only certain file extensions?
|
|
||||||
config_builder = config_builder.add_source(
|
|
||||||
config::File::from(file)
|
|
||||||
.required(false)
|
|
||||||
.format(config::FileFormat::Toml),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let config = config_builder.add_source(env_overrides()).build()?;
|
|
||||||
Ok(UserSettings::from_config(config))
|
Ok(UserSettings::from_config(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue