midas_cli/commands/
mod.rs1pub mod db_lifecycle;
7pub mod db_schema;
8pub mod doctor;
9pub mod faker;
10pub mod generate;
11pub mod migrate;
12pub mod migrate_dev;
13pub mod prepare;
14pub mod schema;
15pub mod seed_meta;
16pub mod setup;
17pub mod source;
18pub mod sqlx;
19
20use anyhow::Result;
21use clap::{
22 ArgMatches,
23 Command,
24};
25use midas::config::Config;
26
27#[derive(Debug, Clone)]
29pub struct Context {
30 pub config: Config,
32 pub dry_run: bool,
34 pub no_lock: bool,
36 pub ignore_missing: bool,
38}
39
40impl Context {
41 pub fn new(config: Config) -> Self {
43 Self {
44 config,
45 dry_run: false,
46 no_lock: false,
47 ignore_missing: false,
48 }
49 }
50
51 pub fn with_flags(mut self, dry_run: bool, no_lock: bool, ignore_missing: bool) -> Self {
53 self.dry_run = dry_run;
54 self.no_lock = no_lock;
55 self.ignore_missing = ignore_missing;
56 self
57 }
58}
59
60pub fn register(app: Command) -> Command {
62 let app = migrate::register(app);
63 let app = schema::register(app);
64 let app = app.subcommand(db_command());
65 let app = generate::register(app);
66 let app = prepare::register(app);
67 let app = faker::register(app);
68 let app = setup::register(app);
69 let app = doctor::register(app);
70 sqlx::register(app)
71}
72
73pub fn dispatch(matches: &ArgMatches, context: &Context) -> Result<()> {
75 let Some((name, sub)) = matches.subcommand() else {
76 return Ok(());
77 };
78
79 if migrate::owns(name) {
80 return migrate::dispatch(name, sub, context);
81 }
82 if schema::owns(name) {
83 return schema::dispatch(name, sub, context);
84 }
85 if generate::owns(name) {
86 return generate::dispatch(name, sub, context);
87 }
88 if prepare::owns(name) {
89 return prepare::dispatch(name, sub, context);
90 }
91 if faker::owns(name) {
92 return faker::dispatch(name, sub, context);
93 }
94 if setup::owns(name) {
95 return setup::dispatch(name, sub, context);
96 }
97 if doctor::owns(name) {
98 return doctor::dispatch(name, sub, context);
99 }
100 if sqlx::owns(name) {
101 return sqlx::dispatch(name, sub, context);
102 }
103 if name == "db" {
104 return dispatch_db(sub, context);
105 }
106
107 anyhow::bail!("unknown subcommand `{name}`")
108}
109
110fn db_command() -> Command {
111 let mut command = Command::new("db")
112 .about("Database lifecycle and schema commands")
113 .arg_required_else_help(true);
114
115 for subcommand in db_lifecycle::subcommands() {
116 command = command.subcommand(subcommand);
117 }
118 for subcommand in db_schema::subcommands() {
119 command = command.subcommand(subcommand);
120 }
121
122 command
123}
124
125fn dispatch_db(matches: &ArgMatches, context: &Context) -> Result<()> {
126 let Some((name, sub)) = matches.subcommand() else {
127 return Ok(());
128 };
129
130 if db_lifecycle::owns(name) {
131 return db_lifecycle::dispatch(name, sub, context);
132 }
133 if db_schema::owns(name) {
134 return db_schema::dispatch(name, sub, context);
135 }
136
137 anyhow::bail!("unknown db subcommand `{name}`")
138}
139
140#[cfg(test)]
141mod tests {
142 use super::*;
143
144 #[test]
145 fn root_registers_expected_subcommands() {
146 let app = register(Command::new("midas"));
147 let names = app
148 .get_subcommands()
149 .map(|command| command.get_name())
150 .collect::<Vec<_>>();
151
152 for expected in [
153 "init", "run", "info", "db", "schema", "generate", "prepare", "sqlx", "doctor", "faker", "setup",
154 ] {
155 assert!(names.contains(&expected), "missing `{expected}` in {names:?}");
156 }
157 }
158
159 #[test]
160 fn db_registers_lifecycle_and_schema_commands() {
161 let db = db_command();
162 let names = db
163 .get_subcommands()
164 .map(|command| command.get_name())
165 .collect::<Vec<_>>();
166
167 for expected in ["create", "drop", "reset", "exists", "pull", "push", "diff"] {
168 assert!(names.contains(&expected), "missing `{expected}` in {names:?}");
169 }
170 }
171}