work
This commit is contained in:
@@ -4,9 +4,13 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.32", features = ["derive"] }
|
||||
clap = { version = "4.5.32", features = ["derive", "env"] }
|
||||
color-eyre = "0.6.3"
|
||||
figment = { version = "0.10.19", features = ["toml"] }
|
||||
sea-schema = "0.16.1"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
syn = { version = "2.0.100", features = ["extra-traits", "full"] }
|
||||
tokio = { version = "1.44.1", features = ["full"] }
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
url = "2.5.4"
|
||||
|
||||
29
src/generate.rs
Normal file
29
src/generate.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use color_eyre::{
|
||||
eyre::{eyre, ContextCompat},
|
||||
Result,
|
||||
};
|
||||
use url::Url;
|
||||
pub async fn generate(database_url: String) -> Result<()> {
|
||||
let url = Url::parse(&database_url)?;
|
||||
|
||||
tracing::trace!(?url);
|
||||
|
||||
let is_sqlite = url.scheme() == "sqlite";
|
||||
|
||||
let database_name = (if !is_sqlite {
|
||||
let database_name = url
|
||||
.path_segments()
|
||||
.context("No database name as part of path")?
|
||||
.next()
|
||||
.context("No database name as part of path")?;
|
||||
|
||||
if database_name.is_empty() {
|
||||
return Err(eyre!("Database path name is empty"));
|
||||
}
|
||||
Ok(database_name)
|
||||
} else {
|
||||
Ok(Default::default())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
66
src/main.rs
66
src/main.rs
@@ -1,22 +1,72 @@
|
||||
mod generate;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use clap::Parser;
|
||||
use color_eyre::Result;
|
||||
use figment::{
|
||||
providers::{Format, Serialized, Toml},
|
||||
Figment,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const DEFAULTS: [&str; 2] = ["prelude.rs", "mod.rs"];
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct Config {
|
||||
sea_orm: SeaOrmConfig,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct SeaOrmConfig {
|
||||
expanded_format: bool,
|
||||
table: SeaOrmTableConfig,
|
||||
include_hidden_tables: bool,
|
||||
with_serde: bool,
|
||||
serde_skip_deserializing_primary_key: bool,
|
||||
serde_skip_hidden_column: bool,
|
||||
}
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct SeaOrmTableConfig {
|
||||
include_hidden: bool,
|
||||
only: Vec<String>,
|
||||
exclude: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sea_orm: SeaOrmConfig {
|
||||
table: SeaOrmTableConfig {
|
||||
include_hidden: false,
|
||||
only: Vec::new(),
|
||||
exclude: Vec::new(),
|
||||
},
|
||||
expanded_format: false,
|
||||
include_hidden_tables: false,
|
||||
with_serde: false,
|
||||
serde_skip_hidden_column: false,
|
||||
serde_skip_deserializing_primary_key: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct Args {
|
||||
data_dir: PathBuf,
|
||||
#[clap(short, long, default_value = "_entities")]
|
||||
entities_folder: String,
|
||||
#[clap(short, long, default_value = "generator.toml")]
|
||||
config: String,
|
||||
#[clap(short, long, env = "DATABASE_URL")]
|
||||
database_url: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
tracing_subscriber::fmt::init();
|
||||
let args = Args::parse();
|
||||
tracing::info!(?args.data_dir, ?args.entities_folder, "Generating/Updating models from entities");
|
||||
|
||||
let config: Config = Figment::new()
|
||||
.merge(Serialized::defaults(Config::default()))
|
||||
.merge(Toml::file(&args.config))
|
||||
.extract()?;
|
||||
tracing::info!(?config);
|
||||
tracing::info!(?args);
|
||||
generate::generate(args.database_url).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user