summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-11-10 02:23:44 -0500
committerJesse Luehrs <doy@tozt.net>2018-11-10 02:23:44 -0500
commita683b67a7e459dffd67ba6321b97b1c3ce263bde (patch)
tree27b470fa32cd2e1f021ddf91f46f1259921027be
parent1c8a540c365f281b9f8dee171360347f0dc897ff (diff)
downloadlastfm-query-a683b67a7e459dffd67ba6321b97b1c3ce263bde.tar.gz
lastfm-query-a683b67a7e459dffd67ba6321b97b1c3ce263bde.zip
refactor some stuff out of the cli module
-rw-r--r--src/cli.rs55
-rw-r--r--src/cmd/sql.rs35
-rw-r--r--src/cmd/sync.rs26
3 files changed, 66 insertions, 50 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 4b6aa0d..1049b64 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -3,62 +3,27 @@ use cmd;
const _DUMMY_DEPENDENCY: &'static str = include_str!("../Cargo.toml");
enum Command {
- Sync {
- username: String,
- },
- SQL {
- query: String,
- tsv: bool,
- },
+ Sync(cmd::sync::Options),
+ SQL(cmd::sql::Options),
}
pub fn run() -> failure::Fallible<()> {
- let command = get_options()?;
- match command {
- Command::Sync { username } => cmd::sync::run(&username),
- Command::SQL { query, tsv } => cmd::sql::run(&query, tsv),
+ match get_options()? {
+ Command::Sync(options) => cmd::sync::run(&options),
+ Command::SQL(options) => cmd::sql::run(&options),
}
}
fn get_options() -> failure::Fallible<Command> {
let matches = app_from_crate!()
- .subcommand(
- clap::SubCommand::with_name("sync")
- .about("Updates the local copy of track data from last.fm")
- .arg(
- clap::Arg::with_name("username")
- .required(true)
- .help("last.fm username to fetch tracks for")
- )
- )
- .subcommand(
- clap::SubCommand::with_name("sql")
- .about("Run a query against the local database")
- .arg(
- clap::Arg::with_name("query")
- .required(true)
- .help("query to run")
- )
- .arg(
- clap::Arg::with_name("tsv")
- .long("tsv")
- .help("format output as tsv")
- )
- )
+ .subcommand(cmd::sync::subcommand())
+ .subcommand(cmd::sql::subcommand())
.get_matches();
let command = match matches.subcommand() {
- ("sync", Some(matches)) => {
- Command::Sync {
- username: matches.value_of("username").unwrap().to_string(),
- }
- },
- ("sql", Some(matches)) => {
- Command::SQL {
- query: matches.value_of("query").unwrap().to_string(),
- tsv: matches.is_present("tsv"),
- }
- },
+ ("sync", Some(matches)) => Command::Sync(cmd::sync::options(matches)),
+ ("sql", Some(matches)) => Command::SQL(cmd::sql::options(matches)),
+
(name, Some(_)) => bail!("unknown subcommand: {}", name),
(_, None) => bail!("no subcommand given"),
};
diff --git a/src/cmd/sql.rs b/src/cmd/sql.rs
index ae398b5..b6a77cf 100644
--- a/src/cmd/sql.rs
+++ b/src/cmd/sql.rs
@@ -1,11 +1,40 @@
use db;
use paths;
-pub fn run(query: &str, tsv: bool) -> failure::Fallible<()> {
+use clap;
+
+pub struct Options {
+ query: String,
+ tsv: bool,
+}
+
+pub fn subcommand<'a, 'b>() -> clap::App<'a, 'b> {
+ clap::SubCommand::with_name("sql")
+ .about("Run a query against the local database")
+ .arg(
+ clap::Arg::with_name("query")
+ .required(true)
+ .help("query to run")
+ )
+ .arg(
+ clap::Arg::with_name("tsv")
+ .long("tsv")
+ .help("format output as tsv")
+ )
+}
+
+pub fn options<'a>(matches: &clap::ArgMatches<'a>) -> Options {
+ Options {
+ query: matches.value_of("query").unwrap().to_string(),
+ tsv: matches.is_present("tsv"),
+ }
+}
+
+pub fn run(options: &Options) -> failure::Fallible<()> {
let db = db::DB::new(&paths::db_path()?)?;
let rows_cell = std::cell::Cell::new(Some(vec![]));
- let cols = db.query(query, |row| {
+ let cols = db.query(&options.query, |row| {
let display_row: Vec<String> = (0..row.column_count())
.map(|i| row.get_raw(i))
.map(|v| format_value(&v))
@@ -17,7 +46,7 @@ pub fn run(query: &str, tsv: bool) -> failure::Fallible<()> {
let rows = rows_cell.into_inner().unwrap();
- if tsv {
+ if options.tsv {
print_tsv(&rows);
}
else {
diff --git a/src/cmd/sync.rs b/src/cmd/sync.rs
index 3f80202..9e09289 100644
--- a/src/cmd/sync.rs
+++ b/src/cmd/sync.rs
@@ -2,9 +2,31 @@ use db;
use lastfm;
use paths;
-pub fn run(username: &str) -> failure::Fallible<()> {
+use clap;
+
+pub struct Options {
+ username: String,
+}
+
+pub fn subcommand<'a, 'b>() -> clap::App<'a, 'b> {
+ clap::SubCommand::with_name("sync")
+ .about("Updates the local copy of track data from last.fm")
+ .arg(
+ clap::Arg::with_name("username")
+ .required(true)
+ .help("last.fm username to fetch tracks for")
+ )
+}
+
+pub fn options<'a>(matches: &clap::ArgMatches<'a>) -> Options {
+ Options {
+ username: matches.value_of("username").unwrap().to_string(),
+ }
+}
+
+pub fn run(options: &Options) -> failure::Fallible<()> {
let db = db::DB::new(&paths::db_path()?)?;
- let lastfm = lastfm::LastFMClient::new(username)?;
+ let lastfm = lastfm::LastFMClient::new(&options.username)?;
let from = db.most_recent_timestamp()?.map(|x| x + 1);
let to_fetch = lastfm.track_count(from)?;