kodegraf-cli 0.1.0

Structural code intelligence for AI coding assistants — CLI
use anyhow::{Context, Result};
use kodegraf_core::config::Config;
use kodegraf_core::graph::GraphStore;

pub fn run(file: &str) -> Result<()> {
    let repo_root = kodegraf_core::config::find_repo_root()?;
    let config_path = Config::config_path(&repo_root);
    let db_path = Config::graph_db_path(&repo_root);

    if !db_path.exists() {
        anyhow::bail!("Graph not built. Run `kodegraf init && kodegraf build` first.");
    }

    let file_path = repo_root.join(file);
    if !file_path.exists() {
        anyhow::bail!("File not found: {}", file);
    }

    let config = Config::load(&config_path)?;
    let store = GraphStore::open(&db_path)?;

    let source = std::fs::read_to_string(&file_path)
        .with_context(|| format!("Failed to read {}", file))?;

    let results = kodegraf_checks::run_all_checks(
        file,
        &source,
        &store,
        &config.checks.severities,
    );

    let output = kodegraf_checks::format_results(&results);
    println!("{}", output);

    // Exit with non-zero if there are errors
    let has_errors = results
        .iter()
        .any(|r| r.severity == kodegraf_checks::Severity::Error);

    if has_errors {
        std::process::exit(1);
    }

    Ok(())
}