reeve-cli 1.1.2

Localhost web dev stack manager: web servers, per-vhost PHP versions, SSL, and DNS — RunCloud, scaled down.
//! Command-line surface. With no subcommand, the TUI dashboard opens
//! (mirrors the ytunnel convention).

use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[command(
    name = "reeve",
    version,
    about = "Localhost web dev stack manager — web servers, per-vhost PHP, SSL, DNS.",
    long_about = None
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Detect Homebrew, scaffold config/state, set up local CA + DNS.
    Init,

    /// Manage PHP versions and their FPM masters.
    #[command(subcommand)]
    Php(PhpCommands),

    /// Manage web server instances.
    #[command(subcommand)]
    Server(ServerCommands),

    /// Manage virtual hosts.
    #[command(subcommand)]
    Vhost(VhostCommands),

    /// Manage background services (databases, redis, memcached, mailpit).
    #[command(subcommand)]
    Service(ServiceCommands),

    /// Park a directory so every subfolder auto-serves as <folder>.<tld>.
    #[command(subcommand)]
    Park(ParkCommands),

    /// Turn Xdebug on or off, or show its status. Acts on the site you're in,
    /// else the default PHP; name a version or site to pick another.
    Xdebug {
        /// on | off | toggle | debug | profile | status (default: status).
        action: Option<String>,
        /// PHP version (e.g. 8.3) or site (e.g. grav.test).
        target: Option<String>,
        /// Apply to every managed PHP version.
        #[arg(long, conflicts_with = "target")]
        all: bool,
    },

    /// Render generated configs and reconcile running services to state.
    Apply,

    /// Run every backend's native config test.
    Validate,

    /// Manage local SSL certificates.
    #[command(subcommand)]
    Ssl(SslCommands),

    /// Manage local wildcard DNS for the dev TLD (default .test).
    #[command(subcommand)]
    Dns(DnsCommands),

    /// View a managed service's log. Omit the target to list available logs.
    Logs {
        /// Target: server-<name>, php-<ver>, dnsmasq (or a bare server name).
        target: Option<String>,
        /// Number of trailing lines to show.
        #[arg(long, short = 'n', default_value_t = 50)]
        lines: usize,
        /// Stream new lines as they arrive (like `tail -f`).
        #[arg(long, short)]
        follow: bool,
    },

    /// Open the dashboard straight into the live traffic monitor.
    Traffic,

    /// Diagnose the stack: Homebrew, servers, FPM, DNS, certs, ports.
    Doctor,

    /// Self-update reeve to the latest GitHub release.
    Update {
        /// Only report whether an update is available; don't install.
        #[arg(long)]
        check: bool,
    },

    /// (hidden) Render one TUI frame to text — for testing without a terminal.
    #[command(hide = true)]
    TuiSnapshot {
        #[arg(long, default_value_t = 102)]
        width: u16,
        #[arg(long, default_value_t = 26)]
        height: u16,
        /// Open a modal for the snapshot: "wizard" (new vhost), "server" (edit),
        /// "modules" (Apache module picker), and so on.
        #[arg(long, default_value = "")]
        modal: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum DnsCommands {
    /// Install dnsmasq, resolve *.<tld> to 127.0.0.1, and wire up the resolver.
    Setup,
    /// Show DNS service + resolver status.
    Status,
}

#[derive(Subcommand, Debug)]
pub enum PhpCommands {
    /// Install a PHP version (e.g. `8.3`) and stand up its FPM master.
    Install { version: String },
    /// Repair a version whose Homebrew keg went away: reinstall php@<version>
    /// if that formula exists yet, otherwise adopt the unversioned `php` keg
    /// holding the series, then restart FPM.
    Pin { version: String },
    /// List installed PHP versions and FPM status.
    List,
    /// Restart a version's FPM master, re-applying its settings.
    Restart { version: String },
    /// Set the default PHP version for new vhosts.
    Use { version: String },
    /// Switch the CLI `php` (via the ~/.reeve/bin shim). Omit the version to
    /// show the current one.
    Cli { version: Option<String> },
    /// Manage PHP extensions for a version.
    #[command(subcommand)]
    Ext(ExtCommands),
    /// Show effective php.ini / OPcache / FPM settings for a version.
    Settings { version: String },
    /// Set a php.ini / OPcache / FPM setting (see `php settings <ver>` for keys).
    Set {
        version: String,
        key: String,
        value: String,
    },
    /// Set Xdebug for a version: on | off | toggle | debug | profile. `debug`
    /// attaches only to requests carrying XDEBUG_SESSION/XDEBUG_TRIGGER;
    /// `profile` profiles every request. See also `reeve xdebug`.
    Xdebug { version: String, mode: String },
}

#[derive(Subcommand, Debug)]
pub enum ExtCommands {
    /// Enable/install an extension for a PHP version.
    Add { version: String, name: String },
    /// Disable an extension for a PHP version.
    Remove { version: String, name: String },
    /// List extensions for a PHP version.
    List { version: String },
}

#[derive(Subcommand, Debug)]
pub enum ServerCommands {
    /// Add a server instance.
    Add {
        /// Backend: caddy|apache|nginx|ols
        backend: String,
        /// Instance name (defaults to the backend name).
        #[arg(long)]
        name: Option<String>,
        #[arg(long, default_value_t = 80)]
        http: u16,
        #[arg(long, default_value_t = 443)]
        https: u16,
        /// Serve a catch-all default site (sites root) on the HTTP port.
        #[arg(long)]
        default_site: bool,
        /// Docroot for the default site. Defaults to the global sites root.
        #[arg(long)]
        root: Option<String>,
        /// Framework preset for the default site: generic|laravel|wordpress|symfony|grav|drupal|public.
        #[arg(long, default_value = "generic")]
        preset: String,
    },
    /// Start a server.
    Start { name: String },
    /// Stop a server.
    Stop { name: String },
    /// Restart a server (picks up config changes).
    Restart { name: String },
    /// List servers and their status.
    List,
    /// Manage an Apache server's loaded modules.
    #[command(subcommand)]
    Mod(ModCommands),
    /// Remove a server.
    Remove { name: String },
}

#[derive(Subcommand, Debug)]
pub enum ModCommands {
    /// List modules the installed httpd ships and which are loaded.
    List {
        /// Server instance name.
        name: String,
        /// Show every available module, not just the loaded ones.
        #[arg(long, short)]
        all: bool,
    },
    /// Enable a module (and anything it needs) for a server.
    Add {
        /// Server instance name.
        name: String,
        /// Module, e.g. `deflate`, `mod_deflate` or `deflate_module`.
        module: String,
    },
    /// Disable a module for a server.
    Remove {
        /// Server instance name.
        name: String,
        /// Module, e.g. `deflate`, `mod_deflate` or `deflate_module`.
        module: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum VhostCommands {
    /// Add a vhost.
    Add {
        /// Hostname, e.g. grav.test
        server_name: String,
        #[arg(long)]
        root: String,
        /// PHP version (not needed for a `--proxy` vhost).
        #[arg(long, default_value = "")]
        php: String,
        #[arg(long)]
        server: String,
        #[arg(long)]
        ssl: bool,
        /// Framework preset: generic|laravel|wordpress|symfony|grav|drupal|public.
        #[arg(long, default_value = "generic")]
        preset: String,
        /// Make this a reverse proxy to an upstream URL (e.g. http://localhost:5173).
        /// Mutually exclusive with PHP serving.
        #[arg(long)]
        proxy: Option<String>,
    },
    /// List vhosts.
    List,
    /// Show how a host is actually served: docroot, preset, PHP, and the
    /// `.reeve.toml` (if any) feeding it.
    Show {
        /// Hostname, e.g. grav.test
        server_name: String,
    },
    /// Remove a vhost.
    Remove { server_name: String },
}

#[derive(Subcommand, Debug)]
pub enum ParkCommands {
    /// Park a directory: subfolders auto-serve as <folder>.<tld>.
    Add {
        /// Directory to park (e.g. ~/Sites).
        dir: String,
        #[arg(long)]
        server: String,
        #[arg(long)]
        php: String,
        /// TLD for parked hosts (defaults to your first configured TLD).
        #[arg(long)]
        tld: Option<String>,
        #[arg(long)]
        ssl: bool,
    },
    /// Stop parking a directory.
    Remove { dir: String },
    /// List parked directories and the sites they currently expand to.
    List,
}

#[derive(Subcommand, Debug)]
pub enum ServiceCommands {
    /// Add a service (mysql|mariadb|postgres|redis|memcached|mailpit) to state.
    Add { kind: String },
    /// Install if needed, then start a service.
    Start { kind: String },
    /// Stop a service.
    Stop { kind: String },
    /// Restart a service.
    Restart { kind: String },
    /// List services and their status.
    List,
    /// Show a service's configurable ports and their current values.
    Ports { kind: String },
    /// Re-point a service's port, e.g. `reeve service set mailpit smtp 1026`.
    /// Single-port services (databases, redis, memcached) take the key `port`.
    /// Restarts the service when it's running; the port is remembered in
    /// `state.toml` and reused on every later start.
    Set {
        kind: String,
        /// Port key — see `reeve service ports <kind>`.
        key: String,
        port: u16,
    },
    /// Remove a service from state (leaves the brew formula installed).
    Remove { kind: String },
}

#[derive(Subcommand, Debug)]
pub enum SslCommands {
    /// Mint a certificate for a hostname from the shared local CA.
    Mint { server_name: String },
    /// Install the mkcert local CA into the system trust store (may prompt for
    /// admin authorization). Run once so browsers trust local certs.
    Trust,
    /// Remove the mkcert local CA from the trust store (browsers stop trusting
    /// local certs). The CA files stay on disk for a later `trust`.
    Untrust,
    /// Show whether the local CA is generated and trusted, and where it lives.
    Status,
    /// Print the local CA root certificate path.
    Ca,
}