rustio-admin-cli 0.27.1

Command-line tools for rustio-admin: project scaffolding, migrations, user management.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! `rustio-admin` -- command-line companion for the `rustio-admin` framework.
//!
//! Beginner surface (the verbs the welcome help promotes first;
//! see `docs/design/DESIGN_ONBOARDING.md` for the doctrine):
//!
//! - `rustio-admin new <name>` -- friendly alias for `startproject`.
//!   Identical behaviour, identical files, identical exit codes.
//! - `rustio-admin doctor` -- health check: reachable DB? auth tables
//!   present? migrations up to date? at least one administrator?
//! - `rustio-admin docs [--open]` -- print where the framework's docs
//!   live; probes the local server and reflects reachability
//!   in the output; `--open` launches the local docs in your
//!   default browser (PR 2.4).
//!
//! Full surface (still listed in `--help` after the welcome banner):
//!
//! - `rustio-admin startproject` / `startapp` -- original scaffolding
//!   verbs. `new` is the friendly alias; `startproject` keeps
//!   working unchanged for scripts.
//! - `rustio-admin migrate apply` / `status` -- numerically prefixed
//!   `migrations/*.sql` runner.
//! - `rustio-admin user create` / `list` / `role` / `delete` -- auth
//!   table CRUD with Argon2 hashing.
//! - `rustio-admin group create` / `list` / `add-user` -- group CRUD
//!   and membership.
//! - `rustio-admin perm grant-user` / `grant-group` / `list` -- permission
//!   grants on top of `auth::permissions`.
//! - `rustio-admin builder new` -- declarative-Builder bootstrap. The
//!   `rustio-admin new` slot was reclaimed for the friendly scaffold
//!   alias; the legacy entrypoint is reachable as the hidden
//!   `rustio-admin new --builder` for a one-release deprecation window.
//! - `rustio-admin add` / `plan` / `commit` -- Builder authoring,
//!   plan, and apply (`docs/design/DESIGN_BUILDER.md`). Unchanged.

use std::process::ExitCode;

use clap::{Parser, Subcommand};

mod ai;
mod app_fields;
mod audit;
mod builder;
mod docs;
mod doctor;
mod doctor_email;
mod emergency_ui;
mod group;
mod memory;
mod migrate;
mod perm;
mod progress;
mod proposal;
mod reload;
mod scaffold;
mod template_override;
mod test_init;
mod theme;
mod ui;
mod user;
mod wizard;

/// Welcome banner printed before clap's auto-generated `--help`
/// command list. Clarity-first within `DESIGN_ONBOARDING.md` §10: it leads
/// with *what RustIO is* (so a first-time reader understands before the
/// command list), then promotes the Phase 1 "Start here" surface. The full
/// command list still follows below — promoted, never amputated.
const WELCOME_HELP: &str = "\
RustIO Admin — a Postgres-first admin framework for Rust.

Describe your data as Rust structs and get a full admin panel, with
authentication, sessions, password recovery, and an audit trail already
built in. For clinics, schools, inventory, blogs — or anything custom.

Start here:

  rustio-admin new <project-name>     # guided setup, then a ready-to-run app

New to RustIO?

  rustio-admin doctor                 # check your environment is ready
  rustio-admin docs                   # find the documentation

Every command is listed below.
────────────────────────────────────────────────────────────\
";

