Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
implement 'add-to-shelf' command
Browse files Browse the repository at this point in the history
  • Loading branch information
thundergolfer-two committed Sep 1, 2019
1 parent 9673966 commit f707c9c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ FLAGS:
-V, --version Prints version information

SUBCOMMANDS:
add-to-shelf Add a book to an existing shelf [In Progress]
add-to-shelf Add a book to an existing shelf (eg. currently-reading, to-read)
auth Setup OAuth for the CLI (1 time only)
finished Tell Goodreads you've finished a book you're currently reading
finished Tell Goodreads you've finished a book that you're currently reading
help Prints this message or the help of the given subcommand(s)
me Show your User ID
new Tell Goodreads you've started a new book
Expand Down
59 changes: 50 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ mod ux;
#[structopt(raw(setting = "structopt::clap::AppSettings::ColoredHelp"))]
enum Cli {
#[structopt(name = "add-to-shelf")]
/// Add a book to an existing shelf [In Progress]
AddToShelf {},
/// Add a book to an existing shelf (eg. currently-reading, to-read)
AddToShelf {
#[structopt(short = "t", long = "title")]
title: Option<String>,
#[structopt(short = "s", long = "shelf")]
shelf: Option<String>,
},
#[structopt(name = "me")]
/// Show your User ID
Me {},
Expand All @@ -38,7 +43,7 @@ enum Cli {
title: Option<String>,
},
#[structopt(name = "update")]
/// Update progress on a book you're currently reading
/// Update progress on a book that you're currently reading
Update {
#[structopt(short = "t", long = "title")]
title: Option<String>,
Expand Down Expand Up @@ -215,15 +220,51 @@ fn run_command(
gr_client: &api_client::GoodreadsApiClient,
) -> BoxResult<()> {
match args {
Cli::AddToShelf {} => {
// TODO(Jonathon): Stop hardcoding these
let res = gr_client.add_to_shelf(9282, "to-read");
Cli::AddToShelf { shelf, title } => {
let mut shelf_answer = String::new();
let mut title_answer = String::new();
let target_shelf = shelf.as_ref().unwrap_or_else(|| {
println!("❓: Which shelf would you like to add the book to?");
stdin()
.read_line(&mut shelf_answer)
.expect("Failed to read your input");
&shelf_answer
});
let title_query = title.as_ref().unwrap_or_else(|| {
println!("🔎 What's the title of the book?");
stdin()
.read_line(&mut title_answer)
.expect("Failed to read your input");
&title_answer
});
let res = gr_client
.search_books(&title_query, "title")
.and_then(|xml| {
models::parse_book_search_results(&xml).map_err(|err| err.to_string())
})
.and_then(|results| {
println!("📖 Results:");
for (i, result) in results.iter().enumerate() {
println!("{}. {} - {}", i + 1, result.1, result.2);
}
println!("\nWhich one is the one you'd like to add?");
let choice = ux::get_choice(1, results.len() as u32)
.map_err(|_err| "Failed to get user choice")?;
let book_to_update = results
.get((choice as usize) - 1)
.expect("Should never access an invalid index here");
Ok(book_to_update.0)
})
.and_then(|id| {
gr_client.add_to_shelf(id, target_shelf)?;
Ok(id)
});
match res {
Ok(_) => {
println!("Added ✅");
println!("Added to {}!", target_shelf);
Ok(())
}
Err(err) => bail!("fuck: {}", err),
Err(err) => bail!("Error: {}", err),
}
}
Cli::New { title } => {
Expand All @@ -249,7 +290,7 @@ fn run_command(
.map_err(|_err| "Failed to get user choice")?;
let book_to_update = results
.get((choice as usize) - 1)
.expect("Should never here access an invalid index");
.expect("Should never access an invalid index here");
Ok(book_to_update.0)
})
.and_then(|id| {
Expand Down

0 comments on commit f707c9c

Please sign in to comment.