From 6257dd00c70c9114aa53a727e164a4cab86645c6 Mon Sep 17 00:00:00 2001 From: Charles Marsh Date: Tue, 9 Aug 2022 17:02:14 -0400 Subject: [PATCH] Support multiple files --- src/bin/main.rs | 42 +++++++++++++----------------------------- src/fs.rs | 20 ++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 src/fs.rs diff --git a/src/bin/main.rs b/src/bin/main.rs index b3b15e676e..1ddbdf7070 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -5,9 +5,20 @@ use anyhow::Result; use clap::{Parser, ValueHint}; use log::{error, info}; use rayon::prelude::*; +use rust_python_linter::fs::collect_python_files; use rust_python_linter::linter::check_path; use rust_python_linter::message::Message; -use walkdir::{DirEntry, WalkDir}; +use walkdir::DirEntry; + +#[derive(Debug, Parser)] +#[clap(name = "rust-python-linter")] +#[clap(about = "A bare-bones Python linter written in Rust", long_about = None)] +struct Cli { + #[clap(parse(from_os_str), value_hint = ValueHint::AnyPath, required = true)] + files: Vec, + #[clap(short, long, action)] + verbose: bool, +} fn set_up_logging(verbose: bool) -> Result<()> { fern::Dispatch::new() @@ -31,27 +42,6 @@ fn set_up_logging(verbose: bool) -> Result<()> { .map_err(|e| e.into()) } -#[derive(Debug, Parser)] -#[clap(name = "rust-python-linter")] -#[clap(about = "A bare-bones Python linter written in Rust", long_about = None)] -struct Cli { - #[clap(name = "filename", parse(from_os_str), value_hint = ValueHint::DirPath)] - filename: PathBuf, - #[clap(short, long, action)] - verbose: bool, - // /// Files to process - // #[clap(name = "FILE", parse(from_os_str), value_hint = ValueHint::AnyPath)] - // files: Vec, -} - -fn is_not_hidden(entry: &DirEntry) -> bool { - entry - .file_name() - .to_str() - .map(|s| entry.depth() == 0 || !s.starts_with('.')) - .unwrap_or(false) -} - fn main() -> Result<()> { let cli = Cli::parse(); @@ -59,13 +49,7 @@ fn main() -> Result<()> { // Collect all the files to check. let start = Instant::now(); - let files: Vec = WalkDir::new(cli.filename) - .follow_links(true) - .into_iter() - .filter_entry(is_not_hidden) - .filter_map(|entry| entry.ok()) - .filter(|entry| entry.path().to_string_lossy().ends_with(".py")) - .collect(); + let files: Vec = cli.files.iter().flat_map(collect_python_files).collect(); let duration = start.elapsed(); info!("Identified files to lint in: {:?}", duration); diff --git a/src/fs.rs b/src/fs.rs new file mode 100644 index 0000000000..25d8db4f1d --- /dev/null +++ b/src/fs.rs @@ -0,0 +1,20 @@ +use std::path::PathBuf; +use walkdir::{DirEntry, WalkDir}; + +fn is_not_hidden(entry: &DirEntry) -> bool { + entry + .file_name() + .to_str() + .map(|s| entry.depth() == 0 || !s.starts_with('.')) + .unwrap_or(false) +} + +pub fn collect_python_files(path: &PathBuf) -> Vec { + WalkDir::new(path) + .follow_links(true) + .into_iter() + .filter_entry(is_not_hidden) + .filter_map(|entry| entry.ok()) + .filter(|entry| entry.path().to_string_lossy().ends_with(".py")) + .collect() +} diff --git a/src/lib.rs b/src/lib.rs index 665e3ecdcc..050555b180 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ mod cache; mod check; +pub mod fs; pub mod linter; pub mod message; mod parser;