diff --git a/src/args.rs b/src/args.rs index 45c8ab1..938c362 100644 --- a/src/args.rs +++ b/src/args.rs @@ -79,9 +79,6 @@ pub(crate) struct Args { /// Write the current configuration to file with `-` for stdout pub(crate) dump_config: Option, - #[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() } } diff --git a/src/config.rs b/src/config.rs index a8e7323..9d21222 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,6 +6,8 @@ use std::collections::HashMap; pub struct Config { pub files: Walk, pub default: EngineConfig, + #[serde(skip)] + pub overrides: Option, } impl Config { @@ -34,6 +36,7 @@ impl Config { Self { files: Walk::from_defaults(), default: EngineConfig::from_defaults(), + overrides: None, } } diff --git a/src/main.rs b/src/main.rs index c0202f8..4b9c00b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/policy.rs b/src/policy.rs index f6083a5..4ac8cdc 100644 --- a/src/policy.rs +++ b/src/policy.rs @@ -35,8 +35,7 @@ impl Default for ConfigStorage { pub struct ConfigEngine<'s> { storage: &'s ConfigStorage, - overrides: Option, - custom: Option, + overrides: Option, isolated: bool, configs: std::collections::HashMap, @@ -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)?;