#[derive(Parser)]
#[command(
    name = "rustio-admin",
    version,
    about = "The rustio-admin command-line tool.",
    before_help = WELCOME_HELP,
)]
struct Cli {
    /// Suppress progress spinners and status feedback. Errors still
    /// print. PR 1.4 of `DESIGN_ONBOARDING.md` §9. Synonym of
    /// `--no-progress`; either flag is enough on its own.
    #[arg(long, global = true)]
    quiet: bool,
    /// Suppress progress spinners. Equivalent to `--quiet`.
    #[arg(long, global = true)]
    no_progress: bool,
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    // ---- Beginner surface (promoted by the welcome banner) ----
    /// Create a new project (friendly alias for `startproject`).
    ///
    /// In an interactive terminal, a calm wizard collects the
    /// project name, project type, and database name, then writes
    /// a matching `.env` alongside the standard files (PR 1.2 of
    /// `DESIGN_ONBOARDING.md`). Under `--no-interactive`, when
    /// stdin/stdout is not a TTY, or under CI, the wizard is
    /// bypassed and behaviour is byte-identical to `startproject`.
    /// The legacy Builder bootstrap that used to live in this slot
    /// moved to `rustio-admin builder new`; the hidden `--builder` flag
    /// below is the one-release deprecation shim.
    New {
        /// Project name -- also the cargo crate name. Letters,
        /// digits, '-', and '_' only; must start with a letter.
        /// Optional: the wizard prompts for it when omitted in
        /// interactive mode. Required in non-interactive mode.
        name: Option<String>,
        /// Forwarded to `startproject`: preset choice. Defaults to
        /// `minimal` (no domain models — the scaffold is neutral so
        /// the project does not feel like someone else's demo);
        /// `blog` adds `Post` + `Comment` with their migrations. In
        /// wizard mode the user's project-type choice supersedes
        /// this flag; in non-interactive mode this flag wins.
        /// Ignored when `--builder` is set.
        #[arg(long, default_value = "minimal")]
        preset: String,
        /// Skip the interactive wizard even when running in a
        /// terminal. Behaviour matches `rustio-admin startproject` exactly
        /// -- required positional name, scaffold writes `.env.example`
        /// only (no auto-generated `.env`), same Next Steps block.
        #[arg(long)]
        no_interactive: bool,
        /// Deprecated: run the legacy Builder bootstrap. Use
        /// `rustio-admin builder new <name>` instead. Hidden from
        /// `--help`; one-release grace.
        #[arg(long, hide = true)]
        builder: bool,
    },

    /// Scaffold a new rustio-admin project at ./<name>.
    #[command(name = "startproject")]
    Startproject {
        /// Name of the project -- also the cargo crate name. Letters,
        /// digits, '-', and '_' only.
        name: String,
        /// Project preset: `minimal` (default -- neutral scaffold,
        /// no domain models) or `blog` (adds `Post` + `Comment`
        /// with their migrations). Unknown presets error out with
        /// the valid list.
        #[arg(long, default_value = "minimal")]
        preset: String,
    },
    /// Scaffold a new model + migration inside the current project.
    #[command(name = "startapp")]
    Startapp {
        /// Singular lowercase identifier (e.g. `post`, `course`,
        /// `book_review`). Becomes both the module file name and
        /// the snake_case prefix; the struct gets the CamelCase
        /// form (`Post`, `BookReview`); the table gets the
        /// pluralised form (`posts`, `book_reviews`).
        name: String,
        /// Field declaration in `<name>:<type>` form, repeatable.
        /// Closed vocabulary: `str / text / int / bigint / bool /
        /// timestamp / json / fk:<Model>`. PR 2.1 of
        /// `DESIGN_ONBOARDING.md`. Omit the flag entirely (or
        /// run interactively at a TTY) to get the historical
        /// one-field placeholder model.
        #[arg(long = "field", value_name = "NAME:TYPE")]
        fields: Vec<String>,
        /// Skip the interactive field-prompt loop even when running
        /// in a terminal. Behaviour matches non-TTY / CI exactly --
        /// if no `--field` flags are passed, the historical
        /// placeholder model is generated.
        #[arg(long)]
        no_interactive: bool,
    },
    /// Apply / inspect SQL migrations from a directory.
    Migrate {
        #[command(subcommand)]
        action: migrate::Action,
    },
    /// User management.
    User {
        #[command(subcommand)]
        action: user::Action,
    },
    /// Group management.
    Group {
        #[command(subcommand)]
        action: group::Action,
    },
    /// Permission management.
    Perm {
        #[command(subcommand)]
        action: perm::Action,
    },
    /// Inspect the audit trail (rustio_admin_actions). Read-only.
    Audit {
        #[command(subcommand)]
        action: audit::Action,
    },
    /// Diagnose the local environment.
    Doctor {
        #[command(subcommand)]
        action: Option<DoctorAction>,
    },

    /// Print where the framework's documentation lives.
    ///
    /// Probes `http://127.0.0.1:8000/admin/health` with a short
    /// timeout to detect a running local server; the printed URL
    /// block reflects whether the local `/admin/docs` is reachable.
    /// Pass `--open` to launch the local docs in your default
    /// browser (no-op without a running server; exits 1).
    Docs {
        /// Open the local `/admin/docs` page in your default
        /// browser. Requires the project's server to be running
        /// (`cargo run`). Exits 1 if the server is unreachable
        /// or the browser opener fails. Default off — auto-
        /// opening a browser is the kind of magic
        /// `DESIGN_ONBOARDING.md` §4 forbids.
        #[arg(long)]
        open: bool,
    },

