#![deny(unsafe_code)]
use anyhow::Result;
use clap::Parser;
use opengrep::{
cli::Cli,
config::Config,
init_logging,
output::{create_formatter, OutputFormat},
search::SearchEngine,
VERSION,
};
use std::io::stdout;
use std::process;
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
if cli.version {
println!("opengrep {}", VERSION);
process::exit(0);
}
if cli.list_languages {
opengrep::parsers::list_supported_languages();
process::exit(0);
}
let verbosity = cli.verbose;
init_logging(verbosity)?;
cli.validate()?;
if cli.interactive {
let config = Config::from_cli(&cli)?;
let engine = SearchEngine::new(config);
let results = engine.interactive_search().await?;
let formatter = create_formatter(OutputFormat::Text);
let mut writer = stdout();
formatter.format(&results, &mut writer, &Default::default())?;
return Ok(());
}
let config = Config::from_cli(&cli)?;
let pattern = &cli.pattern;
let paths = if cli.paths.is_empty() {
vec![std::path::PathBuf::from(".")]
} else {
cli.paths.clone()
};
let engine = SearchEngine::new(config.clone());
let results = engine.search(pattern, &paths).await?;
let output_format = match cli.output_format {
opengrep::cli::OutputFormat::Text => OutputFormat::Text,
opengrep::cli::OutputFormat::Json => OutputFormat::Json,
opengrep::cli::OutputFormat::Html => OutputFormat::Html,
opengrep::cli::OutputFormat::Xml => OutputFormat::Xml,
opengrep::cli::OutputFormat::Csv => OutputFormat::Csv,
};
let formatter = create_formatter(output_format);
let mut writer = stdout();
formatter.format(&results, &mut writer, &config.output)?;
Ok(())
}