diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c40a069 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "clone_repos" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e69b68a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "clone_repos" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/clone_repos b/clone_repos new file mode 100755 index 0000000..c39820d Binary files /dev/null and b/clone_repos differ diff --git a/clone_repos.sh b/clone_repos.sh deleted file mode 100755 index 54f0f3d..0000000 --- a/clone_repos.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -# Prompt for Github username - -read -p "Enter your Github username: " GITHUB_USER - -# Prompt for directory for cloning - -read -p "Enter the directory where you want to clone the repos: " CLONE_DIR - -# Create the directory if not present -mkdir -p "$CLONE_DIR" - -# Nav to directory -cd "$CLONE_DIR" || exit - -# Fetch all repos - -repos=$(gh repo list "$GITHUB_USER" --limit 100 --json nameWithOwner --jq '.[].nameWithOwner') - -for repo in $repos; do - git clone "git@github.com:$repo.git" - echo "$repo has been cloned" -done - -echo "All repos have been cloned into $CLONE_DIR" - diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..cf49434 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,77 @@ +use std::env; +use std::fs; +use std::io::{self, Write}; +use std::process::Command; + +fn main() -> io::Result<()> { + // Prompt for GitHub username + let mut github_user = String::new(); + print!("Enter your GitHub username: "); + io::stdout().flush()?; + io::stdin().read_line(&mut github_user)?; + let github_user = github_user.trim(); + + // Prompt for directory for cloning + let mut clone_dir = String::new(); + print!("Enter the directory where you want to clone the repos: "); + io::stdout().flush()?; + io::stdin().read_line(&mut clone_dir)?; + let clone_dir = clone_dir.trim(); + + // Create the directory if not present + fs::create_dir_all(clone_dir)?; + + // Navigate to the directory + env::set_current_dir(clone_dir)?; + + // Fetch all repos + let output = Command::new("gh") + .arg("repo") + .arg("list") + .arg(github_user) + .arg("--limit") + .arg("100") + .arg("--json") + .arg("nameWithOwner") + .arg("--jq") + .arg(".[].nameWithOwner") + .output()?; + + if !output.status.success() { + eprintln!("Failed to fetch repositories. Make sure 'gh' is installed and configured."); + return Ok(()); + } + + let repos = String::from_utf8_lossy(&output.stdout); + + for repo in repos.lines() { + let repo = repo.trim(); + + // Extract the repository name (remove the username part) + let repo_name = repo.split('/').nth(1).unwrap_or(repo); + let repo_dir = repo_name.replace('/', "_"); // Replace '/' with '_' to avoid invalid directory names + let clone_path = format!("{}/{}", clone_dir, repo_dir); + + // Create a subdirectory for the repo + fs::create_dir_all(&clone_path)?; + + // Clone the repository into the subdirectory + let clone_command = format!("git clone git@github.com:{} {}", repo, clone_path); + + let status = Command::new("sh") + .arg("-c") + .arg(&clone_command) + .status()?; + + if status.success() { + println!("{} has been cloned into {}", repo, clone_path); + } else { + eprintln!("Failed to clone {} into {}", repo, clone_path); + } + } + + println!("All repos have been cloned into {}", clone_dir); + + Ok(()) +} +