Skip to content

Commit f08487f

Browse files
committed
Exported rorm-cli as library
1 parent 30e71f6 commit f08487f

File tree

11 files changed

+271
-261
lines changed

11 files changed

+271
-261
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
".",
4+
"rorm-cli-lib",
45
"rorm-declaration",
56
"rorm-sql",
67
]

src/entry.rs

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
use clap::{ArgAction, Parser, Subcommand};
2+
3+
use crate::init::init;
4+
use crate::make_migrations::{run_make_migrations, MakeMigrationsOptions};
5+
use crate::migrate::{run_migrate, MigrateOptions};
6+
use crate::squash_migrations::squash_migrations;
7+
8+
#[derive(Subcommand)]
9+
pub enum InitDriver {
10+
#[clap(about = "Initialize a sqlite configuration")]
11+
Sqlite {
12+
#[clap(long = "filename")]
13+
#[clap(default_value_t = String::from("db.sqlite3"))]
14+
#[clap(help = "Name of the sqlite file.")]
15+
filename: String,
16+
},
17+
#[clap(about = "Initialize a mysql configuration")]
18+
Mysql {
19+
#[clap(long = "host")]
20+
#[clap(default_value_t = String::from("127.0.0.1"))]
21+
#[clap(help = "The address to use to connect to the database.")]
22+
host: String,
23+
#[clap(long = "port")]
24+
#[clap(default_value_t = 5432)]
25+
#[clap(help = "The port to use to connect to the database.")]
26+
port: u16,
27+
#[clap(long = "user")]
28+
#[clap(default_value_t = String::from("dbuser"))]
29+
#[clap(help = "The user to use to connect to the database.")]
30+
user: String,
31+
#[clap(long = "password")]
32+
#[clap(
33+
help = "Set the password. To minimize the risk of exposing your password, use --ask-password instead."
34+
)]
35+
password: Option<String>,
36+
#[clap(long = "ask-password")]
37+
#[clap(
38+
help = "Ask for the password for the database. If specified with the --password option, this value will be prevalent."
39+
)]
40+
ask_password: bool,
41+
#[clap(long = "name")]
42+
#[clap(default_value_t = String::from("dbname"))]
43+
#[clap(help = "The name of the database to connect to.")]
44+
name: String,
45+
},
46+
#[clap(about = "Initialize a postgres configuration")]
47+
Postgres {
48+
#[clap(long = "host")]
49+
#[clap(default_value_t = String::from("127.0.0.1"))]
50+
#[clap(help = "The address to use to connect to the database.")]
51+
host: String,
52+
#[clap(long = "port")]
53+
#[clap(default_value_t = 5432)]
54+
#[clap(help = "The port to use to connect to the database.")]
55+
port: u16,
56+
#[clap(long = "user")]
57+
#[clap(default_value_t = String::from("dbuser"))]
58+
#[clap(help = "The user to use to connect to the database.")]
59+
user: String,
60+
#[clap(long = "password")]
61+
#[clap(
62+
help = "Set the password. To minimize the risk of exposing your password, use --ask-password instead."
63+
)]
64+
password: Option<String>,
65+
#[clap(long = "ask-password")]
66+
#[clap(
67+
help = "Ask for the password for the database. If specified with the --password option, this value will be prevalent."
68+
)]
69+
ask_password: bool,
70+
#[clap(long = "name")]
71+
#[clap(default_value_t = String::from("dbname"))]
72+
#[clap(help = "The name of the database to connect to.")]
73+
name: String,
74+
},
75+
}
76+
77+
#[derive(Subcommand)]
78+
enum Commands {
79+
#[clap(about = "Create the database configuration file")]
80+
Init {
81+
#[clap(long = "database-config")]
82+
#[clap(default_value_t = String::from("./database.toml"))]
83+
#[clap(help = "Path to the database configuration file that should be created.")]
84+
database_config: String,
85+
86+
#[clap(short = 'f', long = "force")]
87+
#[clap(help = "Overwrite the database configuration if it is existent already")]
88+
force: bool,
89+
90+
#[clap(subcommand)]
91+
driver: InitDriver,
92+
},
93+
94+
#[clap(about = "Tool to create migrations")]
95+
MakeMigrations {
96+
#[clap(long = "models-file")]
97+
#[clap(default_value_t=String::from("./.models.json"))]
98+
#[clap(help = "Location of the intermediate representation of models.")]
99+
models_file: String,
100+
101+
#[clap(short = 'm', long = "migration-dir")]
102+
#[clap(default_value_t=String::from("./migrations/"))]
103+
#[clap(help = "Destination to / from which migrations are written / read.")]
104+
migration_dir: String,
105+
106+
#[clap(help = "Use this name as migration name instead of generating one.")]
107+
name: Option<String>,
108+
109+
#[clap(long = "non-interactive")]
110+
#[clap(action = ArgAction::SetTrue)]
111+
#[clap(help = "If set, no questions will be asked.")]
112+
non_interactive: bool,
113+
114+
#[clap(long = "disable-warnings")]
115+
#[clap(action = ArgAction::SetTrue)]
116+
#[clap(help = "If set, no warnings will be printed.")]
117+
warnings_disabled: bool,
118+
},
119+
120+
#[clap(about = "Apply migrations")]
121+
Migrate {
122+
#[clap(short = 'm', long = "migration-dir")]
123+
#[clap(default_value_t=String::from("./migrations/"))]
124+
#[clap(help = "Destination to / from which migrations are written / read.")]
125+
migration_dir: String,
126+
127+
#[clap(long = "database-config")]
128+
#[clap(default_value_t=String::from("./database.toml"))]
129+
#[clap(help = "Path to the database configuration file.")]
130+
database_config: String,
131+
132+
#[clap(long = "log-sql")]
133+
#[clap(action = ArgAction::SetTrue)]
134+
#[clap(help = "If turned on, all queries to the database will be logged")]
135+
log_queries: bool,
136+
137+
#[clap(long = "apply-until")]
138+
#[clap(id = "MIGRATION_ID")]
139+
#[clap(help = "Only apply the migrations to (inclusive) the given migration.")]
140+
apply_until: Option<u16>,
141+
},
142+
143+
#[clap(about = "Squash migrations")]
144+
SquashMigrations {
145+
#[clap(short = 'm', long = "migration-dir")]
146+
#[clap(default_value_t = String::from("./migrations/"))]
147+
#[clap(help = "Destination to / from which migrations are written / read.")]
148+
migration_dir: String,
149+
150+
#[clap(help = "First migration to start squashing from.")]
151+
first_migration: u16,
152+
153+
#[clap(help = "Last migration to squash.")]
154+
last_migration: u16,
155+
},
156+
157+
#[clap(about = "Merge migrations")]
158+
MergeMigrations {},
159+
}
160+
161+
#[derive(Parser)]
162+
#[clap(version, about = "CLI tool for rorm", long_about = None)]
163+
#[clap(arg_required_else_help = true)]
164+
#[clap(name = "rorm-cli")]
165+
pub struct Cli {
166+
#[clap(subcommand)]
167+
command: Option<Commands>,
168+
}
169+
170+
pub async fn entry(cli: Cli) -> anyhow::Result<()> {
171+
match cli.command {
172+
Some(Commands::Init {
173+
force,
174+
driver,
175+
database_config,
176+
}) => init(database_config, driver, force)?,
177+
Some(Commands::MakeMigrations {
178+
models_file,
179+
migration_dir,
180+
name,
181+
non_interactive,
182+
warnings_disabled,
183+
}) => {
184+
run_make_migrations(MakeMigrationsOptions {
185+
models_file,
186+
migration_dir,
187+
name,
188+
non_interactive,
189+
warnings_disabled,
190+
})?;
191+
}
192+
Some(Commands::Migrate {
193+
migration_dir,
194+
database_config,
195+
log_queries,
196+
apply_until,
197+
}) => {
198+
run_migrate(MigrateOptions {
199+
migration_dir,
200+
database_config,
201+
log_queries,
202+
apply_until,
203+
})
204+
.await?;
205+
}
206+
Some(Commands::SquashMigrations {
207+
migration_dir,
208+
first_migration,
209+
last_migration,
210+
}) => {
211+
squash_migrations(migration_dir, first_migration, last_migration).await?;
212+
}
213+
_ => {}
214+
}
215+
216+
Ok(())
217+
}

src/init/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::process::exit;
55

66
use rorm_declaration::config::{DatabaseConfig, DatabaseDriver};
77

8+
use crate::entry::InitDriver;
89
use crate::migrate::config::DatabaseConfigFile;
9-
use crate::InitDriver;
1010

1111
pub fn init(database_configuration: String, driver: InitDriver, force: bool) -> anyhow::Result<()> {
1212
let p = Path::new(&database_configuration);

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Exports for [rorm-cli-lib].
2+
///
3+
/// Reexports for executing the defined cli parser via another
4+
pub mod entry;
5+
/// This module is used for creating a configuration file that can be used by the
6+
/// binary version.
7+
pub mod init;
8+
/// This module handles the creation of migration files
9+
pub mod make_migrations;
10+
/// This module is used for applying migrations
11+
pub mod migrate;
12+
13+
mod linter;
14+
mod squash_migrations;
15+
mod utils;

0 commit comments

Comments
 (0)