From a192e2631e8a1a2423ef8fadbde9b5cb3c9f0f60 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Thu, 11 Jan 2024 22:50:04 +0100 Subject: [PATCH] Tidier main --- src/main.rs | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) 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)]