diff --git a/src/main.rs b/src/main.rs index e95c52a..bbb9162 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,12 @@ use clap::{Parser, Subcommand}; use colored::*; use git2::Repository; +#[derive(Debug)] +struct Repo { + path: PathBuf, + remotes: Vec<(String, String)> +} + /// Repotool is a tool to manage multiple code repositories #[derive(Parser, Debug)] #[command(version, about)] @@ -30,6 +36,7 @@ fn main() -> Result<(), std::io::Error> { Ok(()) } + fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> { use std::fs; @@ -40,7 +47,7 @@ fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> { ); println!(); - fn gather_repos(dir: &Path, recurse_level: usize) -> Result, std::io::Error> { + fn gather_repos(dir: &Path, recurse_level: usize) -> Result, std::io::Error> { let mut repos = Vec::new(); let dir = fs::read_dir(dir)?; @@ -54,8 +61,21 @@ fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> { .map(|byte| *byte == b'.') .unwrap_or(false); - if let Ok(_repo) = Repository::open(&path) { - repos.push(path); + if let Ok(repository) = Repository::open(&path) { + let remotes = match repository.remotes() { + Ok(remotes) => { + remotes.into_iter().map(|s| s.map(|s| s.to_string()).unwrap_or("".to_string())).collect() + }, + Err(err) => { + eprintln!("Error: {err}"); + vec![] + }, + }; + let repo = Repo { + path, + remotes, + }; + repos.push(repo); } else if path.is_dir() && !hidden { repos.extend(gather_repos(&path, recurse_level + 1)?.into_iter()); } @@ -63,10 +83,17 @@ fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> { Ok(repos) } - let repo_paths = gather_repos(&start, 0)?; - for path in &repo_paths { - let path = path.strip_prefix(&start).unwrap(); - println!("Repository: {}", path.display().to_string().yellow()); + let repos = gather_repos(&start, 0)?; + for repo in &repos { + let path = repo.path.strip_prefix(&start).unwrap(); + print!("Repository: {}", path.display().to_string().yellow()); + print!("Remotes: "); + for item in &repo.remotes { + print!("{item}"); + print!(" "); + } + + println!(); /* let indent = recurse_level * INDENT_INCREMENT; print!("{: <1$}", "", indent); @@ -74,7 +101,7 @@ fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> { } println!(); - println!("Total repos: {}", repo_paths.len()); + println!("Total repos: {}", repos.len()); Ok(()) }