arcature-cli 2026.1.0

Developer lifecycle CLI for Arcature applications.
Documentation
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
use std::ffi::OsString;
use std::path::PathBuf;

use clap::{Arg, ArgAction, Command, value_parser};

use super::{CliCommand, DbCommand, FrontendArg, MakeOptions, NewOptions, OutputFormat};

pub(crate) fn parse<I, T>(arguments: I) -> Result<CliCommand, clap::Error>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    let matches = command().try_get_matches_from(arguments)?;
    match matches.subcommand() {
        Some(("new", values)) => {
            let destination = values
                .get_one::<PathBuf>("destination")
                .cloned()
                .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::MissingRequiredArgument))?;
            let frontend = match values.get_one::<String>("frontend").map(String::as_str) {
                Some("vue") => FrontendArg::Vue,
                _ => FrontendArg::React,
            };
            let no_db = values.get_flag("no-db");
            Ok(CliCommand::New(NewOptions {
                destination,
                frontend,
                no_db,
            }))
        }
        Some(("dev", _)) => Ok(CliCommand::Dev),
        Some(("build", _)) => Ok(CliCommand::Build),
        Some(("check", values)) => Ok(CliCommand::Check(output_format(values))),
        Some(("doctor", values)) => Ok(CliCommand::Doctor(output_format(values))),
        Some(("exposure", values)) => Ok(CliCommand::Exposure(output_format(values))),
        Some(("routes", values)) => Ok(CliCommand::Routes(output_format(values))),
        Some(("modules", values)) => Ok(CliCommand::Modules(output_format(values))),
        Some(("services", values)) => Ok(CliCommand::Services(output_format(values))),
        Some(("schedule", values)) => Ok(CliCommand::Schedule(output_format(values))),
        Some(("run", values)) => {
            let name = values
                .get_one::<String>("name")
                .cloned()
                .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::MissingRequiredArgument))?;
            let arguments = values
                .get_many::<String>("arguments")
                .map(|items| items.cloned().collect())
                .unwrap_or_default();
            Ok(CliCommand::Run { name, arguments })
        }
        Some(("make", values)) => {
            let kind = values
                .get_one::<String>("kind")
                .cloned()
                .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::MissingRequiredArgument))?;
            let name = values
                .get_one::<String>("name")
                .cloned()
                .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::MissingRequiredArgument))?;
            let module = values.get_one::<String>("module").cloned();
            Ok(CliCommand::Make(MakeOptions { kind, name, module }))
        }
        Some(("s", values)) => {
            let name = values
                .get_one::<String>("name")
                .cloned()
                .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::MissingRequiredArgument))?;
            let arguments = values
                .get_many::<String>("arguments")
                .map(|items| items.cloned().collect())
                .unwrap_or_default();
            Ok(CliCommand::Script { name, arguments })
        }
        Some(("db", sub)) => parse_db(sub),
        _ => Err(clap::Error::new(clap::error::ErrorKind::MissingSubcommand)),
    }
}

/// Parse the `arc db` nested subcommand tree.
fn parse_db(sub: &clap::ArgMatches) -> Result<CliCommand, clap::Error> {
    match sub.subcommand() {
        Some(("migrate", _)) => Ok(CliCommand::Db(DbCommand::Migrate)),
        Some(("rollback", values)) => {
            let steps = values.get_one::<u32>("steps").copied();
            Ok(CliCommand::Db(DbCommand::Rollback { steps }))
        }
        Some(("status", _)) => Ok(CliCommand::Db(DbCommand::Status)),
        Some(("fresh", values)) => Ok(CliCommand::Db(DbCommand::Fresh {
            force: values.get_flag("force"),
        })),
        Some(("reset", values)) => Ok(CliCommand::Db(DbCommand::Reset {
            force: values.get_flag("force"),
        })),
        Some(("refresh", values)) => Ok(CliCommand::Db(DbCommand::Refresh {
            force: values.get_flag("force"),
        })),
        Some(("prepare", values)) => {
            if values.get_flag("check") {
                Ok(CliCommand::Db(DbCommand::PrepareCheck))
            } else {
                Ok(CliCommand::Db(DbCommand::Prepare))
            }
        }
        _ => Err(clap::Error::new(clap::error::ErrorKind::MissingSubcommand)),
    }
}

