chyabrung-cli 0.1.0

The `chy` command line tool for Chyabrung: scaffolding, dev server with hot reload, builds, generators, migrations and one-command deploys
//! `chy` — the Chyabrung command line tool.

#![forbid(unsafe_code)]

mod build;
mod db;
mod deploy;
mod dev;
mod make;
mod new;
mod util;

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "chy", version, about = "🥁 Chyabrung — build with rhythm", propagate_version = true)]
struct Cli {
    #[command(subcommand)]
    cmd: Cmd,
}

#[derive(Subcommand)]
enum Cmd {
    /// Create a new app from a template (api, blog, saas)
    New {
        /// App name (directory)
        name: String,
        /// Template: api, blog or saas
        #[arg(short, long, default_value = "api")]
        template: String,
        /// Path to a local Chyabrung checkout to depend on
        #[arg(long)]
        chyabrung_path: Option<String>,
    },
    /// Run the dev server with hot reload
    Dev {
        /// Extra cargo features
        #[arg(long)]
        features: Option<String>,
        /// Live-reload port
        #[arg(long, default_value_t = 35729)]
        reload_port: u16,
    },
    /// Optimized release build (binary, CSS, WASM islands, optional static export)
    Build {
        /// Also pre-render pages to dist/site
        #[arg(long = "static")]
        static_site: bool,
        /// Skip the WASM build even if the crate supports hydration
        #[arg(long)]
        no_wasm: bool,
    },
    /// Generate a model (use --migration to add a migration)
    #[command(name = "make:model")]
    MakeModel {
        name: String,
        #[arg(short, long)]
        migration: bool,
    },
    /// Generate a controller with CRUD handlers
    #[command(name = "make:controller")]
    MakeController { name: String },
    /// Generate a migration (`create_posts_table` pre-fills CREATE TABLE)
    #[command(name = "make:migration")]
    MakeMigration { name: String },
    /// Generate a middleware
    #[command(name = "make:middleware")]
    MakeMiddleware { name: String },
    /// Generate a background job
    #[command(name = "make:job")]
    MakeJob { name: String },
    /// Generate a page under app/ (e.g. `blog/[slug]`)
    #[command(name = "make:page")]
    MakePage { path: String },
    /// Generate a seeder SQL file
    #[command(name = "make:seeder")]
    MakeSeeder { name: String },
    /// Run pending migrations
    Migrate,
    /// Roll back the last batch of migrations
    #[command(name = "migrate:rollback")]
    MigrateRollback {
        #[arg(long, default_value_t = 1)]
        step: u32,
    },
    /// Show migration status
    #[command(name = "migrate:status")]
    MigrateStatus,
    /// Roll back everything and migrate again
    #[command(name = "migrate:fresh")]
    MigrateFresh,
    /// Run seeders (database/seeds/*.sql, then src/bin/seed.rs if present)
    #[command(name = "db:seed")]
    DbSeed,
    /// Generate APP_KEY in .env
    #[command(name = "key:generate")]
    KeyGenerate {
        /// Print instead of writing .env
        #[arg(long)]
        show: bool,
    },
    /// Deploy to a VPS: build, upload, systemd, Nginx and Let's Encrypt
    Deploy {
        /// Print the plan and generated files without running anything
        #[arg(long)]
        dry_run: bool,
    },
}

fn main() {
    let cli = Cli::parse();
    let result = match cli.cmd {
        Cmd::New { name, template, chyabrung_path } => new::run(&name, &template, chyabrung_path.as_deref()),
        Cmd::Dev { features, reload_port } => dev::run(features.as_deref(), reload_port),
        Cmd::Build { static_site, no_wasm } => build::run(static_site, !no_wasm),
        Cmd::MakeModel { name, migration } => make::model(&name, migration),
        Cmd::MakeController { name } => make::controller(&name),
        Cmd::MakeMigration { name } => make::migration(&name).map(|_| ()),
        Cmd::MakeMiddleware { name } => make::middleware(&name),
        Cmd::MakeJob { name } => make::job(&name),
        Cmd::MakePage { path } => make::page(&path),
        Cmd::MakeSeeder { name } => make::seeder(&name),
        Cmd::Migrate => db::migrate(),
        Cmd::MigrateRollback { step } => db::rollback(step),
        Cmd::MigrateStatus => db::status(),
        Cmd::MigrateFresh => db::fresh(),
        Cmd::DbSeed => db::seed(),
        Cmd::KeyGenerate { show } => util::key_generate(show),
        Cmd::Deploy { dry_run } => deploy::run(dry_run),
    };
    if let Err(e) = result {
        eprintln!("\x1b[31merror:\x1b[0m {e}");
        std::process::exit(1);
    }
}