summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 01addd3977acfee099a00e79753e4c2fe4a994c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use githubrepocloner::{create_client, clone_repos_with_client_and_url};
use reqwest::Error;
use std::env;
use std::process::Command;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let args: Vec<String> = env::args().collect();
    if args.len() < 4 {
        println!("Usage: githubrepocloner <organization> <folder> <days_last_updated> [--archive] [--depth=N]");
        println!("  --archive:  Download repositories as tar.gz archives instead of git cloning");
        println!("  --depth=N:  Clone depth (default: 1). Cannot be used with --archive");
        return Ok(());
    }

    let organization = &args[1];
    let folder = &args[2];
    let days_last_updated: i64 = args[3].parse().unwrap_or(365);
    
    // Parse optional flags from remaining args
    let optional_args = &args[4..];
    let use_archive = optional_args.iter().any(|a| a == "--archive");
    let has_depth_flag = optional_args.iter().any(|a| a.starts_with("--depth="));
    let clone_depth: u32 = optional_args.iter()
        .find(|a| a.starts_with("--depth="))
        .and_then(|a| a.strip_prefix("--depth="))
        .and_then(|v| v.parse().ok())
        .unwrap_or(1);
    
    if use_archive && has_depth_flag {
        eprintln!("Error: --depth cannot be used with --archive. Archive mode always downloads the full source tree.");
        std::process::exit(1);
    }

    if use_archive {
        println!("Archive mode enabled - repositories will be downloaded as tar.gz files");
    } else {
        // Verify git is available on PATH before attempting to clone
        match Command::new("git").arg("--version").output() {
            Ok(output) if output.status.success() => {},
            _ => {
                eprintln!("Error: git is not installed or not found on PATH. Install git or use --archive mode instead.");
                std::process::exit(1);
            }
        }
        if clone_depth != 1 {
            println!("Clone depth set to {clone_depth}");
        }
    }

    let token = env::var("GITHUB_TOKEN").ok();
    if token.is_some() {
        println!("Using authenticated GitHub API requests (GITHUB_TOKEN detected)");
    } else {
        println!("Using unauthenticated GitHub API requests (60 req/hour). Set GITHUB_TOKEN for higher limits.");
    }

    let client = create_client(token.as_deref())?;
    clone_repos_with_client_and_url(
        &client,
        "https://api.github.com",
        organization,
        folder,
        days_last_updated,
        use_archive,
        clone_depth,
    ).await
}