From ef7cd5bc677c6aabf5517e74ab64eb84d4784057 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 10 Nov 2018 02:42:17 -0500 Subject: unify cli and cmd --- src/cmd/mod.rs | 27 +++++++++++++++++++++++++++ src/cmd/sql.rs | 3 +-- src/cmd/sync.rs | 3 +-- 3 files changed, 29 insertions(+), 4 deletions(-) (limited to 'src/cmd') diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index a25e955..89c9f5b 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,2 +1,29 @@ pub mod sql; pub mod sync; + +const _DUMMY_DEPENDENCY: &'static str = include_str!("../../Cargo.toml"); + +pub trait Command { + fn run(&self) -> failure::Fallible<()>; +} + +pub fn run() -> failure::Fallible<()> { + get_command()?.run() +} + +fn get_command() -> failure::Fallible> { + let matches = app_from_crate!() + .subcommand(sync::subcommand()) + .subcommand(sql::subcommand()) + .get_matches(); + + let command: Box = match matches.subcommand() { + ("sync", Some(matches)) => Box::new(sync::Command::new(matches)), + ("sql", Some(matches)) => Box::new(sql::Command::new(matches)), + + (name, Some(_)) => bail!("unknown subcommand: {}", name), + (_, None) => bail!("no subcommand given"), + }; + + Ok(command) +} diff --git a/src/cmd/sql.rs b/src/cmd/sql.rs index f0dd4b0..d31f326 100644 --- a/src/cmd/sql.rs +++ b/src/cmd/sql.rs @@ -1,4 +1,3 @@ -use cli; use db; use paths; @@ -33,7 +32,7 @@ impl Command { } } -impl cli::Command for Command { +impl super::Command for Command { fn run(&self) -> failure::Fallible<()> { let db = db::DB::new(&paths::db_path()?)?; diff --git a/src/cmd/sync.rs b/src/cmd/sync.rs index 3dc7ba8..5b71954 100644 --- a/src/cmd/sync.rs +++ b/src/cmd/sync.rs @@ -1,4 +1,3 @@ -use cli; use db; use lastfm; use paths; @@ -27,7 +26,7 @@ impl Command { } } -impl cli::Command for Command { +impl super::Command for Command { fn run(&self) -> failure::Fallible<()> { let db = db::DB::new(&paths::db_path()?)?; let lastfm = lastfm::LastFMClient::new(&self.username)?; -- cgit v1.2.3-54-g00ecf