|
| 1 | +use std::env; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use jsonwebtoken::EncodingKey; |
| 5 | +use rapina::database::DatabaseConfig; |
| 6 | + |
| 7 | +use crate::github::client::GitHubClient; |
| 8 | + |
| 9 | +fn print_line(label: &str, value: &str) { |
| 10 | + println!("{:<24} {}", label, value); |
| 11 | +} |
| 12 | + |
| 13 | +pub async fn run() -> i32 { |
| 14 | + let mut issues: Vec<String> = Vec::new(); |
| 15 | + |
| 16 | + println!("fila doctor\n"); |
| 17 | + |
| 18 | + // .env file presence |
| 19 | + let dotenv_exists = Path::new(".env").exists(); |
| 20 | + print_line( |
| 21 | + ".env file", |
| 22 | + if dotenv_exists { "found" } else { "not found" }, |
| 23 | + ); |
| 24 | + |
| 25 | + // Required env vars — never print values |
| 26 | + let required = [ |
| 27 | + "DATABASE_URL", |
| 28 | + "GITHUB_APP_ID", |
| 29 | + "GITHUB_PRIVATE_KEY", |
| 30 | + "GITHUB_WEBHOOK_SECRET", |
| 31 | + ]; |
| 32 | + |
| 33 | + for var in &required { |
| 34 | + match env::var(var) { |
| 35 | + Ok(_) => print_line(var, "set"), |
| 36 | + Err(_) => { |
| 37 | + print_line(var, "MISSING"); |
| 38 | + issues.push(format!("{var} is not set")); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + println!(); |
| 44 | + |
| 45 | + // Optional vars with defaults |
| 46 | + let optionals: &[(&str, &str)] = &[ |
| 47 | + ("SERVER_PORT", "8000"), |
| 48 | + ("HOST", "127.0.0.1"), |
| 49 | + ("MERGE_STRATEGY", "batch"), |
| 50 | + ("BATCH_SIZE", "5"), |
| 51 | + ("BATCH_INTERVAL_SECS", "10"), |
| 52 | + ("CI_TIMEOUT_SECS", "1800"), |
| 53 | + ("POLL_INTERVAL_SECS", "15"), |
| 54 | + ]; |
| 55 | + |
| 56 | + for (var, default) in optionals { |
| 57 | + let value = env::var(var).unwrap_or_else(|_| default.to_string()); |
| 58 | + print_line(var, &value); |
| 59 | + } |
| 60 | + |
| 61 | + // Validate MERGE_STRATEGY |
| 62 | + let strategy = env::var("MERGE_STRATEGY").unwrap_or_else(|_| "batch".to_string()); |
| 63 | + if strategy != "batch" && strategy != "sequential" { |
| 64 | + issues.push(format!( |
| 65 | + "MERGE_STRATEGY is \"{strategy}\", expected \"batch\" or \"sequential\"" |
| 66 | + )); |
| 67 | + } |
| 68 | + |
| 69 | + println!(); |
| 70 | + |
| 71 | + // Private key validation |
| 72 | + let key_result = env::var("GITHUB_PRIVATE_KEY"); |
| 73 | + match &key_result { |
| 74 | + Ok(key) => match EncodingKey::from_rsa_pem(key.as_bytes()) { |
| 75 | + Ok(_) => print_line("Private key", "valid RSA PEM"), |
| 76 | + Err(e) => { |
| 77 | + print_line("Private key", &format!("INVALID ({e})")); |
| 78 | + issues.push(format!("GITHUB_PRIVATE_KEY is not valid RSA PEM: {e}")); |
| 79 | + } |
| 80 | + }, |
| 81 | + Err(_) => print_line("Private key", "skipped (not set)"), |
| 82 | + } |
| 83 | + |
| 84 | + // Database connectivity |
| 85 | + let db_result = env::var("DATABASE_URL"); |
| 86 | + match &db_result { |
| 87 | + Ok(url) => { |
| 88 | + let db_config = DatabaseConfig::new(url); |
| 89 | + match db_config.connect().await { |
| 90 | + Ok(_) => { |
| 91 | + let db_type = if url.starts_with("sqlite") { |
| 92 | + "sqlite" |
| 93 | + } else if url.starts_with("postgres") { |
| 94 | + "postgres" |
| 95 | + } else { |
| 96 | + "unknown" |
| 97 | + }; |
| 98 | + print_line("Database", &format!("connected ({db_type})")); |
| 99 | + } |
| 100 | + Err(e) => { |
| 101 | + print_line("Database", &format!("FAILED ({e})")); |
| 102 | + issues.push(format!("Database connection failed: {e}")); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + Err(_) => print_line("Database", "skipped (DATABASE_URL not set)"), |
| 107 | + } |
| 108 | + |
| 109 | + // GitHub API auth |
| 110 | + let app_id = env::var("GITHUB_APP_ID"); |
| 111 | + match (&app_id, &key_result) { |
| 112 | + (Ok(id), Ok(key)) => { |
| 113 | + let client = GitHubClient::new(id.clone(), key.clone()); |
| 114 | + match client.get_app_info().await { |
| 115 | + Ok(info) => { |
| 116 | + print_line("GitHub API", &format!("authenticated as \"{}\"", info.name)); |
| 117 | + } |
| 118 | + Err(e) => { |
| 119 | + print_line("GitHub API", &format!("FAILED ({e})")); |
| 120 | + issues.push(format!("GitHub API authentication failed: {e}")); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + _ => print_line("GitHub API", "skipped (credentials not set)"), |
| 125 | + } |
| 126 | + |
| 127 | + // Summary |
| 128 | + println!(); |
| 129 | + if issues.is_empty() { |
| 130 | + println!("All checks passed."); |
| 131 | + 0 |
| 132 | + } else { |
| 133 | + println!( |
| 134 | + "{} {} found:", |
| 135 | + issues.len(), |
| 136 | + if issues.len() == 1 { "issue" } else { "issues" } |
| 137 | + ); |
| 138 | + for issue in &issues { |
| 139 | + println!(" - {issue}"); |
| 140 | + } |
| 141 | + 1 |
| 142 | + } |
| 143 | +} |
0 commit comments