From 181f5e1535fe9c4eeb1dd564ef4ef1e52611dd4c Mon Sep 17 00:00:00 2001 From: Max Hohlfeld Date: Wed, 18 Dec 2024 16:43:43 +0100 Subject: [PATCH] feat: wip cli implementation --- cli/Cargo.toml | 4 ++++ cli/src/db.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++-- config/src/lib.rs | 1 + 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index b6cfa8c7..8a6ec4fb 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -10,3 +10,7 @@ name = "db" path = "src/db.rs" [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"] } diff --git a/cli/src/db.rs b/cli/src/db.rs index 239ebf26..1721518d 100644 --- a/cli/src/db.rs +++ b/cli/src/db.rs @@ -1,3 +1,51 @@ -fn main() { - println!("abc"); +use sqlx::Executor; +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)) + } + } } diff --git a/config/src/lib.rs b/config/src/lib.rs index 6f6f5ca8..56f9a199 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -36,6 +36,7 @@ impl From for SmtpTlsType { } } +#[derive(Clone)] pub enum Environment { Development, Test,