add --firsturl and --cat options to cli
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ad4289546a
commit
b0e97574ec
2 changed files with 43 additions and 10 deletions
|
@ -1,6 +1,7 @@
|
||||||
use anyhow::Result;
|
use anyhow::{Context, Result};
|
||||||
use log::info;
|
use log::info;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
use crate::util::IntoTableRow;
|
use crate::util::IntoTableRow;
|
||||||
use jm_client_core::util::{self, api};
|
use jm_client_core::util::{self, api};
|
||||||
|
@ -9,12 +10,14 @@ pub async fn run(
|
||||||
http: &Client,
|
http: &Client,
|
||||||
query: String,
|
query: String,
|
||||||
user: Option<String>,
|
user: Option<String>,
|
||||||
cat: Option<String>,
|
category: Option<String>,
|
||||||
|
firsturl: bool,
|
||||||
|
cat: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (memes, ..) = tokio::try_join!(
|
let (memes, ..) = tokio::try_join!(
|
||||||
api::memes(
|
api::memes(
|
||||||
http,
|
http,
|
||||||
cat.as_ref().map(String::clone),
|
category.as_ref().map(String::clone),
|
||||||
user.as_ref().map(String::clone),
|
user.as_ref().map(String::clone),
|
||||||
),
|
),
|
||||||
async {
|
async {
|
||||||
|
@ -25,7 +28,7 @@ pub async fn run(
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async {
|
async {
|
||||||
if let Some(c) = cat.as_ref() {
|
if let Some(c) = category.as_ref() {
|
||||||
util::assert_category_exists(http, c).await
|
util::assert_category_exists(http, c).await
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -46,14 +49,32 @@ pub async fn run(
|
||||||
}
|
}
|
||||||
|
|
||||||
matches.sort_by(|a, b| b.1.cmp(&a.1));
|
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 {
|
for m in matches {
|
||||||
table.add_row(m.0.into_table_row());
|
table.add_row(m.0.into_table_row());
|
||||||
}
|
}
|
||||||
|
table.render().into_bytes()
|
||||||
|
},
|
||||||
|
|
||||||
println!("{}", table.render());
|
(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()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let stdout = std::io::stdout();
|
||||||
|
let mut handle = stdout.lock();
|
||||||
|
handle.write_all(&res)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,16 @@ enum Cmd {
|
||||||
|
|
||||||
#[structopt(long, short, help = "filter by user")]
|
#[structopt(long, short, help = "filter by user")]
|
||||||
user: Option<String>,
|
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")]
|
#[structopt(about = "lists all memes")]
|
||||||
|
@ -97,7 +107,9 @@ async fn main() -> Result<()> {
|
||||||
query,
|
query,
|
||||||
user,
|
user,
|
||||||
category,
|
category,
|
||||||
} => commands::search::run(&http, query, user, category).await?,
|
firsturl,
|
||||||
|
cat,
|
||||||
|
} => commands::search::run(&http, query, user, category, firsturl, cat).await?,
|
||||||
Cmd::List {
|
Cmd::List {
|
||||||
category,
|
category,
|
||||||
user,
|
user,
|
||||||
|
|
Loading…
Add table
Reference in a new issue