use opengrep::{search::SearchEngine, Config, SearchConfig, OutputConfig};
use std::path::PathBuf;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = Config {
search: SearchConfig {
ignore_case: true,
regex: true,
follow_symlinks: false,
max_depth: Some(3),
max_file_size: Some(1024 * 1024), threads: 4,
hidden: false,
respect_gitignore: true,
},
output: OutputConfig {
color: true,
line_numbers: true,
before_context: 3,
after_context: 3,
show_ast_context: true,
max_ast_depth: 2,
highlight: true,
},
#[cfg(feature = "ai")]
ai: None,
};
config.save_to_file("opengrep.toml")?;
println!("Configuration saved to opengrep.toml");
let loaded_config = Config::from_file("opengrep.toml")?;
println!("Configuration loaded from file");
let engine = SearchEngine::new(loaded_config);
let results = engine.search(r"pub\s+(fn|struct|enum)", &[PathBuf::from("src")]).await?;
println!("\nFound {} files with public items:", results.len());
for result in results {
println!("\n{} ({} matches)",
result.path.display(),
result.matches.len()
);
for match_item in result.matches.iter().take(3) {
println!(" {}:{} - {}",
match_item.line_number,
match_item.column_range.start,
match_item.line_text.trim()
);
}
if result.matches.len() > 3 {
println!(" ... and {} more matches", result.matches.len() - 3);
}
}
std::fs::remove_file("opengrep.toml").ok();
Ok(())
}