cli: make configs from environment a separate source instead of overrides

This addresses a TODO I had left in the code. In addition to the
reasons I mentioned in the TODO, config sources are also better
because they can be layered. I'm planning on using that for editor
configs, letting `$EDITOR` be a layer under the configs and
`$JJ_EDITOR` be a layer over (matching how `git` does it).
This commit is contained in:
Martin von Zweigbergk 2022-04-09 19:35:32 -07:00 committed by Martin von Zweigbergk
parent 486f49435b
commit 90ca03b244

View file

@ -31,6 +31,21 @@ fn config_path() -> Option<PathBuf> {
} }
} }
/// Environment variables that override config values
fn env_overrides() -> config::Config {
let mut builder = config::Config::builder();
if let Ok(value) = env::var("JJ_USER") {
builder = builder.set_override("user.name", value).unwrap();
}
if let Ok(value) = env::var("JJ_EMAIL") {
builder = builder.set_override("user.email", value).unwrap();
}
if let Ok(value) = env::var("JJ_TIMESTAMP") {
builder = builder.set_override("user.timestamp", value).unwrap();
}
builder.build().unwrap()
}
fn read_config() -> Result<UserSettings, config::ConfigError> { fn read_config() -> Result<UserSettings, config::ConfigError> {
let mut config_builder = config::Config::builder(); let mut config_builder = config::Config::builder();
@ -60,21 +75,7 @@ fn read_config() -> Result<UserSettings, config::ConfigError> {
} }
}; };
// TODO: Make the config from environment a separate source instead? Seems let config = config_builder.add_source(env_overrides()).build()?;
// cleaner to separate it like that, especially if the config::Config instance
// can keep track of where the config comes from then (it doesn't seem like it
// can, however - we don't give a name or anything to the Config object).
if let Ok(value) = env::var("JJ_USER") {
config_builder = config_builder.set_override("user.name", value)?;
}
if let Ok(value) = env::var("JJ_EMAIL") {
config_builder = config_builder.set_override("user.email", value)?;
}
if let Ok(value) = env::var("JJ_TIMESTAMP") {
config_builder = config_builder.set_override("user.timestamp", value)?;
}
let config = config_builder.build()?;
Ok(UserSettings::from_config(config)) Ok(UserSettings::from_config(config))
} }