2023-05-20 00:02:39 +02:00
|
|
|
use std::fs::OpenOptions;
|
2023-04-10 00:13:18 +02:00
|
|
|
use std::path::PathBuf;
|
2023-04-14 16:21:25 +02:00
|
|
|
use std::{ffi::OsString, io};
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2024-01-11 23:27:01 +01:00
|
|
|
use musichoard::database::NoDatabase;
|
|
|
|
use musichoard::library::NoLibrary;
|
2023-05-20 00:02:39 +02:00
|
|
|
use musichoard::Collection;
|
2023-04-14 16:21:25 +02:00
|
|
|
use ratatui::{backend::CrosstermBackend, Terminal};
|
2023-04-10 00:13:18 +02:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
use musichoard::{
|
2023-04-14 16:21:25 +02:00
|
|
|
database::{
|
|
|
|
json::{backend::JsonDatabaseFileBackend, JsonDatabase},
|
2023-05-10 22:52:03 +02:00
|
|
|
IDatabase,
|
2023-04-14 16:21:25 +02:00
|
|
|
},
|
|
|
|
library::{
|
|
|
|
beets::{
|
2023-04-27 20:09:45 +02:00
|
|
|
executor::{ssh::BeetsLibrarySshExecutor, BeetsLibraryProcessExecutor},
|
2023-04-14 16:21:25 +02:00
|
|
|
BeetsLibrary,
|
|
|
|
},
|
2023-05-10 22:52:03 +02:00
|
|
|
ILibrary,
|
2023-04-14 16:21:25 +02:00
|
|
|
},
|
2023-05-10 22:52:03 +02:00
|
|
|
MusicHoard,
|
2023-04-13 14:09:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
mod tui;
|
2023-05-10 22:52:03 +02:00
|
|
|
use tui::ui::Ui;
|
|
|
|
use tui::{event::EventChannel, handler::EventHandler, listener::EventListener, Tui};
|
2023-04-10 00:13:18 +02:00
|
|
|
|
|
|
|
#[derive(StructOpt)]
|
|
|
|
struct Opt {
|
2024-01-11 23:27:01 +01:00
|
|
|
#[structopt(long = "ssh", help = "Beets SSH URI")]
|
2023-04-14 16:21:25 +02:00
|
|
|
beets_ssh_uri: Option<OsString>,
|
|
|
|
|
2024-01-11 23:27:01 +01:00
|
|
|
#[structopt(long = "beets", help = "Beets config file path")]
|
2023-04-14 16:21:25 +02:00
|
|
|
beets_config_file_path: Option<OsString>,
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2024-01-11 23:27:01 +01:00
|
|
|
#[structopt(long = "no-library", help = "Do not connect to the library")]
|
|
|
|
no_library: bool,
|
|
|
|
|
2023-04-10 00:13:18 +02:00
|
|
|
#[structopt(
|
|
|
|
long = "database",
|
2024-01-11 23:27:01 +01:00
|
|
|
help = "Database file path",
|
2023-04-14 16:21:25 +02:00
|
|
|
default_value = "database.json"
|
2023-04-10 00:13:18 +02:00
|
|
|
)]
|
|
|
|
database_file_path: PathBuf,
|
2024-01-11 23:27:01 +01:00
|
|
|
|
|
|
|
#[structopt(long = "no-database", help = "Do not read from/write to the database")]
|
|
|
|
no_database: bool,
|
2023-04-10 00:13:18 +02:00
|
|
|
}
|
|
|
|
|
2024-01-11 23:27:01 +01:00
|
|
|
fn with<LIB: ILibrary, DB: IDatabase>(lib: Option<LIB>, db: Option<DB>) {
|
|
|
|
let music_hoard = MusicHoard::new(lib, db);
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
// Initialize the terminal user interface.
|
|
|
|
let backend = CrosstermBackend::new(io::stdout());
|
|
|
|
let terminal = Terminal::new(backend).expect("failed to initialise terminal");
|
|
|
|
|
|
|
|
let channel = EventChannel::new();
|
2023-05-10 22:52:03 +02:00
|
|
|
let listener = EventListener::new(channel.sender());
|
|
|
|
let handler = EventHandler::new(channel.receiver());
|
2023-04-13 14:09:59 +02:00
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
let ui = Ui::new(music_hoard).expect("failed to initialise ui");
|
2023-04-13 14:09:59 +02:00
|
|
|
|
|
|
|
// Run the TUI application.
|
2023-04-27 19:05:37 +02:00
|
|
|
Tui::run(terminal, ui, handler, listener).expect("failed to run tui");
|
2023-04-13 14:09:59 +02:00
|
|
|
}
|
|
|
|
|
2024-01-11 23:27:01 +01:00
|
|
|
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 {
|
2023-04-14 16:21:25 +02:00
|
|
|
let uri = uri.into_string().expect("invalid SSH URI");
|
2023-04-27 19:34:07 +02:00
|
|
|
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);
|
2024-01-11 23:27:01 +01:00
|
|
|
with(Some(BeetsLibrary::new(lib_exec)), db);
|
2023-04-14 16:21:25 +02:00
|
|
|
} else {
|
|
|
|
let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path);
|
2024-01-11 23:27:01 +01:00
|
|
|
with(Some(BeetsLibrary::new(lib_exec)), db);
|
2023-04-14 16:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 23:27:01 +01:00
|
|
|
fn main() {
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
|
|
|
if opt.no_database {
|
|
|
|
with_database(opt, None::<NoDatabase>);
|
|
|
|
} else {
|
|
|
|
// Create an empty database file if it does not exist.
|
|
|
|
match OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(&opt.database_file_path)
|
|
|
|
{
|
|
|
|
Ok(f) => {
|
|
|
|
drop(f);
|
|
|
|
JsonDatabase::new(JsonDatabaseFileBackend::new(&opt.database_file_path))
|
|
|
|
.save::<Collection>(&vec![])
|
|
|
|
.expect("failed to create empty database");
|
|
|
|
}
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
io::ErrorKind::AlreadyExists => {}
|
|
|
|
_ => panic!("failed to access database file"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path);
|
|
|
|
with_database(opt, Some(JsonDatabase::new(db_exec)));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:09:59 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
mod testlib;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
use musichoard::*;
|
|
|
|
|
|
|
|
pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| collection!());
|
2023-04-10 00:13:18 +02:00
|
|
|
}
|