Skip to content

Commit

Permalink
add forbids from a CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Dec 4, 2020
1 parent 4df62b7 commit f870420
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -11,6 +11,7 @@ license-file = "LICENSE"
anyhow = "1.0"
clap = "3.0.0-beta.2"
crossbeam = "0.8"
csv = "1.0"
ignore = "0.4"
lazy_static = "1.4"
pathdiff = "0.1"
Expand Down
37 changes: 36 additions & 1 deletion src/main.rs
@@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use clap::Clap;
use std::path::PathBuf;
use std::process;
Expand Down Expand Up @@ -44,6 +44,13 @@ enum Mode {
hint: Option<String>,
},

/// Forbid a list of imports held in a CSV. The file should be a 2-column
/// CSV with "module" and "hint" fields and no headers.
ForbidFromCsv {
/// What file has the forbidden import list?
path: PathBuf,
},

/// Stop forbidding the use of a specific import.
Unforbid {
/// The fully-qualified name to forbid (e.g. `Html.Events`)
Expand Down Expand Up @@ -126,6 +133,34 @@ fn run(opts: Options) -> Result<i32> {
Ok(0)
}

Mode::ForbidFromCsv { path } => {
let mut reader = csv::ReaderBuilder::new()
.flexible(true)
.has_headers(false)
.from_path(path)
.context("could not read the CSV of forbidden imports")?;

for record in reader.records() {
let mut record = record.context("could not read record")?;
record.trim();

let module = record
.get(0)
.and_then(|name| if !name.is_empty() { Some(name) } else { None })
.map(|name| name.to_string())
.ok_or(anyhow!(
"I need a module name in the first column of the CSV at "
))?;
let hint = record.get(1).map(|name| name.to_string());

store.forbid(module, hint);
}

store.write().context("could not update the config file")?;

Ok(1)
}

Mode::Unforbid { name } => {
store.unforbid(name);
store.write().context("could not update the config file")?;
Expand Down

0 comments on commit f870420

Please sign in to comment.