    /// Curated `AdminTheme` palette presets. Subcommands print a
    /// Rust snippet to stdout -- the operator pastes it into their
    /// `Admin::new()` builder chain. The verb never touches
    /// `main.rs` or any other project file.
    Theme {
        #[command(subcommand)]
        action: theme::Action,
    },

    /// AI assistant permissions. `status` shows what an AI coding
    /// assistant may do here (Allowed / Needs approval / Blocked) by
    /// reading `.rustio/ai.toml`; `init` writes a default policy. Offline
    /// and read-only -- no AI is contacted, no database is opened. See
    /// `docs/design/DESIGN_AI_ASSISTANT.md`.
    Ai {
        #[command(subcommand)]
        action: ai::Action,
    },

    /// Project memory (CLOUD.md). `render` regenerates the human-readable
    /// CLOUD.md from the per-entry files; `show` lists entries with exact
    /// filters; `verify` checks the view is fresh and the entries are
    /// well-formed. Offline and read-only -- no AI, no database. See
    /// `docs/design/DESIGN_CLOUD.md` and `DESIGN_CLOUD_IMPL.md`.
    Memory {
        #[command(subcommand)]
        action: memory::Action,
    },

    /// Copy an embedded admin template into the project's
    /// `templates/` directory so it can be edited. Pair with
    /// `RUSTIO_TEMPLATE_DIR=./templates` at runtime to make the
    /// override take effect. With no arguments, lists every
    /// available template name.
    #[command(name = "override")]
    Override {
        /// Canonical template name (e.g. `admin/list.html`). Omit
        /// to list every available template.
        name: Option<String>,
        /// Overwrite an existing on-disk file with the framework
        /// default. Off by default -- the verb refuses to clobber so
        /// in-progress edits stay safe.
        #[arg(long)]
        force: bool,
        /// Destination root for the copy. Defaults to `./templates`
        /// (matches the `RUSTIO_TEMPLATE_DIR=./templates` convention
        /// from CLAUDE.md / `docs/architecture.md`).
        #[arg(long, default_value = "templates")]
        out: String,
    },

    /// Watch the project's source tree and re-run `cargo run` on
    /// change. Thin wrapper around `cargo watch -x run`. Requires
    /// `cargo-watch` to be installed; the verb surfaces a friendly
    /// install message when it isn't.
    Reload,

    /// Generate a starter integration test at `./<out>/smoke.rs`.
    /// The test spawns the project binary, probes the bound port,
    /// and asserts that `/admin/` redirects to `/admin/login` --
    /// useful as a project's first CI check.
    #[command(name = "test-init")]
    TestInit {
        /// Overwrite an existing `<out>/smoke.rs`. Off by default
        /// so the verb refuses to clobber in-progress edits.
        #[arg(long)]
        force: bool,
        /// Destination root for the test file. Defaults to
        /// `./tests` (Cargo's integration-test convention).
        #[arg(long, default_value = "tests")]
        out: String,
    },

    // -- Builder verbs (DESIGN_BUILDER.md). All run synchronously
    // and require no database -- Doctrine B9 (network-free). The
    // top-level `new` slot was reclaimed for the friendly scaffold
    // alias; the canonical Builder bootstrap is now `rustio-admin builder
    // new`. `add`, `plan`, `commit` keep their top-level positions
    // for script compatibility.
    /// Builder workflow namespace (`DESIGN_BUILDER.md`).
    ///
    /// Currently exposes `new` (project bootstrap); `add`, `plan`,
    /// and `commit` keep their top-level positions for script
    /// compatibility.
    Builder {
        #[command(subcommand)]
        action: BuilderAction,
    },

    /// Builder authoring verbs.
    Add {
        #[command(subcommand)]
        action: BuilderAddAction,
    },

    /// Show the diff `commit` would apply. Read-only (Doctrine B8).
    Plan,

    /// Apply the plan atomically (Doctrine B8).
    Commit {
        /// Overwrite generator-owned files whose SchemaHash does
        /// not match the current draft. Prior content is
        /// quarantined to `.rustio/forced/<ts>/<path>` first
        /// (DESIGN_BUILDER.md §5.4).
        #[arg(long)]
        force: bool,
    },
}

