feat: wip cli implementation
This commit is contained in:
parent
9a6c714d74
commit
181f5e1535
@ -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"] }
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ impl From<String> for SmtpTlsType {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Environment {
|
||||
Development,
|
||||
Test,
|
||||
|
Loading…
x
Reference in New Issue
Block a user