fn command() -> Command {
    Command::new("arc")
        .version(env!("CARGO_PKG_VERSION"))
        .about("Develop and operate Arcature applications")
        .subcommand_required(true)
        .arg_required_else_help(true)
        .subcommand(
            Command::new("new")
                .about("Create an Arcature application")
                .arg(
                    Arg::new("destination")
                        .required(true)
                        .help("Directory to create; its final component becomes the project name")
                        .value_parser(value_parser!(PathBuf)),
                )
                .arg(
                    Arg::new("frontend")
                        .long("frontend")
                        .value_name("react|vue")
                        .help("Frontend framework")
                        .default_value("react")
                        .value_parser(["react", "vue"]),
                )
                .arg(
                    Arg::new("no-db")
                        .long("no-db")
                        .help("Generate a project without the database subsystem")
                        .action(ArgAction::SetTrue),
                ),
        )
        .subcommand(Command::new("dev").about("Run the full development environment"))
        .subcommand(Command::new("build").about("Build frontend and backend for production"))
        .subcommand(report_command("check", "Check project health"))
        .subcommand(report_command(
            "doctor",
            "Inspect the local Arcature environment",
        ))
        .subcommand(report_command(
            "exposure",
            "List browser-exposed page contracts and lint secret-bearing fields",
        ))
        .subcommand(report_command(
            "routes",
            "List all application routes (side-effect-free, no boot)",
        ))
        .subcommand(report_command(
            "modules",
            "List application modules and their bindings (side-effect-free, no boot)",
        ))
        .subcommand(report_command(
            "services",
            "List application services and their dependencies (side-effect-free, no boot)",
        ))
        .subcommand(report_command(
            "schedule",
            "List scheduled jobs and their cadence (side-effect-free, no boot)",
        ))
        .subcommand(
            Command::new("run")
                .about("Run a compiled application command by name")
                .arg(
                    Arg::new("name")
                        .required(true)
                        .help("Command name (e.g. users:prune)"),
                )
                .arg(
                    Arg::new("arguments")
                        .help("Arguments forwarded to the command")
                        .num_args(0..)
                        .last(true)
                        .action(ArgAction::Append),
                ),
        )
        .subcommand(
            Command::new("make")
                .about("Generate a source file for the given kind")
                .arg(
                    Arg::new("kind")
                        .required(true)
                        .help("Generator kind: module, controller, request, service, policy, middleware, event, listener, job, command, test, resource"),
                )
                .arg(
                    Arg::new("name")
                        .required(true)
                        .help("Name of the item to generate (e.g. Links, send_welcome)"),
                )
                .arg(
                    Arg::new("module")
                        .long("module")
                        .value_name("module")
                        .help("Module directory to place the file in (e.g. links)"),
                ),
        )
        .subcommand(
            Command::new("s")
                .about("Run an application-owned script")
                .arg(
                    Arg::new("name")
                        .required(true)
                        .help("Script name from the project's s.script file"),
                )
                .arg(
                    Arg::new("arguments")
                        .help("Arguments forwarded as distinct argv values")
                        .num_args(0..)
                        .last(true)
                        .action(ArgAction::Append),
                ),
        )
        .subcommand(db_command())
}

/// Build the `arc db` nested subcommand tree (Phase 4 spec §14).
fn db_command() -> Command {
    Command::new("db")
        .about("Database lifecycle: migrations, status, and schema preparation")
        .subcommand_required(true)
        .arg_required_else_help(true)
        .subcommand(Command::new("migrate").about("Apply pending database migrations"))
        .subcommand(
            Command::new("rollback")
                .about("Roll back applied database migrations")
                .arg(
                    Arg::new("steps")
                        .long("steps")
                        .help("Number of migrations to roll back (default: all)")
                        .value_parser(value_parser!(u32)),
                ),
        )
        .subcommand(Command::new("status").about("Show migration status"))
        .subcommand(destructive_command(
            "fresh",
            "Drop all tables and reapply all migrations",
        ))
        .subcommand(destructive_command("reset", "Roll back all migrations"))
        .subcommand(destructive_command(
            "refresh",
            "Roll back all, then reapply all migrations",
        ))
        .subcommand(
            Command::new("prepare")
                .about("Generate SQLx offline metadata (cargo sqlx prepare --workspace)")
                .arg(
                    Arg::new("check")
                        .long("check")
                        .help("Check offline metadata is current instead of generating")
                        .action(ArgAction::SetTrue),
                ),
        )
}

