n4n5/commands/
music.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! To see all subcommands, run:
//! ```shell
//! n4n5 music
//! ```
//!use serde::Serialize;

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,
};

/// Movies configuration
#[derive(Deserialize, Serialize, Default)]
pub struct MusicCliCommand {
    /// Path to the movies file
    pub music_file: Option<String>,

    /// env path
    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 {
    /// Get the music file path
    pub fn get_music_file_path(config: &mut Config) -> PathBuf {
        config_path!(
            config,
            music,
            MusicCliCommand,
            music_file,
            "the file for music"
        )
    }

    /// open music file
    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");
    }

    /// Sync music
    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
        });
    }
}