feat: add ability to use gitignore to exclude obj
This commit is contained in:
@@ -7,3 +7,4 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
colored = "2.0.0"
|
colored = "2.0.0"
|
||||||
|
gitignore = "1.0.7"
|
||||||
41
src/main.rs
41
src/main.rs
@@ -1,10 +1,11 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::env;
|
use std::env;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
use gitignore;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
if args.len() != 2 {
|
if args.len() <= 1{
|
||||||
println!("{}", "ERR: Invalid number of arguments provided".red());
|
println!("{}", "ERR: Invalid number of arguments provided".red());
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -21,14 +22,30 @@ fn main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: find safer way to check for gitignore
|
||||||
|
let gi_exists = fs::metadata(args[1].to_owned() + "/.gitignore").is_ok();
|
||||||
|
if !gi_exists {
|
||||||
|
println!("{}", "WRN: .gitignore not found, may output TODOs in dependencies".yellow())
|
||||||
|
}
|
||||||
|
|
||||||
let mut todos = vec!();
|
let mut todos = vec!();
|
||||||
traverse_dir(&args[1], &mut todos).unwrap();
|
|
||||||
|
if gi_exists {
|
||||||
|
let gi_str = args[1].to_owned() + "/.gitignore";
|
||||||
|
let gi_path = std::path::Path::new(&gi_str);
|
||||||
|
let gi= gitignore::File::new(gi_path).unwrap();
|
||||||
|
traverse_dir(&args[1], &mut todos, Some(&gi)).unwrap();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
traverse_dir(&args[1], &mut todos, None).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
for todo in todos {
|
for todo in todos {
|
||||||
println!("TODO: {}", todo.green());
|
println!("TODO: {}", todo.green());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn traverse_dir(path: &str, todos: &mut Vec<String>) -> Result<(), std::io::Error> {
|
fn traverse_dir(path: &str, todos: &mut Vec<String>, gi_option: Option<&gitignore::File>) -> Result<(), std::io::Error> {
|
||||||
let objects = fs::read_dir(path.to_owned())?;
|
let objects = fs::read_dir(path.to_owned())?;
|
||||||
|
|
||||||
for result in objects {
|
for result in objects {
|
||||||
@@ -36,17 +53,21 @@ fn traverse_dir(path: &str, todos: &mut Vec<String>) -> Result<(), std::io::Erro
|
|||||||
let obj_str = obj_path.to_str().unwrap();
|
let obj_str = obj_path.to_str().unwrap();
|
||||||
let obj_metadata = fs::metadata(obj_str)?;
|
let obj_metadata = fs::metadata(obj_str)?;
|
||||||
|
|
||||||
if obj_metadata.is_dir() {
|
let is_excluded = match gi_option {
|
||||||
// println!("DIRC: {}", obj_str.yellow());
|
Some(gi) => {
|
||||||
traverse_dir(obj_str, todos)?;
|
let dir_path = std::path::Path::new(obj_str);
|
||||||
|
gi.is_excluded(dir_path).unwrap()
|
||||||
}
|
}
|
||||||
else if obj_metadata.is_file() {
|
None => { false }
|
||||||
if obj_str.ends_with(".py") {
|
};
|
||||||
// println!("FILE: {}", obj_str.yellow());
|
|
||||||
|
if !is_excluded && obj_metadata.is_dir() {
|
||||||
|
traverse_dir(obj_str, todos, gi_option)?;
|
||||||
|
}
|
||||||
|
else if !is_excluded && obj_metadata.is_file() && obj_str.ends_with(".py") {
|
||||||
get_todos(obj_str, todos)?;
|
get_todos(obj_str, todos)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user