Make it possible to launch main binary without database and/or library #88

Merged
wojtek merged 3 commits from 87---make-it-possible-to-launch-main-binary-without-database-and/or-library into main 2024-01-11 23:27:01 +01:00
Showing only changes of commit a192e2631e - Show all commits

View File

@ -2,6 +2,7 @@ use std::fs::OpenOptions;
use std::path::PathBuf; use std::path::PathBuf;
use std::{ffi::OsString, io}; use std::{ffi::OsString, io};
use musichoard::database::NoDatabase;
use musichoard::library::NoLibrary; use musichoard::library::NoLibrary;
use musichoard::Collection; use musichoard::Collection;
use ratatui::{backend::CrosstermBackend, Terminal}; use ratatui::{backend::CrosstermBackend, Terminal};
@ -65,11 +66,31 @@ fn with<LIB: ILibrary, DB: IDatabase>(lib: Option<LIB>, db: Option<DB>) {
Tui::run(terminal, ui, handler, listener).expect("failed to run tui"); Tui::run(terminal, ui, handler, listener).expect("failed to run tui");
} }
fn with_database<DB: IDatabase>(opt: Opt, db: Option<DB>) {
if opt.no_library {
with(None::<NoLibrary>, 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() { fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
let db = if opt.no_database { if opt.no_database {
None with_database(opt, None::<NoDatabase>);
} else { } else {
// Create an empty database file if it does not exist. // Create an empty database file if it does not exist.
match OpenOptions::new() match OpenOptions::new()
@ -90,26 +111,8 @@ fn main() {
} }
let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path); 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::<NoLibrary>, 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)] #[cfg(test)]