feat: add cmd_create
This commit is contained in:
1211
Cargo.lock
generated
1211
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -15,4 +15,5 @@ repository = "https://git.hatter.ink/hatter/cargotool"
|
|||||||
clap = "2.33.3"
|
clap = "2.33.3"
|
||||||
toml = "0.5.6"
|
toml = "0.5.6"
|
||||||
rust_util = "0.6.15"
|
rust_util = "0.6.15"
|
||||||
|
crates_io_api = "0.6"
|
||||||
|
|
||||||
|
|||||||
52
src/cmd_crate.rs
Normal file
52
src/cmd_crate.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
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!("Create not found or network error: {}", e);
|
||||||
|
return None;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let crate_data = &c.crate_data;
|
||||||
|
if crate_data.id != crate_name {
|
||||||
|
failure!("Create 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)
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
mod cmd;
|
mod cmd;
|
||||||
mod cmd_default;
|
mod cmd_default;
|
||||||
|
mod cmd_crate;
|
||||||
mod cmd_test;
|
mod cmd_test;
|
||||||
|
|
||||||
use clap::App;
|
use clap::App;
|
||||||
@@ -9,7 +10,8 @@ use cmd::{Command, CommandError};
|
|||||||
|
|
||||||
fn main() -> CommandError {
|
fn main() -> CommandError {
|
||||||
let commands: Vec<Box<dyn Command>> = vec![
|
let commands: Vec<Box<dyn Command>> = vec![
|
||||||
Box::new(cmd_test::CommandImpl)
|
Box::new(cmd_test::CommandImpl),
|
||||||
|
Box::new(cmd_crate::CommandImpl),
|
||||||
];
|
];
|
||||||
let mut app = App::new(env!("CARGO_PKG_NAME"))
|
let mut app = App::new(env!("CARGO_PKG_NAME"))
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
|
|||||||
Reference in New Issue
Block a user