nhentairs/src/commands/view.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

2023-10-28 00:08:10 +00:00
use crate::api;
2020-09-12 12:26:07 +00:00
use crate::utils;
2020-12-01 15:02:05 +00:00
use crate::structs;
2020-09-12 12:26:07 +00:00
use std::env;
use std::process::exit;
use tokio::task::JoinHandle;
extern crate tokio;
extern crate reqwest;
pub async fn run(args: env::Args) {
let sauces = utils::get_arg_sauces(args).unwrap();
if sauces.len() < 1 {
2020-09-12 12:26:07 +00:00
eprintln!("Missing sauce(s)");
exit(1);
}
let client = reqwest::Client::new();
2020-12-02 15:34:42 +00:00
let mut handles: Vec<JoinHandle<(structs::GalleryInfo, i32)>> = Vec::with_capacity(sauces.len());
2020-09-12 12:26:07 +00:00
for sauce in sauces {
let cloned_client = client.clone();
handles.push(tokio::spawn(async move {
2023-10-28 00:08:10 +00:00
(api::get_sauce_info(cloned_client, sauce).await.unwrap(), sauce)
2020-09-12 12:26:07 +00:00
}));
}
let mut fail = false;
let mut one_done = false;
2020-09-12 12:26:07 +00:00
for handle in handles {
let (sauce_info, sauce) = handle.await.unwrap();
match sauce_info {
structs::GalleryInfo::Info(sauce_info) => {
if one_done {
println!("");
}
2020-12-20 10:14:55 +00:00
println!("{}", &sauce_info);
},
structs::GalleryInfo::Error(sauce_error) => {
if one_done {
eprintln!("");
}
eprintln!("Sauce: {}\nError: {}", sauce, sauce_error.error);
fail = true;
}
};
one_done = true;
2020-09-12 12:26:07 +00:00
}
if fail {
exit(1);
}
2020-09-12 12:26:07 +00:00
}