1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
use crate::common::*; use std::error::Error; use std::path::PathBuf; mod checks; mod common; mod fs_utils; pub use checks::available_check_names; #[allow(clippy::redundant_closure)] pub fn run(args: clap::ArgMatches, current_dir: &PathBuf) -> Result<Vec<Warning>, Box<dyn Error>> { let mut file_paths: Vec<PathBuf> = Vec::new(); let mut skip_checks: Vec<&str> = Vec::new(); let mut excluded_paths: Vec<PathBuf> = Vec::new(); let is_recursive = args.is_present("recursive"); if let Some(skip) = args.values_of("skip") { skip_checks = skip.collect(); } if let Some(excluded) = args.values_of("exclude") { excluded_paths = excluded .filter_map(|f| fs_utils::canonicalize(f).ok()) .collect(); } if let Some(inputs) = args.values_of("input") { let input_paths = inputs .filter_map(|s| fs_utils::canonicalize(s).ok()) .collect(); file_paths.extend(get_file_paths(input_paths, &excluded_paths, is_recursive)); } let mut warnings: Vec<Warning> = Vec::new(); for path in file_paths { let relative_path = match fs_utils::get_relative_path(&path, ¤t_dir) { Some(p) => p, None => continue, }; let file_with_lines = match FileEntry::from(relative_path) { Some(f) => f, None => continue, }; let lines = get_line_entries(file_with_lines.0, file_with_lines.1); let result = checks::run(lines, &skip_checks); warnings.extend(result); } Ok(warnings) } fn get_file_paths( dir_entries: Vec<PathBuf>, excludes: &[PathBuf], is_recursive: bool, ) -> Vec<PathBuf> { let nested_paths: Vec<PathBuf> = dir_entries .iter() .filter(|entry| entry.is_dir()) .filter(|entry| !excludes.contains(entry)) .filter_map(|dir| dir.read_dir().ok()) .map(|read_dir| { read_dir .filter_map(|e| e.ok()) .map(|e| e.path()) .filter(|path| FileEntry::is_env_file(path) || (is_recursive && path.is_dir())) .collect() }) .flat_map(|dir_entries| get_file_paths(dir_entries, excludes, is_recursive)) .collect(); let mut file_paths: Vec<PathBuf> = dir_entries .into_iter() .filter(|entry| entry.is_file()) .filter(|entry| !excludes.contains(entry)) .collect(); file_paths.sort(); file_paths.extend(nested_paths); file_paths.dedup(); file_paths } fn get_line_entries(fe: FileEntry, lines: Vec<String>) -> Vec<LineEntry> { let mut entries: Vec<LineEntry> = Vec::with_capacity(fe.total_lines); for (index, line) in lines.into_iter().enumerate() { entries.push(LineEntry { number: index + 1, file: fe.clone(), raw_string: line, }) } entries }