egrix 0.1.0

Find leaked credentials and suspicious network destinations in AI agent transcripts. Local, offline, redacted by default.
//! egrix: find leaked credentials and suspicious network destinations in AI
//! agent transcripts.
//!
//! How the crate is laid out, from the outside in:
//!
//! - `cli`, `commands`: the command line and one function per command.
//! - `tui`: the dashboard. It calls the same core as the commands.
//! - `report`: text, JSON and SARIF renderings of one `Report`.
//! - `scan`: reads the transcripts and builds the `Report`.
//! - `sources`, `git`, `rules`, `hosts`, `context`, `fix`: the pieces a scan
//!   and its follow-up actions are made of. Each is pure or owns one kind of
//!   I/O, and none depends on the layers above.
//! - `model`, `state`, `paths`, `time`: shared types and persistence.

mod cli;
mod commands;
mod context;
mod fix;
mod git;
mod hosts;
mod model;
mod paths;
mod report;
mod rules;
mod scan;
mod sources;
mod state;
mod time;
#[cfg(feature = "tui")]
mod tui;

use clap::Parser;
use cli::{Cli, Command, Filter, Output};
use std::process::ExitCode;

fn main() -> ExitCode {
    match run(Cli::parse()) {
        Ok(code) => code,
        Err(error) => {
            eprintln!("egrix: {error:#}");
            ExitCode::from(2)
        }
    }
}

fn run(cli: Cli) -> anyhow::Result<ExitCode> {
    match cli.command {
        None => default_command(),
        Some(Command::Scan { filter, output }) => commands::scan(&filter, &output),
        Some(Command::Secrets { filter, output, group, show }) => commands::secrets(&filter, &output, group, show.as_deref()),
        Some(Command::Egress { filter, output, show, all }) => commands::egress(&filter, &output, show.as_deref(), all),
        Some(Command::Fix { filter, id, undo, yes }) => commands::fix(&filter, id.as_deref(), undo, yes),
        Some(Command::Mark { id, rotated, ignored, clear }) => commands::mark(&id, rotated, ignored, clear),
        Some(Command::Hosts { action }) => commands::hosts(&action),
        Some(Command::Rules { action }) => commands::rules(&action),
        Some(Command::Baseline { filter }) => commands::baseline(&filter),
    }
}

/// Bare `egrix`: the dashboard for a person at a terminal, a plain summary for
/// a script or an agent. The dashboard never opens on captured output, so it
/// cannot cost an agent tokens.
fn default_command() -> anyhow::Result<ExitCode> {
    #[cfg(feature = "tui")]
    {
        use std::io::IsTerminal;
        if std::io::stdout().is_terminal() && std::io::stdin().is_terminal() {
            return tui::run();
        }
    }
    commands::scan(&Filter::default(), &Output::default())
}