clidispatch: move logic to read system and user config to configparser

Summary: The configparser crate is a better place for logic to load system and user config.

Reviewed By: sfilipco

Differential Revision: D16796396

fbshipit-source-id: c1ae5f85aa9839d89f5026279d699135dc1b442b
This commit is contained in:
Jun Wu 2019-08-21 12:57:32 -07:00 committed by Facebook Github Bot
parent 8f349dbb17
commit b85df9caf2
2 changed files with 15 additions and 19 deletions

View File

@ -11,7 +11,7 @@ use bytes::Bytes;
use cliparser::alias::{expand_aliases, expand_prefix};
use cliparser::parser::{ParseError, ParseOptions, ParseOutput, StructFlags};
use configparser::config::ConfigSet;
use configparser::hg::{parse_list, ConfigSetHgExt};
use configparser::hg::parse_list;
use failure::Fallible;
use std::{collections::BTreeMap, env, path::Path};
@ -25,23 +25,6 @@ pub fn args() -> Fallible<Vec<String>> {
.collect()
}
fn load_config() -> Fallible<ConfigSet> {
// priority is ->
// - system
// - user
// - repo
// - configfile
// - config ( bottom overrides above )
let mut errors = Vec::new();
let mut config = ConfigSet::new();
errors.extend(config.load_system());
errors.extend(config.load_user());
if let Some(error) = errors.pop() {
return Err(failure::Error::from(error));
}
Ok(config)
}
/// Apply config override flags.
fn override_config<P>(
config: &mut ConfigSet,
@ -187,7 +170,7 @@ pub fn dispatch(command_table: &CommandTable, mut args: Vec<String>, io: &mut IO
let mut optional_repo = OptionalRepo::from_repository_path_and_cwd(
&global_opts.repository,
&env::current_dir()?,
load_config()?,
configparser::hg::load()?,
)?;
override_config(
optional_repo.config_mut(),

View File

@ -13,6 +13,7 @@ use std::path::Path;
use bytes::Bytes;
use dirs;
use failure::Fallible;
use crate::config::{expand_path, ConfigSet, Options};
use crate::error::Error;
@ -58,6 +59,18 @@ pub trait ConfigSetHgExt {
fn load_hgrc(&mut self, path: impl AsRef<Path>, source: &'static str) -> Vec<Error>;
}
/// Load system, user config files.
pub fn load() -> Fallible<ConfigSet> {
let mut set = ConfigSet::new();
if let Some(error) = set.load_system().pop() {
return Err(error.into());
}
if let Some(error) = set.load_user().pop() {
return Err(error.into());
}
Ok(set)
}
impl OptionsHgExt for Options {
fn process_hgplain(self) -> Self {
let plain_set = env::var(HGPLAIN).is_ok();