falsegreen 0.1.2

FalseGreen client — independent verification for coding agents
mod shim;

use clap::Parser;
use shim::cli::{Cli, Commands};

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Mcp => {
            // This never returns — it's a STDIO pipe loop.
            shim::mcp::run();
        }
        Commands::Login { token } => {
            let token = match token {
                Some(t) => t,
                None => {
                    eprintln!("Enter your FalseGreen key: ");
                    let mut input = String::new();
                    std::io::stdin()
                        .read_line(&mut input)
                        .expect("failed to read input");
                    input.trim().to_string()
                }
            };
            if token.is_empty() {
                eprintln!("Key cannot be empty.");
                std::process::exit(1);
            }

            // Activate this device with Polar.
            let device_label = shim::device::device_label();
            println!("Activating device \"{}\"...", device_label);

            match shim::device::activate(&token, &device_label) {
                Ok(activation_id) => {
                    match shim::config::save_credentials(&token, &activation_id) {
                        Ok(_) => {
                            println!("Logged in. Device activated.");
                            println!("Credentials stored in {:?}.", shim::config::credentials_path());
                        }
                        Err(e) => {
                            eprintln!("Failed to store credentials: {}", e);
                            std::process::exit(1);
                        }
                    }
                }
                Err(e) => {
                    eprintln!("Activation failed: {}", e);
                    eprintln!("If you've reached your device limit, run `falsegreen logout` on another device.");
                    std::process::exit(1);
                }
            }
        }
        Commands::Logout => {
            // Deactivate the device with Polar if we have an activation ID.
            if let Some(key) = shim::config::load_token() {
                if let Some(activation_id) = shim::config::load_activation_id() {
                    print!("Deactivating device... ");
                    match shim::device::deactivate(&key, &activation_id) {
                        Ok(_) => println!("done."),
                        Err(e) => {
                            println!("failed: {}", e);
                            eprintln!("Removing local credentials anyway.");
                        }
                    }
                }
            }
            match shim::config::clear_token() {
                Ok(_) => println!("Logged out."),
                Err(e) => {
                    eprintln!("Failed to remove credentials: {}", e);
                    std::process::exit(1);
                }
            }
        }
        Commands::Status => {
            match shim::config::load_token() {
                Some(_) => {
                    println!("Authenticated. Key stored in {:?}.", shim::config::credentials_path());
                    if let Some(id) = shim::config::load_activation_id() {
                        println!("Device activation: {}", id);
                    }
                }
                None => println!("Not authenticated. Run `falsegreen login` to set up."),
            }
            println!("API endpoint: {}", shim::config::api_url());
        }
        Commands::Install { agent } => {
            match shim::install::install(&agent) {
                Ok(_) => {}
                Err(e) => {
                    eprintln!("Install failed: {}", e);
                    std::process::exit(1);
                }
            }
        }
    }
}