From 6b17222076923300441394e10733fb87e30fda9c Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Thu, 22 Aug 2024 11:19:35 +0200 Subject: [PATCH] add drop db argument --- ffplayout/src/main.rs | 22 +++++++++++++++++++++- ffplayout/src/utils/args_parse.rs | 9 ++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/ffplayout/src/main.rs b/ffplayout/src/main.rs index b7b1f225..ae428d6a 100644 --- a/ffplayout/src/main.rs +++ b/ffplayout/src/main.rs @@ -2,7 +2,7 @@ use std::{ collections::HashSet, env, fs::File, - io, + io::{self, stdin, stdout, Write}, process::exit, sync::{atomic::AtomicBool, Arc, Mutex}, thread, @@ -14,6 +14,7 @@ use actix_web::{ }; use actix_web_grants::authorities::AttachAuthorities; use actix_web_httpauth::{extractors::bearer::BearerAuth, middleware::HttpAuthentication}; +use sqlx::{migrate::MigrateDatabase, Sqlite}; #[cfg(all(not(debug_assertions), feature = "embed_frontend"))] use actix_web_static_files::ResourceFiles; @@ -35,6 +36,7 @@ use ffplayout::{ utils::{ args_parse::run_args, config::get_config, + db_path, logging::{init_logging, MailQueue}, playlist::generate_playlist, }, @@ -289,6 +291,24 @@ async fn main() -> std::io::Result<()> { playlist, Arc::new(AtomicBool::new(false)), ); + } else if ARGS.drop_db { + let mut drop_answer = String::new(); + + print!("Drop Database [Y/n]: "); + stdout().flush().unwrap(); + + stdin() + .read_line(&mut drop_answer) + .expect("Did not enter a yes or no?"); + + let drop = drop_answer.trim().to_lowercase().starts_with('y'); + + if drop { + match Sqlite::drop_database(db_path().unwrap()).await { + Ok(_) => info!("Successfully dropped DB"), + Err(e) => error!("{e}"), + }; + } } else if !ARGS.init { error!("Run ffplayout with parameters! Run ffplayout -h for more information."); } diff --git a/ffplayout/src/utils/args_parse.rs b/ffplayout/src/utils/args_parse.rs index bdb09a94..5101c010 100644 --- a/ffplayout/src/utils/args_parse.rs +++ b/ffplayout/src/utils/args_parse.rs @@ -35,9 +35,16 @@ pub struct Args { #[clap(short, long, help = "Add a global admin user")] pub add: bool, - #[clap(long, env, help = "path to database file")] + #[clap(long, env, help = "Path to database file")] pub db: Option, + #[clap( + long, + env, + help = "Drop database. WARNING: this will delete all configurations!" + )] + pub drop_db: bool, + #[clap( short, long,