add --firsturl and --cat options to cli
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
LordMZTE 2021-04-06 20:46:21 +02:00
parent ad4289546a
commit b0e97574ec
2 changed files with 43 additions and 10 deletions

View file

@ -1,6 +1,7 @@
use anyhow::Result;
use anyhow::{Context, Result};
use log::info;
use reqwest::Client;
use std::io::Write;
use crate::util::IntoTableRow;
use jm_client_core::util::{self, api};
@ -9,12 +10,14 @@ pub async fn run(
http: &Client,
query: String,
user: Option<String>,
cat: Option<String>,
category: Option<String>,
firsturl: bool,
cat: bool,
) -> Result<()> {
let (memes, ..) = tokio::try_join!(
api::memes(
http,
cat.as_ref().map(String::clone),
category.as_ref().map(String::clone),
user.as_ref().map(String::clone),
),
async {
@ -25,7 +28,7 @@ pub async fn run(
}
},
async {
if let Some(c) = cat.as_ref() {
if let Some(c) = category.as_ref() {
util::assert_category_exists(http, c).await
} else {
Ok(())
@ -46,14 +49,32 @@ pub async fn run(
}
matches.sort_by(|a, b| b.1.cmp(&a.1));
let res = match (firsturl, cat) {
(false, false) => {
let mut table = crate::util::list_table();
let mut table = crate::util::list_table();
for m in matches {
table.add_row(m.0.into_table_row());
}
table.render().into_bytes()
},
for m in matches {
table.add_row(m.0.into_table_row());
}
(true, _) => matches
.first()
.context("No matches found")?
.0
.link
.clone()
.into_bytes(),
(_, true) => {
let url = &matches.first().context("No results found")?.0.link;
http.get(url).send().await?.bytes().await?.to_vec()
},
};
println!("{}", table.render());
let stdout = std::io::stdout();
let mut handle = stdout.lock();
handle.write_all(&res)?;
Ok(())
}

View file

@ -52,6 +52,16 @@ enum Cmd {
#[structopt(long, short, help = "filter by user")]
user: Option<String>,
#[structopt(long, help = "print the URL of the frst result")]
firsturl: bool,
#[structopt(
long,
help = "print the first search result's data to stdout",
conflicts_with = "firsturl"
)]
cat: bool,
},
#[structopt(about = "lists all memes")]
@ -97,7 +107,9 @@ async fn main() -> Result<()> {
query,
user,
category,
} => commands::search::run(&http, query, user, category).await?,
firsturl,
cat,
} => commands::search::run(&http, query, user, category, firsturl, cat).await?,
Cmd::List {
category,
user,