diff --git a/admin-cli/Cargo.toml b/admin-cli/Cargo.toml index 3f57ea5..417a9a4 100644 --- a/admin-cli/Cargo.toml +++ b/admin-cli/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +clap = { version = "4.5.20", features = ["derive"] } +postgres = "0.19.9" diff --git a/admin-cli/src/main.rs b/admin-cli/src/main.rs index e7a11a9..a9b7dd7 100644 --- a/admin-cli/src/main.rs +++ b/admin-cli/src/main.rs @@ -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); }