52 lines
1.9 KiB
Rust
52 lines
1.9 KiB
Rust
use std::time::Duration;
|
|
use clap::{ArgMatches, SubCommand, App, Arg};
|
|
use crates_io_api::{SyncClient, CrateResponse};
|
|
use crate::cmd::{Command, CommandError};
|
|
|
|
pub struct CommandImpl;
|
|
|
|
impl Command for CommandImpl {
|
|
|
|
fn name(&self) -> &str { "crate" }
|
|
|
|
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
|
SubCommand::with_name(self.name()).about("Crate subcommand")
|
|
.arg(Arg::with_name("add").short("A").long("add").multiple(true).takes_value(true).help("Add crates to project"))
|
|
}
|
|
|
|
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
|
|
let crate_io_client = SyncClient::new(
|
|
"cargotool (https://hatter.me/)", Duration::from_millis(1000)
|
|
)?;
|
|
|
|
if let Some(add_crates) = sub_arg_matches.values_of("add") {
|
|
for c in add_crates {
|
|
find_create(&crate_io_client, c);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn find_create(crate_io_client: &SyncClient, crate_name: &str) -> Option<CrateResponse> {
|
|
let c = match crate_io_client.get_crate(crate_name) {
|
|
Ok(c) => c, Err(e) => {
|
|
failure!("Crate not found or network error: {}", e);
|
|
return None;
|
|
},
|
|
};
|
|
let crate_data = &c.crate_data;
|
|
if crate_data.id != crate_name {
|
|
failure!("Crate not found: {}", crate_name);
|
|
return None;
|
|
}
|
|
if crate_data.downloads < 1000 {
|
|
failure!("Crate: {}, downloaded less then 1,000, is: {}, max version: {}", crate_name, crate_data.downloads, crate_data.max_version);
|
|
} else if crate_data.downloads < 10000 {
|
|
warning!("Crate: {}, downloaded less then 10,000, is: {}, max version: {}", crate_name, crate_data.downloads, crate_data.max_version);
|
|
} else {
|
|
success!("Crate: {}, downloaded more then 10,000, is: {}, max version: {}", crate_name, crate_data.downloads, crate_data.max_version);
|
|
}
|
|
Some(c)
|
|
} |