#[derive(Subcommand)]
enum BuilderAction {
    /// Bootstrap a new Builder-managed project at ./<name>.
    ///
    /// Writes `<name>/{Cargo.toml,src/main.rs,.rustio/{draft.toml,history.jsonl,builder.lock},migrations/}`.
    /// See `docs/design/DESIGN_BUILDER.md`.
    New {
        /// Project name -- also the cargo crate name. Letters,
        /// digits, `-`, and `_` only; must start with a letter.
        name: String,
    },
}

#[derive(Subcommand)]
enum BuilderAddAction {
    /// Record an `add_model` event in `.rustio/history.jsonl`.
    Model {
        /// CamelCase model name (e.g. `Patient`).
        name: String,
    },
    /// Record an `add_field` event in `.rustio/history.jsonl`.
    Field {
        /// CamelCase model name the field belongs to.
        model: String,
        /// snake_case field name (e.g. `full_name`).
        name: String,
        /// Declared field type. MVP closed list: text, integer,
        /// boolean, timestamp.
        #[arg(name = "type")]
        type_name: String,
        /// Add a UNIQUE constraint to this column.
        #[arg(long)]
        unique: bool,
    },
}

#[derive(Subcommand)]
enum DoctorAction {
    /// SMTP self-validation. Reads `SMTP_*` + `MAIL_FROM` (or
    /// `MAIL_PROVIDER` for a preset host/port/TLS) from the
    /// environment, opens a TLS + AUTH handshake against the
    /// configured server, and optionally sends a single test
    /// message when `--to <address>` is supplied. No credentials
    /// are echoed -- the password is reported as `(set, N chars)`.
    ///
    /// `--html-preview` skips SMTP entirely and renders the
    /// recovery-email template to /tmp + opens it in the
    /// default browser. Useful for iterating on email design.
    Email {
        /// Optional recipient. When set, the doctor sends a tiny
        /// test message after the handshake passes. When omitted,
        /// the handshake is the deepest check (no email goes out).
        /// 30-second cooldown between consecutive `--to` sends
        /// (prevents accidental spam loops).
        #[arg(long)]
        to: Option<String>,
        /// Render the recovery-email body to /tmp and open it
        /// in your default browser. No SMTP traffic.
        #[arg(long)]
        html_preview: bool,
    },
}

fn main() -> ExitCode {
    // .env is optional; production deploys typically use real env vars.
    let _ = dotenvy::dotenv();
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();

    let cli = match Cli::try_parse() {
        Ok(c) => c,
        Err(e) => {
            // Intercept clap's InvalidValue error (e.g. `--role admin`
            // when valid roles are user/staff/...) and reformat using
            // the four-part onboarding shape from DESIGN_ONBOARDING.md §8.
            // Every other clap error (missing arg, --help, --version, …)
            // falls through to clap's default printing untouched.
            if let Some(rewritten) = rewrite_clap_invalid_value(&e) {
                eprintln!("{}", rewritten.format());
                return ExitCode::from(2);
            }
            e.exit();
        }
    };
    // Wire the global `--quiet` / `--no-progress` flags into the
    // progress layer before any Step::start is reachable. PR 1.4 /
    // DESIGN_ONBOARDING.md §9.
    progress::configure(cli.quiet, cli.no_progress);
    let result = match cli.command {
        // Pure filesystem; no async / db needed. Builder verbs also
        // sit here -- DESIGN_BUILDER.md Doctrine B9 forbids network
        // calls in plan/commit, and `new` / `add` are even simpler.
        Command::New {
            name,
            preset,
            no_interactive,
            builder,
        } => dispatch_new(name, preset, no_interactive, builder),
        Command::Startproject { name, preset } => scaffold::project(&name, &preset),
        Command::Startapp {
            name,
            fields,
            no_interactive,
        } => scaffold::app(&name, fields, no_interactive),
        Command::Builder { action } => match action {
            BuilderAction::New { name } => builder_new(&name),
        },
        Command::Add { action } => builder_add(action),
        Command::Plan => builder_plan(),
        Command::Commit { force } => builder_commit(force),
        Command::Docs { open } => docs::print_docs(open),
        Command::Override { name, force, out } => template_override::run(name, force, &out),
        Command::Reload => reload::run(),
        Command::TestInit { force, out } => test_init::run(force, &out),
        Command::Theme { action } => theme::run(action),
        Command::Ai { action } => ai::run(action),
        Command::Memory { action } => memory::run(action),
        // Everything else opens a Postgres connection.
        other => tokio_run(async {
            match other {
                Command::New { .. }
                | Command::Startproject { .. }
                | Command::Startapp { .. }
                | Command::Builder { .. }
                | Command::Add { .. }
                | Command::Plan
                | Command::Commit { .. }
                | Command::Docs { .. }
                | Command::Override { .. }
                | Command::Reload
                | Command::TestInit { .. }
                | Command::Theme { .. }
                | Command::Ai { .. }
                | Command::Memory { .. } => unreachable!("handled above"),
                Command::Migrate { action } => migrate::run(action).await,
                Command::User { action } => user::run(action).await,
                Command::Group { action } => group::run(action).await,
                Command::Perm { action } => perm::run(action).await,
                Command::Audit { action } => audit::run(action).await,
                Command::Doctor { action } => match action {
                    None => doctor::run().await,
                    Some(DoctorAction::Email { to, html_preview }) => {
                        doctor_email::run(to, html_preview).await
                    }
                },
            }
        }),
    };
    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            // Four-part onboarding errors (DESIGN_ONBOARDING.md §8)
            // carry their own structural headline; the `error: ` label
            // would just clutter it. Plain-string errors keep the
            // label so non-rewritten paths look unchanged.
            if e.starts_with(ui::ONBOARDING_SENTINEL) {
                eprintln!("{e}");
            } else {
                eprintln!("error: {e}");
            }
            ExitCode::FAILURE
        }
    }
}

