(feat): See ya rust, going to Python
This commit is contained in:
parent
f2b1814abd
commit
a124d6af8e
6 changed files with 62 additions and 91 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +0,0 @@
|
|||
/target
|
||||
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -1,7 +0,0 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "clone_repos"
|
||||
version = "0.1.0"
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
[package]
|
||||
name = "clone_repos"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
62
clone.py
Normal file
62
clone.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import os
|
||||
import subprocess
|
||||
|
||||
def main():
|
||||
github_user = input("Enter your GitHub username: ").strip()
|
||||
clone_dir = input("Enter the directory where you want to clone the repos: ").strip()
|
||||
|
||||
os.makedirs(clone_dir, exist_ok=True)
|
||||
os.chdir(clone_dir)
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[
|
||||
"gh", "repo", "list", github_user, "--limit", "100", "--json",
|
||||
"nameWithOwner,isFork,isArchived", "--jq",
|
||||
".[] | select(.isFork == false and .isArchived == false) | .nameWithOwner"
|
||||
],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
print("Failed to fetch repositories. Make sure 'gh' is installed and configured.")
|
||||
print(result.stderr)
|
||||
return
|
||||
|
||||
repos = result.stdout.strip().splitlines()
|
||||
except Exception as e:
|
||||
print(f"Error fetching repositories: {e}")
|
||||
return
|
||||
|
||||
for repo in repos:
|
||||
repo = repo.strip()
|
||||
|
||||
repo_name = repo.split("/")[1] if "/" in repo else repo
|
||||
repo_dir = repo_name.replace("/", "_") # Replace '/' with '_' to avoid invalid directory names
|
||||
clone_path = os.path.join(clone_dir, repo_dir)
|
||||
|
||||
os.makedirs(clone_path, exist_ok=True)
|
||||
|
||||
clone_command = f"git clone git@github.com:{repo} {clone_path}"
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["sh", "-c", clone_command],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
print(f"{repo} has been cloned into {clone_path}")
|
||||
else:
|
||||
print(f"Failed to clone {repo} into {clone_path}")
|
||||
print(result.stderr)
|
||||
except Exception as e:
|
||||
print(f"Error cloning {repo}: {e}")
|
||||
|
||||
print(f"All repos have been cloned into {clone_dir}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
BIN
clone_repos
BIN
clone_repos
Binary file not shown.
77
src/main.rs
77
src/main.rs
|
|
@ -1,77 +0,0 @@
|
|||
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(())
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue