Making it possible to run setup db sql with cli tool
This commit is contained in:
parent
29561fb202
commit
2fd05e150b
@ -4,3 +4,5 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.20", features = ["derive"] }
|
||||
postgres = "0.19.9"
|
||||
|
@ -1,3 +1,39 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::env;
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user