/// Inspect a clap parse error and, if it's an `InvalidValue` (a
/// `ValueEnum` mismatch like `--role admin`), build the matching
/// four-part [`ui::OnboardingError`]. Returns `None` for every
/// other clap error kind so the default printing still runs.
fn rewrite_clap_invalid_value(e: &clap::Error) -> Option<ui::OnboardingError> {
    use clap::error::{ContextKind, ContextValue, ErrorKind};
    if e.kind() != ErrorKind::InvalidValue {
        return None;
    }
    let arg = match e.get(ContextKind::InvalidArg)? {
        ContextValue::String(s) => s.clone(),
        _ => return None,
    };
    let bad = match e.get(ContextKind::InvalidValue)? {
        ContextValue::String(s) => s.clone(),
        _ => return None,
    };
    let valid: Vec<String> = match e.get(ContextKind::ValidValue)? {
        ContextValue::Strings(v) => v.clone(),
        _ => return None,
    };
    Some(ui::invalid_value(&arg, &bad, &valid))
}

/// Builder bootstrap dispatch -- pure filesystem, no async.
/// Reached via `rustio-admin builder new <name>` (canonical) and via
/// the hidden one-release `rustio-admin new --builder <name>` shim.
fn builder_new(name: &str) -> Result<(), String> {
    let summary = builder::cmd::run_new(name)?;
    println!("{summary}");
    Ok(())
}

/// `rustio-admin new` dispatch -- wizard or legacy scaffold path.
///
/// Decision order: `--builder` → legacy Builder shim (PR 1.1); else
/// if the wizard is eligible (TTY, not CI, not `--no-interactive`)
/// → run the wizard and emit a matching `.env`; else → call
/// `scaffold::project` exactly as PR 1.1 shipped it (byte-identical,
/// no `.env`, mandatory positional name).
fn dispatch_new(
    name: Option<String>,
    preset: String,
    no_interactive: bool,
    builder: bool,
) -> Result<(), String> {
    if builder {
        let name =
            name.ok_or_else(|| "`rustio-admin new --builder <name>` requires a name".to_string())?;
        eprintln!("note: `rustio-admin new --builder` is the legacy Builder entrypoint.");
        eprintln!("      Prefer `rustio-admin builder new <name>` going forward.");
        eprintln!();
        return builder_new(&name);
    }
    if wizard::should_run(no_interactive) {
        let input = wizard::run(name.as_deref())?;
        // PR 1.5: project_type drives preset selection — only the
        // explicit `blog` choice writes blog-shaped files. Every
        // other type (custom / clinic / school / inventory) gets
        // the neutral minimal scaffold and a project-type-aware
        // homepage. `DESIGN_ONBOARDING.md` §6.
        let resolved_preset = if input.project_type == "blog" {
            "blog"
        } else {
            "minimal"
        };
        return scaffold::project_with_db(
            &input.project_name,
            resolved_preset,
            &input.db_name,
            &input.project_type,
        );
    }
    // Non-interactive path -- name is mandatory (matches `startproject`).
    let name = name.ok_or_else(|| {
        "project name is required in non-interactive mode (try `rustio-admin new <name>`)"
            .to_string()
    })?;
    scaffold::project(&name, &preset)
}

