update-projects.nvim/src/main.rs
2022-10-14 23:50:17 +02:00

74 lines
1.9 KiB
Rust

use std::path::PathBuf;
use clap::Parser;
#[derive(clap::Parser, Clone)]
struct Args {
#[arg(long)]
root_dir: Vec<String>,
#[arg(long)]
pattern: Option<String>,
#[arg(long)]
projects_file: String,
}
fn main() -> eyre::Result<()> {
let args = Args::parse();
// Choose dir
let mut projects: Vec<PathBuf> = vec![];
let pattern = Box::leak(args.pattern.unwrap_or(".git".into()).into_boxed_str());
// Look for dir .git if found add path to map
for root_dir in args.root_dir {
println!("searching: {}", root_dir);
for entry in jwalk::WalkDir::new(root_dir)
.skip_hidden(false)
.follow_links(false)
.process_read_dir(|_, _, _, children| {
children.retain(|dir_entry_result| {
dir_entry_result
.as_ref()
.map(|dir_entry| {
let metadata = match dir_entry.metadata() {
Ok(m) => m,
Err(_) => return false,
};
return metadata.is_dir();
})
.unwrap_or(false)
});
})
{
match entry {
Err(_) => {}
Ok(entry) => {
let name = entry.file_name().to_string_lossy();
if name.ends_with(&*pattern) {
projects.push(entry.path())
}
}
}
}
}
// Print
println!("writing projects");
let projects_str = projects
.into_iter()
.map(|mut p| {
p.pop();
p
})
.map(|p| p.to_string_lossy().to_string())
.collect::<Vec<String>>();
let content = projects_str.join("\n");
std::fs::write(args.projects_file, content)?;
Ok(())
}