Skip to content

Commit

Permalink
refactor(cli): Pull out policy creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Page committed Mar 30, 2021
1 parent f402d3e commit a76ddd4
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub(crate) struct Args {
pub(crate) verbose: clap_verbosity_flag::Verbosity,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Clone, StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub(crate) struct FileArgs {
#[structopt(long, overrides_with("no-binary"))]
Expand Down
31 changes: 16 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ pub struct Config {
}

impl Config {
pub fn from_dir(cwd: &std::path::Path) -> Result<Option<Self>, anyhow::Error> {
let config = if let Some(path) =
find_project_file(cwd, &["typos.toml", "_typos.toml", ".typos.toml"])
{
Some(Self::from_file(&path)?)
} else {
None
};
Ok(config)
}

pub fn from_file(path: &std::path::Path) -> Result<Self, anyhow::Error> {
let s = std::fs::read_to_string(path)?;
Self::from_toml(&s)
Expand All @@ -134,14 +145,6 @@ impl Config {
}
}

pub fn derive(cwd: &std::path::Path) -> Result<Self, anyhow::Error> {
if let Some(path) = find_project_file(cwd, &["typos.toml", "_typos.toml", ".typos.toml"]) {
Self::from_file(&path)
} else {
Ok(Default::default())
}
}

pub fn update(&mut self, source: &dyn ConfigSource) {
if let Some(walk) = source.walk() {
self.files.update(walk);
Expand Down Expand Up @@ -522,13 +525,11 @@ impl DictSource for DictConfig {
}

fn find_project_file(dir: &std::path::Path, names: &[&str]) -> Option<std::path::PathBuf> {
for ancestor in dir.ancestors() {
let mut file_path = ancestor.join("placeholder");
for name in names {
file_path.set_file_name(name);
if file_path.exists() {
return Some(file_path);
}
let mut file_path = dir.join("placeholder");
for name in names {
file_path.set_file_name(name);
if file_path.exists() {
return Some(file_path);
}
}
None
Expand Down
13 changes: 7 additions & 6 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,25 +552,25 @@ fn fix_buffer(mut buffer: Vec<u8>, typos: impl Iterator<Item = typos::Typo<'stat
pub fn walk_path(
walk: ignore::Walk,
checks: &dyn FileChecker,
policy: &crate::policy::Policy,
engine: &crate::policy::ConfigEngine,
reporter: &dyn report::Report,
) -> Result<(), ignore::Error> {
for entry in walk {
walk_entry(entry, checks, policy, reporter)?;
walk_entry(entry, checks, engine, reporter)?;
}
Ok(())
}

pub fn walk_path_parallel(
walk: ignore::WalkParallel,
checks: &dyn FileChecker,
policy: &crate::policy::Policy,
engine: &crate::policy::ConfigEngine,
reporter: &dyn report::Report,
) -> Result<(), ignore::Error> {
let error: std::sync::Mutex<Result<(), ignore::Error>> = std::sync::Mutex::new(Ok(()));
walk.run(|| {
Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| {
match walk_entry(entry, checks, policy, reporter) {
match walk_entry(entry, checks, engine, reporter) {
Ok(()) => ignore::WalkState::Continue,
Err(err) => {
*error.lock().unwrap() = Err(err);
Expand All @@ -586,7 +586,7 @@ pub fn walk_path_parallel(
fn walk_entry(
entry: Result<ignore::DirEntry, ignore::Error>,
checks: &dyn FileChecker,
policy: &crate::policy::Policy,
engine: &crate::policy::ConfigEngine,
reporter: &dyn report::Report,
) -> Result<(), ignore::Error> {
let entry = match entry {
Expand All @@ -603,7 +603,8 @@ fn walk_entry(
} else {
entry.path()
};
checks.check_file(path, explicit, policy, reporter)?;
let policy = engine.policy(path);
checks.check_file(path, explicit, &policy, reporter)?;
}

Ok(())
Expand Down
52 changes: 28 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
path.as_path()
};

let config = load_config(cwd, &args).with_code(proc_exit::Code::CONFIG_ERR)?;
let storage = typos_cli::policy::ConfigStorage::new();
let mut overrides = config::EngineConfig::default();
overrides.update(&args.overrides);
let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
engine.set_isolated(args.isolated).set_overrides(overrides);
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);
}
let config = engine
.load_config(cwd)
.with_code(proc_exit::Code::CONFIG_ERR)?;

let mut defaulted_config = config::Config::from_defaults();
defaulted_config.update(&config);
let output = toml::to_string_pretty(&defaulted_config).with_code(proc_exit::Code::FAILURE)?;
Expand Down Expand Up @@ -88,12 +100,21 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
} else {
path.as_path()
};
let config = load_config(cwd, &args).with_code(proc_exit::Code::CONFIG_ERR)?;

let storage = typos_cli::policy::ConfigStorage::new();
let engine = typos_cli::policy::ConfigEngine::new(config, &storage);
let files = engine.files();
let policy = engine.policy();
let mut overrides = config::EngineConfig::default();
overrides.update(&args.overrides);
let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
engine.set_isolated(args.isolated).set_overrides(overrides);
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);
}

engine
.init_dir(cwd)
.with_code(proc_exit::Code::CONFIG_ERR)?;
let files = engine.files(cwd);

let threads = if path.is_file() { 1 } else { args.threads };
let single_threaded = threads == 1;
Expand Down Expand Up @@ -131,12 +152,12 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
};

if single_threaded {
typos_cli::file::walk_path(walk.build(), selected_checks, &policy, reporter)
typos_cli::file::walk_path(walk.build(), selected_checks, &engine, reporter)
} else {
typos_cli::file::walk_path_parallel(
walk.build_parallel(),
selected_checks,
&policy,
&engine,
reporter,
)
}
Expand Down Expand Up @@ -189,20 +210,3 @@ fn init_logging(level: Option<log::Level>) {
builder.init();
}
}

fn load_config(cwd: &std::path::Path, args: &args::Args) -> Result<config::Config, anyhow::Error> {
let mut config = config::Config::default();

if !args.isolated {
let derived = config::Config::derive(cwd)?;
config.update(&derived);
}
if let Some(path) = args.custom_config.as_ref() {
config.update(&config::Config::from_file(path)?);
}

config.update(&args.config);
config.default.update(&args.overrides);

Ok(config)
}
Loading

0 comments on commit a76ddd4

Please sign in to comment.