/// `rustio-admin add ...` dispatch.
fn builder_add(action: BuilderAddAction) -> Result<(), String> {
    let cwd = std::env::current_dir().map_err(|e| e.to_string())?;
    let msg = match action {
        BuilderAddAction::Model { name } => builder::cmd::run_add_model(&cwd, &name)?,
        BuilderAddAction::Field {
            model,
            name,
            type_name,
            unique,
        } => builder::cmd::run_add_field(&cwd, &model, &name, &type_name, unique)?,
    };
    println!("{msg}");
    Ok(())
}

/// `rustio-admin plan` dispatch.
fn builder_plan() -> Result<(), String> {
    let cwd = std::env::current_dir().map_err(|e| e.to_string())?;
    let out = builder::cmd::run_plan(&cwd)?;
    print!("{out}");
    Ok(())
}

/// `rustio-admin commit` dispatch.
fn builder_commit(force: bool) -> Result<(), String> {
    let cwd = std::env::current_dir().map_err(|e| e.to_string())?;
    let out = builder::cmd::run_commit(&cwd, force)?;
    print!("{out}");
    Ok(())
}

fn tokio_run<F>(fut: F) -> Result<(), String>
where
    F: std::future::Future<Output = Result<(), String>>,
{
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .map_err(|e| format!("tokio runtime: {e}"))?
        .block_on(fut)
}

/// Connect to the database read from `DATABASE_URL` (loaded from
/// `.env` if present). Every subcommand uses this -- failures here
/// flow through the four-part onboarding error shape
/// (`DESIGN_ONBOARDING.md` §8) so beginners see plain-English
/// guidance instead of raw driver text. The verbatim backend error
/// is preserved in the `Details:` block for senior engineers.
pub(crate) async fn db() -> Result<rustio_admin::Db, String> {
    let url = std::env::var("DATABASE_URL").map_err(|_| ui::database_url_missing().format())?;
    // Silent-on-success spinner -- `rustio-admin user list` and friends
    // produce their own output and don't need a "✓ Connected" noise
    // line. The spinner exists only so a slow connect doesn't
    // freeze the terminal. PR 1.4 / DESIGN_ONBOARDING.md §9.
    let step = progress::Step::start("Connecting to PostgreSQL");
    match rustio_admin::Db::connect(&url).await {
        Ok(db) => {
            step.clear();
            Ok(db)
        }
        Err(e) => {
            step.clear();
            Err(ui::classify_db_connect_error(&redact_password(&url), &e.to_string()).format())
        }
    }
}

/// Strip the password component from a DATABASE_URL for log output.
/// Returns the input unchanged if it doesn't parse as a URL.
fn redact_password(url: &str) -> String {
    // postgres://user:pw@host/db → postgres://user:***@host/db
    if let Some(at) = url.rfind('@') {
        if let Some(scheme_end) = url.find("://") {
            let prefix = &url[..scheme_end + 3];
            let creds_and_after = &url[scheme_end + 3..];
            let creds_end = at - (scheme_end + 3);
            let creds = &creds_and_after[..creds_end];
            let after = &creds_and_after[creds_end..];
            if let Some((user, _)) = creds.split_once(':') {
                return format!("{prefix}{user}:***{after}");
            }
        }
    }
    url.to_string()
}

#[cfg(test)]
mod tests {
    use super::redact_password;

    #[test]
    fn redact_strips_password() {
        assert_eq!(
            redact_password("postgres://postgres:secret@localhost/db"),
            "postgres://postgres:***@localhost/db"
        );
    }

    #[test]
    fn redact_passthrough_when_no_password() {
        assert_eq!(
            redact_password("postgres://localhost/db"),
            "postgres://localhost/db"
        );
    }
}