repotool/src/main.rs

80 lines
2.0 KiB
Rust
Raw Normal View History

2024-10-17 01:22:51 -07:00
use std::path::{Path, PathBuf};
2024-10-16 14:26:43 -07:00
2024-10-16 14:36:41 -07:00
use clap::{Parser, Subcommand};
2024-10-17 01:22:51 -07:00
use colored::*;
use git2::Repository;
2024-10-16 14:26:43 -07:00
/// Repotool is a tool to manage multiple code repositories
#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
2024-10-16 14:36:41 -07:00
#[command(subcommand)]
command: Command,
}
2024-10-16 14:26:43 -07:00
2024-10-16 14:36:41 -07:00
#[derive(Subcommand, Debug)]
enum Command {
///List all repositories found in the directory tree
List {
/// Directory to start from
#[arg(short, long)]
directory: PathBuf,
},
2024-10-16 14:26:43 -07:00
}
2024-10-16 14:36:41 -07:00
fn main() -> Result<(), std::io::Error> {
2024-10-16 14:26:43 -07:00
let args = Args::parse();
2024-10-16 14:36:41 -07:00
match args.command {
Command::List { directory } => list_repos(directory)?,
}
Ok(())
}
fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> {
2024-10-17 01:22:51 -07:00
use std::fs;
let start = fs::canonicalize(directory)?;
println!(
"Listing repositories under: {}",
start.display().to_string().yellow()
);
println!();
fn gather_repos(dir: &Path, recurse_level: usize) -> Result<Vec<PathBuf>, std::io::Error> {
let mut repos = Vec::new();
let dir = fs::read_dir(dir)?;
for entry in dir {
let entry = entry?;
let path = entry.path();
let hidden = path
.file_name()
.map(|name| name.as_encoded_bytes())
.and_then(|bytes| bytes.first())
.map(|byte| *byte == b'.')
.unwrap_or(false);
if let Ok(_repo) = Repository::open(&path) {
repos.push(path);
} else if path.is_dir() && !hidden {
repos.extend(gather_repos(&path, recurse_level + 1)?.into_iter());
}
}
Ok(repos)
}
let repo_paths = gather_repos(&start, 0)?;
for path in &repo_paths {
println!("Repository: {}", path.display().to_string().yellow());
/*
let indent = recurse_level * INDENT_INCREMENT;
print!("{: <1$}", "", indent);
*/
}
println!();
println!("Total repos: {}", repo_paths.len());
2024-10-16 14:26:43 -07:00
2024-10-16 14:36:41 -07:00
Ok(())
2024-10-16 14:09:55 -07:00
}