use std::{path::PathBuf, process::Command};
use clap::{ArgMatches, Command as ClapCommand};
use music_exporter::PlatformType;
use serde::{Deserialize, Serialize};
use tokio::runtime::Runtime;
use crate::{
cli::{input_path, CliCommand},
config::Config,
config_path,
};
#[derive(Deserialize, Serialize, Default)]
pub struct MusicCliCommand {
pub music_file: Option<String>,
pub env_path: Option<String>,
}
impl CliCommand for MusicCliCommand {
fn get_subcommand() -> ClapCommand {
ClapCommand::new("music")
.about("sync subcommand")
.subcommand(ClapCommand::new("sync").about("save music"))
.subcommand(ClapCommand::new("open").about("open music file"))
.arg_required_else_help(true)
}
fn invoke(config: &mut Config, args_matches: &ArgMatches) {
if let Some(matches) = args_matches.subcommand_matches("sync") {
MusicCliCommand::sync_music(config, matches);
} else if let Some(_matches) = args_matches.subcommand_matches("open") {
MusicCliCommand::open_music_file(config);
}
}
}
impl MusicCliCommand {
pub fn get_music_file_path(config: &mut Config) -> PathBuf {
config_path!(
config,
music,
MusicCliCommand,
music_file,
"the file for music"
)
}
pub fn open_music_file(config: &mut Config) {
let music_file = MusicCliCommand::get_music_file_path(config);
println!("Opening music file at {}", music_file.display());
Command::new("vi")
.arg(&music_file)
.spawn()
.expect("Unable to open config with default editor")
.wait()
.expect("Error: Editor returned a non-zero status");
}
pub fn sync_music(config: &mut Config, _matches: &ArgMatches) {
let rt = Runtime::new().expect("Failed to create tokio runtime");
let music_file = MusicCliCommand::get_music_file_path(config);
let env_path = config_path!(config, music, MusicCliCommand, env_path, "the env path");
println!("music file: {:?}", music_file);
let platforms = vec![
PlatformType::Deezer,
PlatformType::Spotify,
PlatformType::Youtube,
];
rt.block_on(async {
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.format_target(false)
.format_timestamp(None)
.init();
music_exporter::cli_main(music_file, Some(env_path), &platforms).await
});
}
}