summaryrefslogtreecommitdiffstats
path: root/src/cmd.rs
blob: 172f6a62de3140bd17624e54e91332f0e0260370 (plain) (blame)
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
mod recommend;
mod sql;
mod sync;

use clap::{
    app_from_crate, crate_authors, crate_description, crate_name,
    crate_version,
};
use failure::bail;

const _DUMMY_DEPENDENCY: &str = include_str!("../Cargo.toml");

trait Command {
    fn run(&self) -> failure::Fallible<()>;
}

pub fn run() -> failure::Fallible<()> {
    get_command()?.run()
}

fn get_command() -> failure::Fallible<Box<Command>> {
    let subcommands = vec![
        sync::subcommand(),
        sql::subcommand(),
        recommend::subcommand(),
    ];
    let mut app = app_from_crate!().subcommands(
        subcommands
            .into_iter()
            .map(|s| s.setting(clap::AppSettings::DisableVersion)),
    );
    let matches = app.clone().get_matches();

    let command: Box<Command> = match matches.subcommand() {
        ("sync", Some(matches)) => Box::new(sync::Command::new(matches)),
        ("sql", Some(matches)) => Box::new(sql::Command::new(matches)),
        ("recommend", Some(matches)) => {
            Box::new(recommend::Command::new(matches)?)
        }

        (name, Some(_)) => bail!("unknown subcommand: {}", name),
        (_, None) => {
            let mut stderr = std::io::stderr();
            app.write_long_help(&mut stderr)?;
            eprintln!("");
            bail!("no subcommand given")
        }
    };

    Ok(command)
}