feat: wip cli implementation

This commit is contained in:
Max Hohlfeld 2024-12-18 16:43:43 +01:00
parent 9a6c714d74
commit 181f5e1535
3 changed files with 55 additions and 2 deletions

View File

@ -10,3 +10,7 @@ name = "db"
path = "src/db.rs" path = "src/db.rs"
[dependencies] [dependencies]
clap = { version = "4.5.23", features = ["derive"] }
brass-config = { path = "../config" }
async-std = { version = "1.13.0", features = ["attributes"] }
sqlx = { version = "0.8.2", features = ["runtime-async-std"] }

View File

@ -1,3 +1,51 @@
fn main() { use sqlx::Executor;
println!("abc"); use std::str::FromStr;
use brass_config::{load_config, parse_env, Environment};
use clap::{Args, Parser, Subcommand};
use sqlx::{postgres::PgConnectOptions, Connection, PgConnection};
#[derive(Parser)]
#[command(about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Command,
#[arg(short, long, global = true, help = "Choose the environment (development, test, production).", value_parser = parse_env, default_value = "development")]
environment: Environment,
}
#[derive(Subcommand)]
enum Command {
Setup,
Reset,
}
#[derive(Args)]
struct CommandArgs {}
#[async_std::main]
async fn main() {
let cli = Cli::parse();
let config = load_config(&cli.environment).expect("Could not load config!");
match cli.command {
Command::Setup => {}
Command::Reset => {
let db_config = PgConnectOptions::from_str(&config.database_url).expect("Invalid DATABASE_URL!");
let db_name = db_config
.get_database()
.expect("Failed to get database name!");
let root_db_config = db_config.clone().database("postgres");
let mut root_connection: PgConnection = Connection::connect_with(&root_db_config).await.unwrap();
let query = format!("DROP DATABASE {}", db_name);
root_connection
.execute(query.as_str())
.await
.expect("Failed to drop database!");
//Ok(String::from(db_name))
}
}
} }

View File

@ -36,6 +36,7 @@ impl From<String> for SmtpTlsType {
} }
} }
#[derive(Clone)]
pub enum Environment { pub enum Environment {
Development, Development,
Test, Test,