fix(cli): Allow CLI to override walking config

This commit is contained in:
Ed Page 2021-04-05 07:34:05 -05:00
parent 78330ba9c1
commit 3fd90b09f8
4 changed files with 30 additions and 25 deletions

View file

@ -79,9 +79,6 @@ pub(crate) struct Args {
/// Write the current configuration to file with `-` for stdout
pub(crate) dump_config: Option<std::path::PathBuf>,
#[structopt(flatten)]
pub(crate) overrides: FileArgs,
#[structopt(
long,
possible_values(&Format::variants()),
@ -177,12 +174,15 @@ impl FileArgs {
pub(crate) struct ConfigArgs {
#[structopt(flatten)]
walk: WalkArgs,
#[structopt(flatten)]
overrides: FileArgs,
}
impl ConfigArgs {
pub fn to_config(&self) -> config::Config {
config::Config {
files: self.walk.to_config(),
overrides: Some(self.overrides.to_config()),
..Default::default()
}
}

View file

@ -6,6 +6,8 @@ use std::collections::HashMap;
pub struct Config {
pub files: Walk,
pub default: EngineConfig,
#[serde(skip)]
pub overrides: Option<EngineConfig>,
}
impl Config {
@ -34,6 +36,7 @@ impl Config {
Self {
files: Walk::from_defaults(),
default: EngineConfig::from_defaults(),
overrides: None,
}
}

View file

@ -58,14 +58,17 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
};
let storage = typos_cli::policy::ConfigStorage::new();
let mut overrides = config::EngineConfig::default();
overrides.update(&args.overrides.to_config());
let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
engine.set_isolated(args.isolated).set_overrides(overrides);
engine.set_isolated(args.isolated);
let mut overrides = config::Config::default();
if let Some(path) = args.custom_config.as_ref() {
let custom = config::Config::from_file(path).with_code(proc_exit::Code::CONFIG_ERR)?;
engine.set_custom_config(custom);
overrides.update(&custom);
}
overrides.update(&args.config.to_config());
engine.set_overrides(overrides);
let config = engine
.load_config(cwd)
.with_code(proc_exit::Code::CONFIG_ERR)?;
@ -86,14 +89,16 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
let global_cwd = std::env::current_dir()?;
let storage = typos_cli::policy::ConfigStorage::new();
let mut overrides = config::EngineConfig::default();
overrides.update(&args.overrides.to_config());
let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
engine.set_isolated(args.isolated).set_overrides(overrides);
engine.set_isolated(args.isolated);
let mut overrides = config::Config::default();
if let Some(path) = args.custom_config.as_ref() {
let custom = config::Config::from_file(path).with_code(proc_exit::Code::CONFIG_ERR)?;
engine.set_custom_config(custom);
overrides.update(&custom);
}
overrides.update(&args.config.to_config());
engine.set_overrides(overrides);
let mut typos_found = false;
let mut errors_found = false;

View file

@ -35,8 +35,7 @@ impl Default for ConfigStorage {
pub struct ConfigEngine<'s> {
storage: &'s ConfigStorage,
overrides: Option<crate::config::EngineConfig>,
custom: Option<crate::config::Config>,
overrides: Option<crate::config::Config>,
isolated: bool,
configs: std::collections::HashMap<std::path::PathBuf, DirConfig>,
@ -50,7 +49,6 @@ impl<'s> ConfigEngine<'s> {
Self {
storage,
overrides: Default::default(),
custom: Default::default(),
configs: Default::default(),
isolated: false,
walk: Default::default(),
@ -59,16 +57,11 @@ impl<'s> ConfigEngine<'s> {
}
}
pub fn set_overrides(&mut self, overrides: crate::config::EngineConfig) -> &mut Self {
pub fn set_overrides(&mut self, overrides: crate::config::Config) -> &mut Self {
self.overrides = Some(overrides);
self
}
pub fn set_custom_config(&mut self, custom: crate::config::Config) -> &mut Self {
self.custom = Some(custom);
self
}
pub fn set_isolated(&mut self, isolated: bool) -> &mut Self {
self.isolated = isolated;
self
@ -125,11 +118,8 @@ impl<'s> ConfigEngine<'s> {
config.update(&derived);
}
}
if let Some(custom) = self.custom.as_ref() {
config.update(custom);
}
if let Some(overrides) = self.overrides.as_ref() {
config.default.update(overrides);
config.update(overrides);
}
Ok(config)
@ -141,7 +131,14 @@ impl<'s> ConfigEngine<'s> {
}
let config = self.load_config(cwd)?;
let crate::config::Config { files, default } = config;
let crate::config::Config {
files,
mut default,
overrides,
} = config;
if let Some(overrides) = overrides {
default.update(&overrides);
}
let walk = self.walk.intern(files);
let default = self.init_file_config(default)?;