use clap::Parser;
use colored::Colorize;
use llm_config_devtools::report::{generate_report, write_report, OutputFormat};
use llm_config_devtools::security::{ScanConfig, SecurityScanner};
use std::path::PathBuf;
use std::process;
use tracing_subscriber::EnvFilter;
#[derive(Parser)]
#[command(name = "llm-security-scan")]
#[command(about = "Security scanner for LLM Config Manager", long_about = None)]
#[command(version)]
struct Cli {
#[arg(short, long, default_value = ".")]
project: PathBuf,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(short, long, default_value = "markdown")]
format: String,
#[arg(long)]
no_clippy: bool,
#[arg(long)]
no_unsafe: bool,
#[arg(long)]
no_secrets: bool,
#[arg(long)]
no_sql: bool,
#[arg(short, long)]
verbose: bool,
#[arg(long)]
fail_on_high: bool,
}
fn main() {
let cli = Cli::parse();
let filter = if cli.verbose {
EnvFilter::new("llm_config_devtools=debug")
} else {
EnvFilter::new("llm_config_devtools=info")
};
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_target(false)
.init();
let format = match cli.format.parse::<OutputFormat>() {
Ok(f) => f,
Err(e) => {
eprintln!("{} {}", "Error:".red().bold(), e);
process::exit(1);
}
};
let config = ScanConfig {
project_root: cli.project.clone(),
scan_clippy: !cli.no_clippy,
scan_unsafe: !cli.no_unsafe,
scan_secrets: !cli.no_secrets,
scan_sql: !cli.no_sql,
max_workers: None,
};
println!("{}", "═══════════════════════════════════════════".cyan().bold());
println!("{}", " LLM Config Manager Security Scanner".cyan().bold());
println!("{}", "═══════════════════════════════════════════".cyan().bold());
println!();
println!("{} {}", "Scanning:".blue().bold(), cli.project.display());
println!();
let scanner = SecurityScanner::new(config);
let report = match scanner.scan() {
Ok(r) => r,
Err(e) => {
eprintln!("{} {}", "Error:".red().bold(), e);
process::exit(1);
}
};
print_summary(&report);
let report_content = match generate_report(&report, format) {
Ok(c) => c,
Err(e) => {
eprintln!("{} {}", "Error generating report:".red().bold(), e);
process::exit(1);
}
};
if let Some(output_path) = cli.output {
match write_report(&report, format, &output_path) {
Ok(()) => {
println!(
"{} {}",
"Report written to:".green().bold(),
output_path.display()
);
}
Err(e) => {
eprintln!("{} {}", "Error writing report:".red().bold(), e);
process::exit(1);
}
}
} else {
println!();
println!("{}", "─".repeat(50).dimmed());
println!();
println!("{}", report_content);
}
if cli.fail_on_high && report.has_high_severity() {
eprintln!();
eprintln!(
"{} {}",
"✗".red().bold(),
"High severity findings detected".red()
);
process::exit(1);
}
if report.summary.total > 0 {
println!();
println!(
"{} {}",
"⚠".yellow().bold(),
format!("{} findings - review recommended", report.summary.total).yellow()
);
process::exit(0);
}
println!();
println!(
"{} {}",
"✓".green().bold(),
"No security issues found!".green()
);
process::exit(0);
}
fn print_summary(report: &llm_config_devtools::security::SecurityReport) {
println!("{}", "Summary:".blue().bold());
println!(
" {} {}",
"Total:".dimmed(),
if report.summary.total > 0 {
report.summary.total.to_string().yellow()
} else {
report.summary.total.to_string().green()
}
);
println!(" {} {}", "Critical:".dimmed(), report.summary.critical.to_string().red());
println!(" {} {}", "High:".dimmed(), report.summary.high.to_string().bright_red());
println!(" {} {}", "Medium:".dimmed(), report.summary.medium.to_string().yellow());
println!(" {} {}", "Low:".dimmed(), report.summary.low.to_string().blue());
println!(
" {} {:.2}s",
"Duration:".dimmed(),
report.summary.duration_seconds
);
println!();
}