Skip to main content

reddb_server/cli/
commands.rs

1/// RedDB command definitions.
2///
3/// Defines the command tree, Flag and Route types used by help and completion
4/// generators, and the schema for each built-in command.
5use super::types::FlagSchema;
6
7// ============================================================================
8// Help-layer types (used by help.rs and complete.rs)
9// ============================================================================
10
11/// Lightweight flag descriptor used by the help generator.
12#[derive(Debug, Clone)]
13pub struct Flag {
14    pub short: Option<char>,
15    pub long: String,
16    pub description: String,
17    pub default: Option<String>,
18    pub arg: Option<String>,
19}
20
21impl Flag {
22    pub fn new(long: &str, desc: &str) -> Self {
23        Self {
24            short: None,
25            long: long.to_string(),
26            description: desc.to_string(),
27            default: None,
28            arg: None,
29        }
30    }
31
32    pub fn with_short(mut self, short: char) -> Self {
33        self.short = Some(short);
34        self
35    }
36
37    pub fn with_default(mut self, default: &str) -> Self {
38        self.default = Some(default.to_string());
39        self
40    }
41
42    pub fn with_arg(mut self, arg: &str) -> Self {
43        self.arg = Some(arg.to_string());
44        self
45    }
46}
47
48/// A single routable verb within a resource.
49#[derive(Debug, Clone)]
50pub struct Route {
51    pub verb: &'static str,
52    pub summary: &'static str,
53    pub usage: &'static str,
54}
55
56// ============================================================================
57// RedDB command definitions
58// ============================================================================
59
60/// Command descriptor for a top-level RedDB command.
61pub struct CommandDef {
62    pub name: &'static str,
63    pub summary: &'static str,
64    pub usage: &'static str,
65    pub flags: Vec<FlagSchema>,
66}
67
68/// Return all RedDB commands.
69pub fn all_commands() -> Vec<CommandDef> {
70    vec![
71    CommandDef {
72      name: "server",
73      summary: "Start the database server (router/HTTP/gRPC/wire)",
74      usage: "red server [--grpc] [--http] [--grpc-bind 127.0.0.1:5555] [--http-bind 127.0.0.1:5055] [--wire-bind 127.0.0.1:5050] [--path ./data/reddb.rdb]",
75      flags: server_flags(),
76    },
77    CommandDef {
78      name: "service",
79      summary: "Install or inspect a systemd service",
80      usage: "red service <install|print-unit> [--binary /usr/local/bin/red] [--grpc-bind 0.0.0.0:5555] [--http-bind 0.0.0.0:5055] [--path /var/lib/reddb/data.rdb]",
81      flags: service_flags(),
82    },
83    CommandDef {
84      name: "query",
85      summary: "Execute a query against the database",
86      usage: "red query \"SELECT * FROM users WHERE age > $1\" -p 21",
87      flags: query_flags(),
88    },
89    CommandDef {
90      name: "insert",
91      summary: "Insert an entity into a collection",
92      usage: "red insert users '{\"name\": \"Alice\", \"age\": 30}'",
93      flags: insert_flags(),
94    },
95    CommandDef {
96      name: "get",
97      summary: "Get an entity by ID from a collection",
98      usage: "red get users abc123",
99      flags: get_flags(),
100    },
101    CommandDef {
102      name: "delete",
103      summary: "Delete an entity by ID from a collection",
104      usage: "red delete users abc123",
105      flags: delete_flags(),
106    },
107    CommandDef {
108      name: "health",
109      summary: "Run a health check against the server",
110      usage: "red health [--bind 127.0.0.1:5050] [--grpc|--http]",
111      flags: health_flags(),
112    },
113    CommandDef {
114      name: "tick",
115      summary: "Run maintenance/reclaim tick operations",
116      usage: "red tick [--bind 127.0.0.1:5055] [--operations maintenance,retention,checkpoint] [--dry-run]",
117      flags: tick_flags(),
118    },
119    CommandDef {
120      name: "migrate-from-redis",
121      summary: "Validate Redis to Blob Cache migration readiness; dual-write uses the documented application-owned helper pattern",
122      usage: "red migrate-from-redis --dry-run --redis-url redis://127.0.0.1:6379/0 [--path ./data/reddb.rdb]",
123      flags: migrate_from_redis_flags(),
124    },
125    CommandDef {
126      name: "replica",
127      summary: "Start as a read replica connected to a primary",
128      usage: "red replica --primary-addr http://primary:5555 [--grpc] [--http] [--grpc-bind 127.0.0.1:5555] [--http-bind 127.0.0.1:5055] [--path ./data/reddb.rdb]",
129      flags: replica_flags(),
130    },
131    CommandDef {
132      name: "status",
133      summary: "Show replication status",
134      usage: "red status [--bind 0.0.0.0:6380]",
135      flags: status_flags(),
136    },
137    CommandDef {
138      name: "inspect",
139      summary: "Inspect on-disk database state (catalog snapshot)",
140      usage: "red inspect catalog --path <FILE> [--at <SEQ>] [--json]",
141      flags: inspect_flags(),
142    },
143    CommandDef {
144      name: "mcp",
145      summary: "Start MCP server for AI agent integration",
146      usage: "red mcp [--path /data]",
147      flags: mcp_flags(),
148    },
149    CommandDef {
150      name: "auth",
151      summary: "Manage authentication (users, tokens, roles)",
152      usage: "red auth <subcommand>",
153      flags: auth_flags(),
154    },
155    CommandDef {
156      name: "connect",
157      summary: "Connect to a remote RedDB server (interactive REPL)",
158      usage: "red connect [--token <token>] [--query <sql>] <addr>",
159      flags: connect_flags(),
160    },
161    CommandDef {
162      name: "dump",
163      summary: "Export one or all collections as JSONL for backup/migration",
164      usage: "red dump [--path file] [--collection NAME] [-o FILE]",
165      flags: dump_flags(),
166    },
167    CommandDef {
168      name: "restore",
169      summary: "Import a previously dumped JSONL file into the database",
170      usage: "red restore [--path file] -i FILE [--collection NAME]",
171      flags: restore_flags(),
172    },
173    CommandDef {
174      name: "pitr-list",
175      summary: "List available point-in-time restore points from a snapshot archive",
176      usage: "red pitr-list --snapshot-prefix DIR --wal-prefix DIR",
177      flags: pitr_list_flags(),
178    },
179    CommandDef {
180      name: "pitr-restore",
181      summary: "Restore a database to a specific point in time from snapshots + WAL archive",
182      usage: "red pitr-restore --target-time UNIX_MS --dest PATH --snapshot-prefix DIR --wal-prefix DIR",
183      flags: pitr_restore_flags(),
184    },
185    CommandDef {
186      name: "doctor",
187      summary: "Health-check a running server against operator thresholds (PLAN.md Phase 5.5)",
188      usage: "red doctor [--bind 127.0.0.1:5055] [--token <admin>] [--json] [--backup-age-warn-secs 600] [--backup-age-crit-secs 3600] [--wal-lag-warn 1000] [--wal-lag-crit 10000]",
189      flags: doctor_flags(),
190    },
191    CommandDef {
192      name: "bootstrap",
193      summary: "One-shot first-admin bootstrap for headless containers / K8s Jobs",
194      usage: "red bootstrap --path PATH --vault [--username USER] [--password-stdin] [--print-certificate] [--json]",
195      flags: bootstrap_flags(),
196    },
197    CommandDef {
198      name: "version",
199      summary: "Show RedDB version information",
200      usage: "red version",
201      flags: vec![],
202    },
203    CommandDef {
204      name: "vcs",
205      summary: "Version-control operations (Git for Data)",
206      usage: "red vcs <commit|branch|branches|tag|tags|checkout|merge|log|status|lca|resolve> [args] [flags]",
207      flags: vcs_flags(),
208    },
209    CommandDef {
210      name: "ui",
211      summary: "Open a graphical UI against a local .rdb or a remote red:///reds:// instance over a RedWire-over-WS bridge",
212      usage: "red ui file://./data.rdb | red ui red://host:port [--token TOKEN] [--ui-dir DIR] [--port N] [--tls-ca PEM] [--no-browser]",
213      flags: ui_flags(),
214    },
215  ]
216}
217
218/// Return the help text for the main `red` command.
219pub fn main_help_text() -> String {
220    let mut out = String::with_capacity(1024);
221
222    out.push_str("reddb -- unified multi-model database engine\n");
223    out.push('\n');
224    out.push_str("Usage: red <command> [args] [flags]\n");
225    out.push('\n');
226
227    out.push_str("Commands:\n");
228    for cmd in all_commands() {
229        out.push_str(&format!("  {:<14} {}\n", cmd.name, cmd.summary));
230    }
231    out.push_str(&format!("  {:<14} {}\n", "help", "Show help for a command"));
232    out.push('\n');
233
234    out.push_str("Global flags:\n");
235    out.push_str(&format!("  {:<24} {}\n", "-h, --help", "Show help"));
236    out.push_str(&format!("  {:<24} {}\n", "-j, --json", "Force JSON output"));
237    out.push_str(&format!(
238        "  {:<24} {}\n",
239        "-o, --output FORMAT", "Output format [text|json|yaml]"
240    ));
241    out.push_str(&format!("  {:<24} {}\n", "-v, --verbose", "Verbose output"));
242    out.push_str(&format!(
243        "  {:<24} {}\n",
244        "    --no-color", "Disable colors"
245    ));
246    out.push_str(&format!("  {:<24} {}\n", "    --version", "Show version"));
247    out.push('\n');
248
249    out.push_str("Examples:\n");
250    out.push_str("  red server --path ./data/reddb.rdb\n");
251    out.push_str("  red server --grpc-bind 127.0.0.1:5555 --http-bind 127.0.0.1:5055 --path ./data/reddb.rdb\n");
252    out.push_str("  red server --wire-bind 127.0.0.1:5050 --path ./data/reddb.rdb\n");
253    out.push_str("  sudo red service install --binary /usr/local/bin/red --grpc-bind 0.0.0.0:5555 --http-bind 0.0.0.0:5055 --path /var/lib/reddb/data.rdb\n");
254    out.push_str("  red replica --primary-addr http://primary:5555 --path ./data/replica.rdb\n");
255    out.push_str("  red query \"SELECT * FROM users\"\n");
256    out.push_str("  red insert users '{\"name\": \"Alice\"}'\n");
257    out.push_str("  red get users abc123\n");
258    out.push_str("  red health\n");
259    out.push_str(
260        "  red tick --bind 127.0.0.1:5055 --operations maintenance,retention,checkpoint\n",
261    );
262    out.push_str("  red auth create-user alice --password secret --role admin\n");
263    out.push_str("  red auth create-api-key alice --name \"ci-token\" --role write\n");
264    out.push_str("  red auth list-users\n");
265    out.push_str("  red auth login alice --password secret\n");
266    out.push_str("  red connect 127.0.0.1:5050\n");
267    out.push_str("  red connect --query \"SELECT * FROM users\" 127.0.0.1:5050\n");
268    out.push('\n');
269
270    out.push_str("Run 'red <command> --help' for more information on a command.\n");
271    out
272}
273
274/// Return help text for a specific command.
275pub fn command_help_text(name: &str) -> Option<String> {
276    let cmds = all_commands();
277    let cmd = cmds.iter().find(|c| c.name == name)?;
278
279    let mut out = String::with_capacity(512);
280
281    out.push_str(&format!("red {} -- {}\n", cmd.name, cmd.summary));
282    out.push('\n');
283    out.push_str(&format!("Usage: {}\n", cmd.usage));
284    out.push('\n');
285
286    if !cmd.flags.is_empty() {
287        out.push_str("Flags:\n");
288        for flag in &cmd.flags {
289            let short_part = match flag.short {
290                Some(ch) => format!("-{}, ", ch),
291                None => "    ".to_string(),
292            };
293            let value_part = if flag.expects_value {
294                format!(" <{}>", flag.long.to_uppercase())
295            } else {
296                String::new()
297            };
298            let label = format!("{}--{}{}", short_part, flag.long, value_part);
299            let padding = if label.len() < 24 {
300                24 - label.len()
301            } else {
302                2
303            };
304            let default_text = match &flag.default {
305                Some(d) => format!(" (default: {})", d),
306                None => String::new(),
307            };
308            out.push_str(&format!(
309                "  {}{}{}{}\n",
310                label,
311                " ".repeat(padding),
312                flag.description,
313                default_text,
314            ));
315        }
316        out.push('\n');
317    }
318
319    Some(out)
320}
321
322// ============================================================================
323// Per-command flag schemas
324// ============================================================================
325
326fn server_flags() -> Vec<FlagSchema> {
327    vec![
328        FlagSchema::new("path")
329            .with_short('d')
330            .with_description("Persistent database file path (omit for in-memory)")
331            .with_default("./data/reddb.rdb"),
332        FlagSchema::new("bind").with_short('b').with_description(
333            "Bind address (host:port) for the routed front-door or legacy single-transport mode",
334        ),
335        FlagSchema::boolean("grpc").with_description("Enable the gRPC API"),
336        FlagSchema::boolean("http").with_description("Serve the HTTP API"),
337        FlagSchema::new("grpc-bind").with_description("Explicit gRPC bind address (host:port)"),
338        FlagSchema::new("http-bind").with_description("Explicit HTTP bind address (host:port)"),
339        FlagSchema::new("wire-bind")
340            .with_description("Explicit wire bind address (host:port or unix:///path/to/socket)"),
341        FlagSchema::new("wire-tls-bind")
342            .with_description("Explicit wire TLS bind address (host:port)"),
343        FlagSchema::new("wire-tls-cert")
344            .with_description("Path to TLS certificate PEM for wire TLS"),
345        FlagSchema::new("wire-tls-key")
346            .with_description("Path to TLS private key PEM for wire TLS"),
347        FlagSchema::new("pg-bind").with_description(
348            "PostgreSQL wire protocol bind address (enables psql / JDBC / DBeaver clients)",
349        ),
350        FlagSchema::new("role")
351            .with_short('r')
352            .with_description("Replication role")
353            .with_choices(&["standalone", "primary", "replica"])
354            .with_default("standalone"),
355        FlagSchema::new("primary-addr").with_description("Primary gRPC address for replica mode"),
356        FlagSchema::boolean("read-only").with_description("Open the database in read-only mode"),
357        FlagSchema::boolean("no-create-if-missing")
358            .with_description("Fail instead of creating the database file"),
359        FlagSchema::boolean("auth").with_description("Enable authentication for this boot"),
360        FlagSchema::boolean("require-auth")
361            .with_description("Reject anonymous requests; implies --auth"),
362        FlagSchema::new("vault")
363            .with_description("Enable encrypted auth vault (reserved pages in main .rdb file)")
364            .with_default("false"),
365        FlagSchema::boolean("no-auth").with_description(
366            "Hard-disable auth: anonymous access, ignores REDDB_USERNAME/PASSWORD/vault, \
367             prints a startup warning. Local-dev shortcut — NEVER use in production.",
368        ),
369        FlagSchema::boolean("dev")
370            .with_description("Alias for --no-auth (local development convenience)."),
371        FlagSchema::new("bootstrap-preset")
372            .with_description("First-boot preset")
373            .with_choices(&["simple", "production", "regulated", "cloud"]),
374        FlagSchema::new("bootstrap-manifest")
375            .with_description("Path to first-boot bootstrap manifest JSON"),
376        FlagSchema::new("bootstrap-admin")
377            .with_description("First admin username for production/cloud bootstrap"),
378        FlagSchema::new("bootstrap-admin-password").with_description(
379            "First admin password (DEV ONLY; prefer --bootstrap-admin-password-file)",
380        ),
381        FlagSchema::new("bootstrap-admin-password-file")
382            .with_description("File containing first admin password"),
383        FlagSchema::new("cloud-head-admin")
384            .with_description("Cloud preset head/platform admin username"),
385        FlagSchema::new("cloud-head-admin-password").with_description(
386            "Cloud preset head/platform admin password (DEV ONLY; prefer file flag)",
387        ),
388        FlagSchema::new("cloud-head-admin-password-file")
389            .with_description("File containing cloud head admin password"),
390        FlagSchema::new("customer-admin").with_description("Cloud preset customer admin username"),
391        FlagSchema::new("customer-admin-password")
392            .with_description("Cloud preset customer admin password (DEV ONLY; prefer file flag)"),
393        FlagSchema::new("customer-admin-password-file")
394            .with_description("File containing cloud customer admin password"),
395        FlagSchema::new("log-dir").with_description(
396            "Directory for rotating log files (defaults to the parent of --path / ./logs)",
397        ),
398        FlagSchema::new("log-level")
399            .with_description(
400                "Log level filter — trace / debug / info / warn / error, or a RUST_LOG expression",
401            )
402            .with_default("info"),
403        FlagSchema::new("log-format")
404            .with_description("Log output format")
405            .with_choices(&["pretty", "json"])
406            .with_default("pretty"),
407        FlagSchema::new("log-keep-days")
408            .with_description("Number of rotated log files to keep")
409            .with_default("14"),
410        FlagSchema::boolean("no-log-file")
411            .with_description("Disable rotating file logs (stderr only)"),
412        FlagSchema::new("http-max-handlers").with_description(
413            "Max concurrent HTTP handler threads (env: REDDB_HTTP_MAX_HANDLERS; \
414             red_config: red.http.max_handlers; default: (2 x num_cpus).clamp(8, 256))",
415        ),
416        FlagSchema::new("http-handler-timeout-ms")
417            .with_description(
418                "Per-handler total-time budget in ms (env: REDDB_HTTP_HANDLER_TIMEOUT_MS; \
419             red_config: red.http.handler_timeout_ms)",
420            )
421            .with_default("30000"),
422        FlagSchema::new("http-retry-after-secs")
423            .with_description(
424                "Retry-After seconds on limiter 503 (env: REDDB_HTTP_RETRY_AFTER_SECS; \
425             red_config: red.http.retry_after_secs; clamped to [1, 30])",
426            )
427            .with_default("5"),
428        FlagSchema::new("http-max-inflight-per-principal").with_description(
429            "Max concurrent in-flight HTTP requests per principal; over-cap requests \
430             get a structured 429 (env: REDDB_HTTP_MAX_INFLIGHT_PER_PRINCIPAL; \
431             red_config: red.http.max_inflight_per_principal; 0 disables; default: 64)",
432        ),
433    ]
434}
435
436fn replica_flags() -> Vec<FlagSchema> {
437    vec![
438        FlagSchema::new("primary-addr")
439            .with_short('p')
440            .with_description("Primary gRPC address (e.g. http://primary:50051)"),
441        FlagSchema::new("path")
442            .with_short('d')
443            .with_description("Local replica database file path")
444            .with_default("./data/reddb.rdb"),
445        FlagSchema::new("bind").with_short('b').with_description(
446            "Bind address (host:port) for the routed front-door or legacy single-transport mode",
447        ),
448        FlagSchema::boolean("grpc").with_description("Enable the gRPC API"),
449        FlagSchema::boolean("http").with_description("Serve the HTTP API"),
450        FlagSchema::new("grpc-bind").with_description("Explicit gRPC bind address (host:port)"),
451        FlagSchema::new("http-bind").with_description("Explicit HTTP bind address (host:port)"),
452        FlagSchema::new("wire-bind")
453            .with_description("Explicit wire bind address (host:port or unix:///path/to/socket)"),
454        FlagSchema::boolean("auth").with_description("Enable authentication for this boot"),
455        FlagSchema::boolean("require-auth")
456            .with_description("Reject anonymous requests; implies --auth"),
457        FlagSchema::new("vault")
458            .with_description("Enable encrypted auth vault (reserved pages in main .rdb file)")
459            .with_default("false"),
460        FlagSchema::boolean("no-auth")
461            .with_description("Hard-disable auth: anonymous access, ignores vault"),
462    ]
463}
464
465fn ui_flags() -> Vec<FlagSchema> {
466    vec![
467        FlagSchema::boolean("server")
468            .with_description("Force the browser-served bridge path (skip the desktop deep link)"),
469        FlagSchema::boolean("desktop").with_description(
470            "Force the desktop app via the redui:// deep link (no browser fallback)",
471        ),
472        FlagSchema::new("ui-dir").with_description(
473            "Directory to serve the UI bundle from (defaults to the built-in fixture)",
474        ),
475        FlagSchema::new("port")
476            .with_description("Loopback port for the bridge (0 / omit picks an ephemeral port)"),
477        FlagSchema::new("tls-ca").with_description(
478            "PEM CA bundle to trust for a reds:// target (on top of system roots)",
479        ),
480        FlagSchema::new("token").with_short('t').with_description(
481            "Bearer token (session/API key). Held by red and injected into the \
482             RedWire handshake — the UI never sees it (env: RED_UI_TOKEN)",
483        ),
484        FlagSchema::boolean("no-browser").with_description(
485            "Do not open the default browser (also honoured via RED_UI_NO_BROWSER)",
486        ),
487    ]
488}
489
490fn vcs_flags() -> Vec<FlagSchema> {
491    vec![
492        FlagSchema::new("path")
493            .with_short('d')
494            .with_description("Persistent database file path (omit for in-memory)"),
495        FlagSchema::new("connection")
496            .with_short('c')
497            .with_description("Connection id for workset scoping")
498            .with_default("1"),
499        FlagSchema::new("branch").with_description("Branch name (for log/checkout/merge)"),
500        FlagSchema::new("from").with_description("Source ref or commit (branch create / merge)"),
501        FlagSchema::new("to").with_description("Upper bound for log range"),
502        FlagSchema::new("author")
503            .with_description("Commit author name")
504            .with_default("reddb"),
505        FlagSchema::new("email")
506            .with_description("Commit author email")
507            .with_default("reddb@localhost"),
508        FlagSchema::new("message")
509            .with_short('m')
510            .with_description("Commit message"),
511        FlagSchema::new("limit")
512            .with_description("Max log entries")
513            .with_default("20"),
514        FlagSchema::boolean("ff-only").with_description("Merge only if fast-forward"),
515        FlagSchema::boolean("no-ff").with_description("Always create a merge commit"),
516    ]
517}
518
519fn service_flags() -> Vec<FlagSchema> {
520    vec![
521        FlagSchema::new("binary")
522            .with_description("Path to the red binary")
523            .with_default("/usr/local/bin/red"),
524        FlagSchema::new("service-name")
525            .with_description("systemd unit name")
526            .with_default("reddb"),
527        FlagSchema::new("user")
528            .with_description("Service user")
529            .with_default("reddb"),
530        FlagSchema::new("group")
531            .with_description("Service group")
532            .with_default("reddb"),
533        FlagSchema::new("path")
534            .with_short('d')
535            .with_description("Persistent database file path")
536            .with_default(reddb_file::DEFAULT_SERVICE_DATABASE_PATH),
537        FlagSchema::new("bind").with_short('b').with_description(
538            "Bind address (host:port) for the routed front-door or legacy single-transport mode",
539        ),
540        FlagSchema::boolean("grpc").with_description("Enable the gRPC API in the service"),
541        FlagSchema::boolean("http").with_description("Install an HTTP service"),
542        FlagSchema::new("grpc-bind").with_description("Explicit gRPC bind address (host:port)"),
543        FlagSchema::new("http-bind").with_description("Explicit HTTP bind address (host:port)"),
544    ]
545}
546
547fn query_flags() -> Vec<FlagSchema> {
548    vec![
549        FlagSchema::new("bind")
550            .with_short('b')
551            .with_description("Server address")
552            .with_default("0.0.0.0:6380"),
553        FlagSchema::new("path").with_description("Open a local .rdb file in embedded mode"),
554        FlagSchema::new("param")
555            .with_short('p')
556            .with_description("Positional parameter for $1, $2, ... (repeatable)"),
557        FlagSchema::new("param-type").with_description("Type override for the preceding --param"),
558    ]
559}
560
561fn insert_flags() -> Vec<FlagSchema> {
562    vec![FlagSchema::new("bind")
563        .with_short('b')
564        .with_description("Server address")
565        .with_default("0.0.0.0:6380")]
566}
567
568fn get_flags() -> Vec<FlagSchema> {
569    vec![FlagSchema::new("bind")
570        .with_short('b')
571        .with_description("Server address")
572        .with_default("0.0.0.0:6380")]
573}
574
575fn delete_flags() -> Vec<FlagSchema> {
576    vec![FlagSchema::new("bind")
577        .with_short('b')
578        .with_description("Server address")
579        .with_default("0.0.0.0:6380")]
580}
581
582fn health_flags() -> Vec<FlagSchema> {
583    vec![
584        FlagSchema::new("bind")
585            .with_short('b')
586            .with_description("Server address; defaults by transport"),
587        FlagSchema::boolean("grpc").with_description("Probe a gRPC listener (default transport)"),
588        FlagSchema::boolean("http").with_description("Probe an HTTP listener"),
589    ]
590}
591
592fn bootstrap_flags() -> Vec<FlagSchema> {
593    vec![
594        FlagSchema::new("path")
595            .with_short('d')
596            .with_description("Persistent database file path"),
597        FlagSchema::boolean("vault")
598            .with_description("Required: seal credentials in the encrypted vault"),
599        FlagSchema::new("username")
600            .with_short('u')
601            .with_description("Admin username (defaults to REDDB_USERNAME)"),
602        FlagSchema::new("password")
603            .with_description("Admin password (DEV ONLY; prefer --password-stdin)"),
604        FlagSchema::boolean("password-stdin")
605            .with_description("Read the admin password from stdin (one line)"),
606        FlagSchema::boolean("print-certificate")
607            .with_description("Print only the certificate to stdout"),
608    ]
609}
610
611fn doctor_flags() -> Vec<FlagSchema> {
612    vec![
613        FlagSchema::new("bind")
614            .with_description("HTTP address of the server to probe")
615            .with_default("127.0.0.1:5055"),
616        FlagSchema::new("token")
617            .with_description("Admin bearer token; defaults to RED_ADMIN_TOKEN env"),
618        FlagSchema::boolean("json")
619            .with_description("Emit a single JSON object instead of human text"),
620        FlagSchema::new("backup-age-warn-secs")
621            .with_description("Warn when last successful backup is older than N seconds")
622            .with_default("600"),
623        FlagSchema::new("backup-age-crit-secs")
624            .with_description("Critical when last successful backup is older than N seconds")
625            .with_default("3600"),
626        FlagSchema::new("wal-lag-warn")
627            .with_description("Warn when WAL archive lag exceeds N records")
628            .with_default("1000"),
629        FlagSchema::new("wal-lag-crit")
630            .with_description("Critical when WAL archive lag exceeds N records")
631            .with_default("10000"),
632    ]
633}
634
635fn dump_flags() -> Vec<FlagSchema> {
636    vec![
637        FlagSchema::new("path")
638            .with_description("Local database file to dump from")
639            .with_default("./data/reddb.rdb"),
640        FlagSchema::new("collection")
641            .with_short('c')
642            .with_description("Single collection to dump (omit for all)"),
643        FlagSchema::new("output")
644            .with_short('o')
645            .with_description("Destination file (defaults to stdout)"),
646    ]
647}
648
649fn restore_flags() -> Vec<FlagSchema> {
650    vec![
651        FlagSchema::new("path")
652            .with_description("Local database file to restore into")
653            .with_default("./data/reddb.rdb"),
654        FlagSchema::new("input")
655            .with_short('i')
656            .with_description("Dump file to read (required)"),
657        FlagSchema::new("collection")
658            .with_short('c')
659            .with_description("Override target collection name"),
660    ]
661}
662
663fn pitr_list_flags() -> Vec<FlagSchema> {
664    vec![
665        FlagSchema::new("snapshot-prefix")
666            .with_description("Directory (or remote prefix) holding .snapshot files"),
667        FlagSchema::new("wal-prefix")
668            .with_description("Directory (or remote prefix) holding archived WAL segments"),
669    ]
670}
671
672fn pitr_restore_flags() -> Vec<FlagSchema> {
673    vec![
674        FlagSchema::new("target-time")
675            .with_description("Recovery target — UNIX ms (0 = latest available)"),
676        FlagSchema::new("dest")
677            .with_description("Destination database file path for the restored DB"),
678        FlagSchema::new("snapshot-prefix")
679            .with_description("Directory (or remote prefix) holding .snapshot files"),
680        FlagSchema::new("wal-prefix")
681            .with_description("Directory (or remote prefix) holding archived WAL segments"),
682    ]
683}
684
685fn tick_flags() -> Vec<FlagSchema> {
686    vec![
687        FlagSchema::new("bind")
688            .with_short('b')
689            .with_description("Server HTTP bind address")
690            .with_default("127.0.0.1:5055"),
691        FlagSchema::new("operations")
692            .with_description("Comma-separated operations: maintenance,retention,checkpoint"),
693        FlagSchema::boolean("dry-run")
694            .with_description("Validate operations without applying changes"),
695    ]
696}
697
698fn migrate_from_redis_flags() -> Vec<FlagSchema> {
699    vec![
700        FlagSchema::boolean("dry-run")
701            .with_description("Validate Redis and RedDB connectivity without cache writes"),
702        FlagSchema::new("redis-url")
703            .with_description("Redis URL to validate, for example redis://127.0.0.1:6379/0"),
704        FlagSchema::new("path")
705            .with_short('d')
706            .with_description("Local RedDB .rdb file to open for connectivity validation"),
707        FlagSchema::new("phase")
708            .with_description("Migration phase: dry-run | dual-write")
709            .with_default("dry-run"),
710        FlagSchema::new("namespace")
711            .with_description("Blob Cache namespace recorded in dry-run output")
712            .with_default("redis-migration"),
713    ]
714}
715
716fn status_flags() -> Vec<FlagSchema> {
717    vec![FlagSchema::new("bind")
718        .with_short('b')
719        .with_description("Server address")
720        .with_default("0.0.0.0:6380")]
721}
722
723fn inspect_flags() -> Vec<FlagSchema> {
724    vec![
725        FlagSchema::new("path")
726            .with_short('d')
727            .with_description("Path to the on-disk database file"),
728        FlagSchema::new("at")
729            .with_description("Catalog at snapshot sequence (requires metadata journal)"),
730    ]
731}
732
733fn mcp_flags() -> Vec<FlagSchema> {
734    vec![FlagSchema::new("path")
735        .with_short('d')
736        .with_description("Data directory path (omit for in-memory)")
737        .with_default("")]
738}
739
740fn connect_flags() -> Vec<FlagSchema> {
741    vec![
742        FlagSchema::new("token")
743            .with_short('t')
744            .with_description("Auth token (session or API key)"),
745        FlagSchema::new("query")
746            .with_short('q')
747            .with_description("Execute a single query and exit"),
748        FlagSchema::new("user")
749            .with_short('u')
750            .with_description("Username for login"),
751        FlagSchema::new("password")
752            .with_short('p')
753            .with_description("Password for login"),
754    ]
755}
756
757fn auth_flags() -> Vec<FlagSchema> {
758    vec![
759        FlagSchema::new("bind")
760            .with_short('b')
761            .with_description("Server address")
762            .with_default("0.0.0.0:6380"),
763        FlagSchema::new("password")
764            .with_short('p')
765            .with_description("User password"),
766        FlagSchema::new("role")
767            .with_short('r')
768            .with_description("User role")
769            .with_choices(&["read", "write", "admin"]),
770        FlagSchema::new("name")
771            .with_short('n')
772            .with_description("API key name"),
773        FlagSchema::new("user")
774            .with_short('u')
775            .with_description("Target username"),
776    ]
777}
778
779// ============================================================================
780// Completion data helpers
781// ============================================================================
782
783/// Return domain data for completion scripts.
784pub fn completion_domains() -> Vec<(String, Vec<String>)> {
785    vec![
786        ("server".to_string(), vec![]),
787        ("service".to_string(), vec![]),
788        ("replica".to_string(), vec![]),
789        ("tick".to_string(), vec![]),
790        ("query".to_string(), vec!["q".to_string()]),
791        ("insert".to_string(), vec!["i".to_string()]),
792        ("get".to_string(), vec![]),
793        ("delete".to_string(), vec!["del".to_string()]),
794        ("health".to_string(), vec![]),
795        ("status".to_string(), vec![]),
796        ("inspect".to_string(), vec![]),
797        ("migrate-from-redis".to_string(), vec![]),
798        ("mcp".to_string(), vec![]),
799        ("auth".to_string(), vec![]),
800        ("connect".to_string(), vec![]),
801        ("version".to_string(), vec![]),
802    ]
803}
804
805/// Return global flag data for completion scripts.
806pub fn completion_global_flags() -> Vec<(&'static str, Option<char>)> {
807    vec![
808        ("help", Some('h')),
809        ("json", Some('j')),
810        ("output", Some('o')),
811        ("verbose", Some('v')),
812        ("no-color", None),
813        ("version", None),
814    ]
815}
816
817#[cfg(test)]
818mod tests {
819    use super::*;
820
821    #[test]
822    fn test_all_commands_defined() {
823        let cmds = all_commands();
824        let names: Vec<&str> = cmds.iter().map(|c| c.name).collect();
825        assert!(names.contains(&"server"));
826        assert!(names.contains(&"query"));
827        assert!(names.contains(&"insert"));
828        assert!(names.contains(&"get"));
829        assert!(names.contains(&"delete"));
830        assert!(names.contains(&"health"));
831        assert!(names.contains(&"tick"));
832        assert!(names.contains(&"migrate-from-redis"));
833        assert!(names.contains(&"status"));
834        assert!(names.contains(&"inspect"));
835        assert!(names.contains(&"connect"));
836        assert!(names.contains(&"version"));
837    }
838
839    #[test]
840    fn test_inspect_has_flags() {
841        let cmds = all_commands();
842        let inspect = cmds.iter().find(|c| c.name == "inspect").unwrap();
843        let flag_names: Vec<&str> = inspect.flags.iter().map(|f| f.long.as_str()).collect();
844        assert!(flag_names.contains(&"path"));
845        assert!(flag_names.contains(&"at"));
846    }
847
848    #[test]
849    fn test_server_has_flags() {
850        let cmds = all_commands();
851        let server = cmds.iter().find(|c| c.name == "server").unwrap();
852        let flag_names: Vec<&str> = server.flags.iter().map(|f| f.long.as_str()).collect();
853        assert!(flag_names.contains(&"path"));
854        assert!(flag_names.contains(&"bind"));
855        // Slice 5 of issue #574 — HTTP handler-pool knobs.
856        assert!(flag_names.contains(&"http-max-handlers"));
857        assert!(flag_names.contains(&"http-handler-timeout-ms"));
858        assert!(flag_names.contains(&"http-retry-after-secs"));
859    }
860
861    #[test]
862    fn test_server_help_text_lists_http_limit_flags() {
863        let help = command_help_text("server").unwrap();
864        assert!(help.contains("--http-max-handlers"));
865        assert!(help.contains("--http-handler-timeout-ms"));
866        assert!(help.contains("--http-retry-after-secs"));
867        assert!(help.contains("REDDB_HTTP_MAX_HANDLERS"));
868    }
869
870    #[test]
871    fn test_replica_has_flags() {
872        let cmds = all_commands();
873        let replica = cmds.iter().find(|c| c.name == "replica").unwrap();
874        let flag_names: Vec<&str> = replica.flags.iter().map(|f| f.long.as_str()).collect();
875        assert!(flag_names.contains(&"primary-addr"));
876        assert!(flag_names.contains(&"path"));
877        assert!(flag_names.contains(&"bind"));
878    }
879
880    #[test]
881    fn test_main_help_text() {
882        let help = main_help_text();
883        assert!(help.contains("reddb"));
884        assert!(help.contains("Usage: red"));
885        assert!(help.contains("Commands:"));
886        assert!(help.contains("server"));
887        assert!(help.contains("query"));
888        assert!(help.contains("Global flags:"));
889        assert!(help.contains("--help"));
890        assert!(help.contains("Examples:"));
891    }
892
893    #[test]
894    fn test_command_help_text() {
895        let help = command_help_text("server").unwrap();
896        assert!(help.contains("red server"));
897        assert!(help.contains("--path"));
898        assert!(help.contains("--bind"));
899    }
900
901    #[test]
902    fn test_replica_command_help() {
903        let help = command_help_text("replica").unwrap();
904        assert!(help.contains("red replica"));
905        assert!(help.contains("--primary-addr"));
906    }
907
908    #[test]
909    fn test_migrate_from_redis_command_help() {
910        let help = command_help_text("migrate-from-redis").unwrap();
911        assert!(help.contains("red migrate-from-redis"));
912        assert!(help.contains("--dry-run"));
913        assert!(help.contains("--redis-url"));
914        assert!(help.contains("application-owned helper"));
915    }
916
917    #[test]
918    fn test_command_help_text_unknown() {
919        assert!(command_help_text("nonexistent").is_none());
920    }
921
922    #[test]
923    fn test_flag_builder() {
924        let flag = Flag::new("output", "Output format")
925            .with_short('o')
926            .with_default("text")
927            .with_arg("FORMAT");
928
929        assert_eq!(flag.long, "output");
930        assert_eq!(flag.short, Some('o'));
931        assert_eq!(flag.description, "Output format");
932        assert_eq!(flag.default, Some("text".to_string()));
933        assert_eq!(flag.arg, Some("FORMAT".to_string()));
934    }
935
936    #[test]
937    fn test_completion_domains() {
938        let domains = completion_domains();
939        let names: Vec<&str> = domains.iter().map(|(n, _)| n.as_str()).collect();
940        assert!(names.contains(&"server"));
941        assert!(names.contains(&"query"));
942        assert!(names.contains(&"health"));
943    }
944
945    #[test]
946    fn test_completion_global_flags() {
947        let flags = completion_global_flags();
948        assert!(flags.contains(&("help", Some('h'))));
949        assert!(flags.contains(&("json", Some('j'))));
950        assert!(flags.contains(&("verbose", Some('v'))));
951        assert!(flags.contains(&("no-color", None)));
952    }
953}