#![recursion_limit = "2048"]
#![warn(clippy::all, clippy::correctness)]
#![warn(rust_2018_idioms)]
#![warn(clippy::pedantic)]
mod cli;
mod config;
#[cfg(feature = "discord")]
mod discord;
mod invidious;
mod player;
mod playlist;
#[allow(unused)]
mod podcast;
mod songtag;
mod sqlite;
mod track;
#[cfg(feature = "cover")]
mod ueberzug;
mod ui;
mod utils;
use anyhow::Result;
use clap::Parser;
use config::Settings;
use std::path::Path;
use std::process;
use ui::UI;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const MAX_DEPTH: usize = 4;
fn main() -> Result<()> {
let mut config = Settings::default();
config.load()?;
let args = cli::Args::parse();
if let Some(dir) = args.music_directory {
config.music_dir_from_cli = get_path(&dir);
}
config.disable_album_art_from_cli = args.disable_cover;
config.disable_discord_rpc_from_cli = args.disable_discord;
if let Some(d) = args.max_depth {
config.max_depth_cli = d;
} else {
config.max_depth_cli = MAX_DEPTH;
}
match args.action {
Some(cli::Action::Import { file }) => {
eprintln!("need to import from file {file}");
if let Some(path_str) = get_path(&file) {
if let Ok(db_path) = utils::get_app_config_path() {
if let Err(e) = podcast::import_from_opml(db_path.as_path(), &config, &path_str)
{
println!("Error when import file {file}: {e}");
}
}
}
process::exit(0);
}
Some(cli::Action::Export { file }) => {
eprintln!("need to export to file {file}");
let path_string = get_path_export(&file);
if let Ok(db_path) = utils::get_app_config_path() {
eprintln!("export to {path_string}");
if let Err(e) = podcast::export_to_opml(db_path.as_path(), &path_string) {
println!("Error when export file {file}: {e}");
}
}
process::exit(0);
}
None => {}
}
let mut ui = UI::new(&config);
ui.run();
Ok(())
}
fn get_path(dir: &str) -> Option<String> {
let music_dir: Option<String>;
let mut path = Path::new(&dir).to_path_buf();
if path.exists() {
if !path.has_root() {
if let Ok(p_base) = std::env::current_dir() {
path = p_base.join(path);
}
}
if let Ok(p_canonical) = path.canonicalize() {
path = p_canonical;
}
music_dir = Some(path.to_string_lossy().to_string());
} else {
eprintln!("Error: unknown directory '{dir}'");
process::exit(0);
}
music_dir
}
fn get_path_export(dir: &str) -> String {
let mut path = Path::new(&dir).to_path_buf();
if !path.has_root() {
if let Ok(p_base) = std::env::current_dir() {
path = p_base.join(path);
}
}
if let Ok(p_canonical) = path.canonicalize() {
path = p_canonical;
}
path.to_string_lossy().to_string()
}