Making it possible to run setup db sql with cli tool

This commit is contained in:
shockrah 2024-10-28 20:58:36 -07:00
parent 29561fb202
commit 2fd05e150b
2 changed files with 40 additions and 2 deletions

View File

@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.20", features = ["derive"] }
postgres = "0.19.9"

View File

@ -1,3 +1,39 @@
fn main() { use std::env;
println!("Hello, world!"); use std::fs;
use clap::Parser;
use postgres::{Client, NoTls};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Setup everything from scratch
#[arg(short, long)]
setup: bool,
/// Setup just the Database
#[arg(short, long)]
db: bool
}
fn execute_sql(connection_string: &str, filename: &'static str) -> Result<(), postgres::Error> {
let content = fs::read_to_string(filename)
.expect(&format!("Failed to load file: {}", filename));
let mut client = Client::connect(connection_string, NoTls)?;
client.batch_execute(&content)
}
fn setup_database() -> Result<(), postgres::Error> {
// Check to make sure we have the DB url set to connect
const KEY: &'static str = "DB_CONNECTION_STRING" ;
let connection_string = env::var(KEY).expect(
&format!("The env var {} is not set!", KEY)
);
execute_sql(&connection_string, "../db/tables.sql")
}
fn main() {
let args = Args::parse();
if args.db { let _ = setup_database(); }
println!("Setup is {}", args.setup);
println!("Setup is {}", args.db);
} }