summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-11-07 20:34:42 -0500
committerJesse Luehrs <doy@tozt.net>2018-11-07 20:34:42 -0500
commit1b18401ad9acc6f2fe6625b5cbb9b2b8f162536a (patch)
tree264bd3d34dec937e0ddfc2c277bb854f1d878067
parentf2c66c8e47fddd468b0b6894e7c1902b0baecc16 (diff)
downloadlastfm-query-1b18401ad9acc6f2fe6625b5cbb9b2b8f162536a.tar.gz
lastfm-query-1b18401ad9acc6f2fe6625b5cbb9b2b8f162536a.zip
factor out into subcommands
-rw-r--r--src/cli.rs55
-rw-r--r--src/main.rs18
2 files changed, 53 insertions, 20 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 4e1d7cb..27b2742 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,28 +1,51 @@
use clap;
+use failure;
const _DUMMY_DEPENDENCY: &'static str = include_str!("../Cargo.toml");
+pub enum Command {
+ Sync,
+}
+
pub struct Options {
- pub username: String,
- pub api_key: String,
+ pub command: Command,
+ pub username: Option<String>,
+ pub api_key: Option<String>,
}
-pub fn get_options() -> Options {
+pub fn get_options() -> failure::Fallible<Options> {
let matches = app_from_crate!()
- .arg(
- clap::Arg::with_name("username")
- .required(true)
- .help("last.fm username to fetch tracks for")
- )
- .arg(
- clap::Arg::with_name("api_key")
- .required(true)
- .help("last.fm api key")
+ .subcommand(
+ clap::SubCommand::with_name("sync")
+ .about("Updates the local copy of track data from last.fm")
+ .arg(
+ clap::Arg::with_name("username")
+ .short("u")
+ .long("username")
+ .value_name("USERNAME")
+ .required(true)
+ .help("last.fm username to fetch tracks for")
+ )
+ .arg(
+ clap::Arg::with_name("api-key")
+ .short("k")
+ .long("api-key")
+ .value_name("API_KEY")
+ .required(true)
+ .help("last.fm api key")
+ )
)
.get_matches();
- Options {
- username: matches.value_of("username").unwrap().to_string(),
- api_key: matches.value_of("api_key").unwrap().to_string(),
- }
+ let (command, sub_matches) = match matches.subcommand() {
+ ("sync", Some(matches)) => (Command::Sync, matches),
+ (name, Some(_)) => bail!("unknown subcommand: {}", name),
+ (_, None) => bail!("no subcommand given"),
+ };
+
+ Ok(Options {
+ command: command,
+ username: sub_matches.value_of("username").map(|s| s.to_string()),
+ api_key: sub_matches.value_of("api-key").map(|s| s.to_string()),
+ })
}
diff --git a/src/main.rs b/src/main.rs
index ed5a189..c104b23 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -17,9 +17,13 @@ mod lastfm;
mod paths;
mod db;
-fn run(opts: cli::Options) -> failure::Fallible<()> {
+fn sync(opts: &cli::Options) -> failure::Fallible<()> {
let db = db::DB::new(&paths::dbpath())?;
- let lastfm = lastfm::LastFMClient::new(&opts.api_key, &opts.username);
+ let lastfm = lastfm::LastFMClient::new(
+ opts.api_key.as_ref().unwrap(),
+ opts.username.as_ref().unwrap()
+ );
+
let exporter = exporter::Exporter::new(&db, &lastfm);
let to_fetch = exporter.tracks_to_sync()?;
@@ -39,6 +43,13 @@ fn run(opts: cli::Options) -> failure::Fallible<()> {
Ok(())
}
+fn run() -> failure::Fallible<()> {
+ let opts = cli::get_options()?;
+ match opts.command {
+ cli::Command::Sync => sync(&opts),
+ }
+}
+
fn program_name() -> failure::Fallible<String> {
let program = std::env::args()
.next()
@@ -52,8 +63,7 @@ fn program_name() -> failure::Fallible<String> {
}
fn main() {
- let opts = cli::get_options();
- match run(opts) {
+ match run() {
Ok(_) => {},
Err(e) => {
let name = program_name()