diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-12-23 02:49:04 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-12-23 02:49:04 -0500 |
commit | 96c8e1e5ac561e450b78912f65b2415d41fc6a58 (patch) | |
tree | 6710f042521c37e85b90d3b2c8cf2b3a1f98d8cb /src/cmd.rs | |
parent | bb860c9777d971a72e685148c6c1b2cdc13b3e0b (diff) | |
download | lastfm-query-96c8e1e5ac561e450b78912f65b2415d41fc6a58.tar.gz lastfm-query-96c8e1e5ac561e450b78912f65b2415d41fc6a58.zip |
Diffstat (limited to 'src/cmd.rs')
-rw-r--r-- | src/cmd.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/cmd.rs b/src/cmd.rs new file mode 100644 index 0000000..172f6a6 --- /dev/null +++ b/src/cmd.rs @@ -0,0 +1,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) +} |