1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
use { atty::Stream, clap::{ App, crate_authors, crate_description, crate_name, crate_version, load_yaml, }, std::{ io::{self, BufRead}, process, }, }; use crate::{errors, reader}; pub struct Options { pub list: Option<String>, pub timeout: u64, pub concurrency: usize, } impl Options { pub fn get() -> Self { let yaml = load_yaml!("cli.yaml"); let usage = format!( "{0} -l {1} {2}\n cat {1} | {0} {2}", crate_name!(), "FILE", "[OPTIONS]" ); let app = App::from(yaml) .author(crate_authors!()) .about(crate_description!()) .name(crate_name!()) .version(crate_version!()) .override_usage(&*usage); let matches = app.get_matches(); Self { list: Some( matches .value_of("list") .unwrap_or("").to_owned() ), timeout: matches .value_of_t("timeout") .unwrap_or(30), concurrency: matches .value_of_t("concurrency") .unwrap_or(15) } } pub fn urls(&mut self) -> Vec<String> { let mut urls: Vec<String> = vec![]; if self.list == Some("".to_string()) { if atty::isnt(Stream::Stdin) { let stdin = io::stdin(); urls.extend(stdin.lock().lines().map(|l| l.unwrap())) } else { errors::show("No input target provided!".to_string()); process::exit(1) } } else { urls.extend(reader::from_file(self.list.as_ref().unwrap())) } urls } }