/// Build a destructive `arc db` subcommand that requires `--force` (Phase 4
/// spec §23: the flag IS the explicit confirmation).
fn destructive_command(name: &'static str, about: &'static str) -> Command {
    Command::new(name).about(about).arg(
        Arg::new("force")
            .long("force")
            .help("Confirm the destructive operation (required)")
            .action(ArgAction::SetTrue),
    )
}

fn report_command(name: &'static str, about: &'static str) -> Command {
    Command::new(name).about(about).arg(
        Arg::new("json")
            .long("json")
            .help("Emit stable machine-readable JSON")
            .action(ArgAction::SetTrue),
    )
}

fn output_format(matches: &clap::ArgMatches) -> OutputFormat {
    if matches.get_flag("json") {
        OutputFormat::Json
    } else {
        OutputFormat::Human
    }
}

#[cfg(test)]
mod tests {
    use clap::error::ErrorKind;

    use super::parse;
    use crate::cli::{CliCommand, DbCommand, FrontendArg, OutputFormat};

    #[test]
    fn parses_default_and_explicit_frontends() {
        let react = parse(["arc", "new", "demo"]).expect("React command should parse");
        let vue =
            parse(["arc", "new", "demo", "--frontend", "vue"]).expect("Vue command should parse");
        assert!(
            matches!(react, CliCommand::New(options) if options.frontend == FrontendArg::React)
        );
        assert!(matches!(vue, CliCommand::New(options) if options.frontend == FrontendArg::Vue));
    }

    #[test]
    fn parses_no_db_flag() {
        let no_db = parse(["arc", "new", "demo", "--no-db"]).expect("no-db should parse");
        assert!(matches!(no_db, CliCommand::New(options) if options.no_db));
        let default = parse(["arc", "new", "demo"]).expect("default should parse");
        assert!(matches!(default, CliCommand::New(options) if !options.no_db));
    }

    #[test]
    fn rejects_unknown_frontend_and_command() {
        let frontend = parse(["arc", "new", "demo", "--frontend", "svelte"])
            .expect_err("unsupported frontend should fail");
        let command = parse(["arc", "deploy"]).expect_err("unknown command should fail");
        assert_eq!(frontend.kind(), ErrorKind::InvalidValue);
        assert_eq!(command.kind(), ErrorKind::InvalidSubcommand);
    }

    #[test]
    fn reports_help_version_and_missing_arguments() {
        assert_eq!(
            parse(["arc", "--help"]).expect_err("help exits").kind(),
            ErrorKind::DisplayHelp
        );
        assert_eq!(
            parse(["arc", "--version"])
                .expect_err("version exits")
                .kind(),
            ErrorKind::DisplayVersion
        );
        assert_eq!(
            parse(["arc", "new"])
                .expect_err("destination is required")
                .kind(),
            ErrorKind::MissingRequiredArgument
        );
    }

    #[test]
    fn preserves_forwarded_script_arguments() {
        let parsed = parse([
            "arc",
            "s",
            "user:add",
            "--",
            "alice@example.com",
            ";touch /tmp/no",
        ])
        .expect("script command should parse");
        assert!(
            matches!(parsed, CliCommand::Script { name, arguments } if name == "user:add" && arguments == ["alice@example.com", ";touch /tmp/no"])
        );
    }

    #[test]
    fn parses_machine_readable_reports() {
        assert_eq!(
            parse(["arc", "doctor", "--json"]).expect("doctor should parse"),
            CliCommand::Doctor(OutputFormat::Json)
        );
        assert_eq!(
            parse(["arc", "check"]).expect("check should parse"),
            CliCommand::Check(OutputFormat::Human)
        );
    }

    #[test]
    fn parses_exposure_command() {
        let human = parse(["arc", "exposure"]).expect("exposure should parse");
        let json = parse(["arc", "exposure", "--json"]).expect("exposure --json should parse");
        assert_eq!(human, CliCommand::Exposure(OutputFormat::Human));
        assert_eq!(json, CliCommand::Exposure(OutputFormat::Json));
    }

