Make it possible to launch main binary without database and/or library #88
45
src/main.rs
45
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: ILibrary, DB: IDatabase>(lib: Option<LIB>, db: Option<DB>) {
|
||||
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() {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
let db = if opt.no_database {
|
||||
None
|
||||
if opt.no_database {
|
||||
with_database(opt, None::<NoDatabase>);
|
||||
} 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::<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)]
|
||||
|
Loading…
Reference in New Issue
Block a user