o402 0.1.4

OpenAI-compatible gateway, paid with x402.
//! CLI: `o402 init` and `o402 serve`.

pub(crate) mod init;
pub(crate) mod serve;

use std::path::PathBuf;

use clap::{Parser, Subcommand};

use crate::error::AppError;

/// OpenAI-compatible gateway, paid with x402.
#[derive(Debug, Parser)]
#[command(
    name = "o402",
    version = env!("CARGO_PKG_VERSION"),
    about = "OpenAI-compatible gateway, paid with x402."
)]
struct Cli {
    /// Subcommand to run.
    #[command(subcommand)]
    command: Command,
}

/// Process subcommands.
#[derive(Debug, Subcommand)]
enum Command {
    /// Write `deploy/gateway.unpaid.example.toml` to `--path`.
    Init {
        /// Destination path.
        #[arg(long, default_value = "gateway.toml")]
        path: PathBuf,
    },
    /// Load TOML and serve `/health` `/ready` plus the billed proxy.
    Serve {
        /// Path to the gateway TOML file.
        #[arg(long)]
        config: PathBuf,
    },
}

/// Dispatch `init` or `serve`.
///
/// # Errors
///
/// Returns [`AppError`] from the selected subcommand.
pub(crate) async fn run() -> Result<(), AppError> {
    match Cli::parse().command {
        Command::Init { path } => init::run(&path),
        Command::Serve { config } => serve::run(&config).await,
    }
}