use std::io::Write;
use std::process;
use clap::Parser;
use tracing::error;
use gqlprobe::cli::{init_logging, output_findings, run_scan, validate_target, Cli};
#[tokio::main]
async fn main() {
let cli = Cli::parse();
if cli.list_modules {
gqlprobe::cli::print_modules();
return;
}
init_logging(cli.verbose, cli.debug);
let _ = writeln!(
std::io::stderr().lock(),
"gqlprobe v{} - GraphQL security scanner (authorized targets only)",
env!("CARGO_PKG_VERSION")
);
let target = match &cli.target {
Some(t) => t.as_str(),
None => {
let _ = writeln!(
std::io::stderr().lock(),
"Error: URL is required. Example: gqlprobe https://api.example.com/graphql"
);
process::exit(2);
}
};
if let Err(e) = validate_target(target) {
error!("Invalid target URL: {}", e);
let _ = writeln!(std::io::stderr().lock(), "Error: {e}");
process::exit(2);
}
match run_scan(&cli).await {
Ok(findings) => {
if let Err(e) = output_findings(&findings, cli.output) {
error!("Failed to output findings: {}", e);
process::exit(1);
}
let exit_code = i32::from(!findings.is_empty());
if findings.is_empty() {
let _ = writeln!(std::io::stdout().lock(), "\n✓ No security issues found");
} else {
let (mut critical, mut high, mut medium, mut low) = (0, 0, 0, 0);
for f in &findings {
match f.severity() {
secfinding::Severity::Critical => critical += 1,
secfinding::Severity::High => high += 1,
secfinding::Severity::Medium => medium += 1,
secfinding::Severity::Low => low += 1,
_ => {}
}
}
let mut out = std::io::stdout().lock();
let _ = writeln!(out, "\n⚠ Found {} security issues:", findings.len());
if critical > 0 {
let _ = writeln!(out, " Critical: {critical}");
}
if high > 0 {
let _ = writeln!(out, " High: {high}");
}
if medium > 0 {
let _ = writeln!(out, " Medium: {medium}");
}
if low > 0 {
let _ = writeln!(out, " Low: {low}");
}
}
process::exit(exit_code);
}
Err(e) => {
error!("Scan failed: {}", e);
let _ = writeln!(std::io::stderr().lock(), "Error: Scan failed: {e}");
process::exit(1);
}
}
}