diff --git a/src/main.rs b/src/main.rs index f4b1e08..48cd409 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::fs::OpenOptions; use std::path::PathBuf; use std::{ffi::OsString, io}; +use musichoard::database::NoDatabase; use musichoard::library::NoLibrary; use musichoard::Collection; use ratatui::{backend::CrosstermBackend, Terminal}; @@ -65,11 +66,31 @@ fn with(lib: Option, db: Option) { Tui::run(terminal, ui, handler, listener).expect("failed to run tui"); } +fn with_database(opt: Opt, db: Option) { + if opt.no_library { + with(None::, db); + } else if let Some(uri) = opt.beets_ssh_uri { + let uri = uri.into_string().expect("invalid SSH URI"); + let beets_config_file_path = opt + .beets_config_file_path + .map(|s| s.into_string()) + .transpose() + .expect("failed to extract beets config file path"); + let lib_exec = BeetsLibrarySshExecutor::new(uri) + .expect("failed to initialise beets") + .config(beets_config_file_path); + with(Some(BeetsLibrary::new(lib_exec)), db); + } else { + let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path); + with(Some(BeetsLibrary::new(lib_exec)), db); + } +} + fn main() { let opt = Opt::from_args(); - let db = if opt.no_database { - None + if opt.no_database { + with_database(opt, None::); } else { // Create an empty database file if it does not exist. match OpenOptions::new() @@ -90,26 +111,8 @@ fn main() { } let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path); - Some(JsonDatabase::new(db_exec)) + with_database(opt, Some(JsonDatabase::new(db_exec))); }; - - if opt.no_library { - with(None::, db); - } else if let Some(uri) = opt.beets_ssh_uri { - let uri = uri.into_string().expect("invalid SSH URI"); - let beets_config_file_path = opt - .beets_config_file_path - .map(|s| s.into_string()) - .transpose() - .expect("failed to extract beets config file path"); - let lib_exec = BeetsLibrarySshExecutor::new(uri) - .expect("failed to initialise beets") - .config(beets_config_file_path); - with(Some(BeetsLibrary::new(lib_exec)), db); - } else { - let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path); - with(Some(BeetsLibrary::new(lib_exec)), db); - } } #[cfg(test)]