add forbids from a CSV

main
Brian Hicks 2020-12-03 20:20:16 -06:00
parent 4df62b7ee9
commit f870420d55
3 changed files with 78 additions and 1 deletions

41
Cargo.lock generated
View File

@ -44,9 +44,18 @@ version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "473fc6b38233f9af7baa94fb5852dca389e3d95b8e21c8e3719301462c5d9faf" checksum = "473fc6b38233f9af7baa94fb5852dca389e3d95b8e21c8e3719301462c5d9faf"
dependencies = [ dependencies = [
"lazy_static",
"memchr", "memchr",
"regex-automata",
"serde",
] ]
[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.62" version = "1.0.62"
@ -185,6 +194,28 @@ dependencies = [
"lazy_static", "lazy_static",
] ]
[[package]]
name = "csv"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d58633299b24b515ac72a3f869f8b91306a3cec616a602843a383acd6f9e97"
dependencies = [
"bstr",
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "elm-forbid-import" name = "elm-forbid-import"
version = "0.1.0" version = "0.1.0"
@ -193,6 +224,7 @@ dependencies = [
"cc", "cc",
"clap", "clap",
"crossbeam", "crossbeam",
"csv",
"ignore", "ignore",
"lazy_static", "lazy_static",
"pathdiff", "pathdiff",
@ -381,6 +413,15 @@ dependencies = [
"thread_local", "thread_local",
] ]
[[package]]
name = "regex-automata"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.21" version = "0.6.21"

View File

@ -11,6 +11,7 @@ license-file = "LICENSE"
anyhow = "1.0" anyhow = "1.0"
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
crossbeam = "0.8" crossbeam = "0.8"
csv = "1.0"
ignore = "0.4" ignore = "0.4"
lazy_static = "1.4" lazy_static = "1.4"
pathdiff = "0.1" pathdiff = "0.1"

View File

@ -1,4 +1,4 @@
use anyhow::{Context, Result}; use anyhow::{anyhow, Context, Result};
use clap::Clap; use clap::Clap;
use std::path::PathBuf; use std::path::PathBuf;
use std::process; use std::process;
@ -44,6 +44,13 @@ enum Mode {
hint: Option<String>, 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. /// Stop forbidding the use of a specific import.
Unforbid { Unforbid {
/// The fully-qualified name to forbid (e.g. `Html.Events`) /// The fully-qualified name to forbid (e.g. `Html.Events`)
@ -126,6 +133,34 @@ fn run(opts: Options) -> Result<i32> {
Ok(0) 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 } => { Mode::Unforbid { name } => {
store.unforbid(name); store.unforbid(name);
store.write().context("could not update the config file")?; store.write().context("could not update the config file")?;