    #[test]
    fn parses_inspection_reports() {
        assert_eq!(
            parse(["arc", "routes"]).expect("routes should parse"),
            CliCommand::Routes(OutputFormat::Human)
        );
        assert_eq!(
            parse(["arc", "routes", "--json"]).expect("routes --json should parse"),
            CliCommand::Routes(OutputFormat::Json)
        );
        assert_eq!(
            parse(["arc", "modules"]).expect("modules should parse"),
            CliCommand::Modules(OutputFormat::Human)
        );
        assert_eq!(
            parse(["arc", "services", "--json"]).expect("services --json should parse"),
            CliCommand::Services(OutputFormat::Json)
        );
        assert_eq!(
            parse(["arc", "schedule"]).expect("schedule should parse"),
            CliCommand::Schedule(OutputFormat::Human)
        );
    }

    #[test]
    fn parses_run_command_with_arguments() {
        let parsed = parse(["arc", "run", "users:prune"]).expect("run should parse");
        assert!(
            matches!(parsed, CliCommand::Run { name, arguments } if name == "users:prune" && arguments.is_empty())
        );
        let parsed = parse(["arc", "run", "db:cleanup", "--", "--dry-run", "30d"])
            .expect("run with args should parse");
        assert!(
            matches!(parsed, CliCommand::Run { name, arguments } if name == "db:cleanup" && arguments == ["--dry-run", "30d"])
        );
    }

    #[test]
    fn parses_make_command_with_and_without_module() {
        let parsed = parse(["arc", "make", "controller", "Sessions"]).expect("make should parse");
        assert!(
            matches!(parsed, CliCommand::Make(opts) if opts.kind == "controller" && opts.name == "Sessions" && opts.module.is_none())
        );
        let parsed = parse(["arc", "make", "request", "Login", "--module", "accounts"])
            .expect("make with module should parse");
        assert!(
            matches!(parsed, CliCommand::Make(opts) if opts.kind == "request" && opts.name == "Login" && opts.module.as_deref() == Some("accounts"))
        );
    }

    #[test]
    fn run_requires_name() {
        let err = parse(["arc", "run"]).expect_err("run needs a name");
        assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
    }

    #[test]
    fn make_requires_kind_and_name() {
        let err = parse(["arc", "make"]).expect_err("make needs kind + name");
        assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
    }

    #[test]
    fn parses_db_subcommands() {
        assert_eq!(
            parse(["arc", "db", "migrate"]).expect("migrate should parse"),
            CliCommand::Db(DbCommand::Migrate),
        );
        assert_eq!(
            parse(["arc", "db", "status"]).expect("status should parse"),
            CliCommand::Db(DbCommand::Status),
        );
        assert_eq!(
            parse(["arc", "db", "rollback"]).expect("rollback default should parse"),
            CliCommand::Db(DbCommand::Rollback { steps: None }),
        );
        assert_eq!(
            parse(["arc", "db", "rollback", "--steps", "3"]).expect("rollback steps should parse"),
            CliCommand::Db(DbCommand::Rollback { steps: Some(3) }),
        );
    }

    #[test]
    fn parses_db_destructive_commands_require_force() {
        // Without --force, the command parses but force is false.
        let fresh = parse(["arc", "db", "fresh"]).expect("fresh should parse");
        assert!(matches!(
            fresh,
            CliCommand::Db(DbCommand::Fresh { force: false })
        ));
        let fresh_forced =
            parse(["arc", "db", "fresh", "--force"]).expect("fresh --force should parse");
        assert!(matches!(
            fresh_forced,
            CliCommand::Db(DbCommand::Fresh { force: true })
        ));
    }

    #[test]
    fn parses_db_prepare_and_prepare_check() {
        let prepare = parse(["arc", "db", "prepare"]).expect("prepare should parse");
        assert!(matches!(prepare, CliCommand::Db(DbCommand::Prepare)));
        let check =
            parse(["arc", "db", "prepare", "--check"]).expect("prepare --check should parse");
        assert!(matches!(check, CliCommand::Db(DbCommand::PrepareCheck)));
    }
}