diff --git a/src/main.rs b/src/main.rs index ac8d96f..5b77d25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,11 @@ struct Args { command: Command, } +struct Repo { + path: PathBuf, + remotes: Vec<(String, String)> +} + #[derive(Subcommand, Debug)] enum Command { ///List all repositories found in the directory tree @@ -40,7 +45,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 +59,22 @@ 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_array = repository.remotes().unwrap(); + + let remotes = remotes_array.into_iter().map(|remote_name| { + let name = remote_name.unwrap(); + let remote = repository.find_remote(name).unwrap(); + let url = remote.url().unwrap(); + + (name.to_string(), url.to_string()) + }).collect(); + + 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,11 +82,17 @@ fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> { Ok(repos) } - let mut repo_paths = gather_repos(&start, 0)?; - repo_paths.sort(); - for path in &repo_paths { - let path = path.strip_prefix(&start).unwrap(); - println!("Repository: {}", path.display().to_string().yellow()); + let mut repos = gather_repos(&start, 0)?; + repos.sort_unstable_by_key(|repo| repo.path.clone()); + for repo in &repos { + let path = repo.path.strip_prefix(&start).unwrap(); + print!("Repository: {}", path.display().to_string().yellow()); + + print!(" "); + for (name, url) in &repo.remotes { + print!("[{name} {url}]"); + } + println!(); /* let indent = recurse_level * INDENT_INCREMENT; print!("{: <1$}", "", indent); @@ -75,7 +100,7 @@ fn list_repos(directory: PathBuf) -> Result<(), std::io::Error> { } println!(); - println!("Total repos: {}", repo_paths.len()); + println!("Total repos: {}", repos.len()); Ok(()) }