aboutsummaryrefslogtreecommitdiffstats
path: root/src/command_handler.rs
blob: 742b79e480c5e859aa1c9c382390b205a0003529 (plain)
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
use clap::{App, ArgMatches};

use crate::actions::*;
use crate::arg_parser;
use crate::commands;
use crate::errors::*;
use crate::structs::*;

pub fn parse_sub_command(matches: &ArgMatches) -> commands::Command {
    match matches.subcommand_name() {
        Some("download") => commands::Command::Download,
        Some("ls") | Some("list") => commands::Command::List,
        Some("play") => commands::Command::Play,
        Some("sub") | Some("subscribe") => commands::Command::Subscribe,
        Some("search") => commands::Command::Search,
        Some("rm") => commands::Command::Remove,
        Some("completion") => commands::Command::Complete,
        Some("refresh") => commands::Command::Refresh,
        Some("update") => commands::Command::Update,
        _ => commands::Command::NoMatch,
    }
}

pub fn handle_matches(
    version: &str,
    state: &mut State,
    config: &Config,
    app: &mut App,
    matches: &ArgMatches,
) -> Result<()> {
    let command = parse_sub_command(matches);
    match command {
        commands::Command::Download => {
            arg_parser::download(state, matches)?;
        }
        commands::Command::List => {
            arg_parser::list(state, matches)?;
        }
        commands::Command::Play => {
            arg_parser::play(state, matches)?;
        }
        commands::Command::Subscribe => {
            arg_parser::subscribe(state, config, matches)?;
        }
        commands::Command::Search => {
            println!("This feature is coming soon...");
        }
        commands::Command::Remove => {
            arg_parser::remove(state, matches)?;
        }
        commands::Command::Complete => {
            arg_parser::complete(app, matches)?;
        }
        commands::Command::Refresh => {
            update_rss(state);
        }
        commands::Command::Update => {
            check_for_update(version)?;
        }
        _ => (),
    };
    Ok(())
}