use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use tokio;
use loregrep::cli_main::{AnalyzeArgs, CliApp, CliConfig, ExecToolArgs, ScanArgs, SearchArgs};
#[derive(Parser)]
#[command(name = "loregrep")]
#[command(about = "Lightweight Code Analysis Tool with AI-powered search")]
#[command(
long_about = "Loregrep analyzes codebases using tree-sitter parsing and provides AI-powered natural language search capabilities"
)]
#[command(version)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
#[arg(short, long, global = true, default_value = ".")]
pub directory: PathBuf,
#[arg(short, long, global = true)]
pub verbose: bool,
#[arg(long, global = true)]
pub no_color: bool,
#[arg(short, long, global = true)]
pub config: Option<PathBuf>,
}
#[derive(Subcommand)]
pub enum Commands {
Scan(ScanArgs),
Search(SearchArgs),
Analyze(AnalyzeArgs),
ExecTool(ExecToolArgs),
Config,
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let cli = Cli::parse();
let config = CliConfig::load(cli.config.as_deref())?;
let mut app = CliApp::new(config, cli.verbose, !cli.no_color).await?;
match cli.command {
Commands::Scan(mut args) => {
if args.path == PathBuf::from(".") {
args.path = cli.directory;
}
app.scan(args).await
}
Commands::Search(mut args) => {
if args.path == PathBuf::from(".") {
args.path = cli.directory;
}
app.search(args).await
}
Commands::Analyze(mut args) => {
if args.file == PathBuf::from(".") {
args.file = cli.directory;
} else if args.file.is_relative() {
args.file = cli.directory.join(args.file);
}
app.analyze(args).await
}
Commands::ExecTool(mut args) => {
if args.path == PathBuf::from(".") {
args.path = cli.directory.clone();
}
app.exec_tool(args).await
}
Commands::Config => app.show_config().await,
}
}