feat: CommandError -> CommandResult, add git check

This commit is contained in:
2020-12-27 15:31:06 +08:00
parent c13952a886
commit 86dcbef727
8 changed files with 44 additions and 20 deletions

4
Cargo.lock generated
View File

@@ -942,9 +942,9 @@ dependencies = [
[[package]] [[package]]
name = "rust_util" name = "rust_util"
version = "0.6.15" version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "754278eaff41b86ced9e2913b3f5ee8bd7c2446be81f0739a567e9d0ad6cdb3a" checksum = "d86f4838bd1e232610932d5a26fcf58305d1dbc936590612af966f8365afe1e7"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"libc", "libc",

View File

@@ -14,6 +14,6 @@ repository = "https://git.hatter.ink/hatter/cargotool"
[dependencies] [dependencies]
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.22"
crates_io_api = "0.6" crates_io_api = "0.6"

View File

@@ -1,7 +1,7 @@
use clap::{ArgMatches, App}; use clap::{ArgMatches, App};
use rust_util::XResult; use rust_util::XResult;
pub type CommandError = XResult<()>; pub type CommandResult = XResult<i32>;
pub trait Command { pub trait Command {
@@ -9,5 +9,5 @@ pub trait Command {
fn subcommand<'a>(&self) -> App<'a, 'a>; fn subcommand<'a>(&self) -> App<'a, 'a>;
fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError; fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandResult;
} }

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::time::Duration; use std::time::Duration;
use clap::{ArgMatches, SubCommand, App, Arg}; use clap::{ArgMatches, SubCommand, App, Arg};
use crates_io_api::{SyncClient, CrateResponse}; use crates_io_api::{SyncClient, CrateResponse};
use crate::cmd::{Command, CommandError}; use crate::cmd::{Command, CommandResult};
pub struct CommandImpl; pub struct CommandImpl;
@@ -16,7 +16,7 @@ impl Command for CommandImpl {
.arg(Arg::with_name("add").short("A").long("add").multiple(true).takes_value(true).help("Add crates to project")) .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 { fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandResult {
let crate_io_client = SyncClient::new( let crate_io_client = SyncClient::new(
"cargotool (https://hatter.me/)", Duration::from_millis(1000) "cargotool (https://hatter.me/)", Duration::from_millis(1000)
)?; )?;
@@ -24,13 +24,13 @@ impl Command for CommandImpl {
let toml_str = match fs::read_to_string("Cargo.toml") { let toml_str = match fs::read_to_string("Cargo.toml") {
Ok(s) => s, Err(e) => { Ok(s) => s, Err(e) => {
failure!("Read file Cargo.toml failed: {}", e); failure!("Read file Cargo.toml failed: {}", e);
return Ok(()); return Ok(-1);
}, },
}; };
let mut toml: toml::Value = match toml_str.parse() { let mut toml: toml::Value = match toml_str.parse() {
Ok(t) => t, Err(e) => { Ok(t) => t, Err(e) => {
failure!("Parse Cargo.toml failed: {}", e); failure!("Parse Cargo.toml failed: {}", e);
return Ok(()); return Ok(-1);
}, },
}; };
@@ -83,7 +83,7 @@ impl Command for CommandImpl {
} }
} }
Ok(()) Ok(0)
} }
} }

View File

@@ -1,5 +1,5 @@
use clap::{App, ArgMatches}; use clap::{App, ArgMatches};
use crate::cmd::CommandError; use crate::cmd::CommandResult;
pub struct CommandImpl; pub struct CommandImpl;
@@ -10,7 +10,7 @@ impl CommandImpl {
app app
} }
pub fn run(_arg_matches: &ArgMatches) -> CommandError { pub fn run(_arg_matches: &ArgMatches) -> CommandResult {
let logo = include_str!("../cargotool.logo.txt"); let logo = include_str!("../cargotool.logo.txt");
println!("{}", logo println!("{}", logo
.replace("{color1}", "\x1B[1m\x1B[91m") .replace("{color1}", "\x1B[1m\x1B[91m")
@@ -20,6 +20,6 @@ impl CommandImpl {
information!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); information!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
information!("Please use `{} --help` to see help message", env!("CARGO_PKG_NAME")); information!("Please use `{} --help` to see help message", env!("CARGO_PKG_NAME"));
println!(); println!();
Ok(()) Ok(0)
} }
} }

20
src/cmd_gitcheck.rs Normal file
View File

@@ -0,0 +1,20 @@
use clap::{ArgMatches, SubCommand, App/*, Arg */};
use rust_util::util_git;
use crate::cmd::{Command, CommandResult};
pub struct CommandImpl;
impl Command for CommandImpl {
fn name(&self) -> &str { "gitcheck" }
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Git check 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) -> CommandResult {
// util_git::git_fetch_dry_run("working_dir");
Ok(0)
}
}

View File

@@ -1,5 +1,5 @@
use clap::{ArgMatches, SubCommand, App}; use clap::{ArgMatches, SubCommand, App};
use crate::cmd::{Command, CommandError}; use crate::cmd::{Command, CommandResult};
pub struct CommandImpl; pub struct CommandImpl;
@@ -11,7 +11,7 @@ impl Command for CommandImpl {
SubCommand::with_name(self.name()).about("Sample subcommand") SubCommand::with_name(self.name()).about("Sample subcommand")
} }
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandResult {
Ok(()) Ok(0)
} }
} }

View File

@@ -3,15 +3,19 @@
mod cmd; mod cmd;
mod cmd_default; mod cmd_default;
mod cmd_crate; mod cmd_crate;
mod cmd_gitcheck;
mod cmd_sample; mod cmd_sample;
use std::process;
use clap::App; use clap::App;
use cmd::{Command, CommandError}; use cmd::Command;
use rust_util::XResult;
fn main() -> CommandError { fn main() -> XResult<()> {
let commands: Vec<Box<dyn Command>> = vec![ let commands: Vec<Box<dyn Command>> = vec![
Box::new(cmd_sample::CommandImpl), Box::new(cmd_sample::CommandImpl),
Box::new(cmd_crate::CommandImpl), Box::new(cmd_crate::CommandImpl),
Box::new(cmd_gitcheck::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"))
@@ -24,9 +28,9 @@ fn main() -> CommandError {
let matches = app.get_matches(); let matches = app.get_matches();
for command in &commands { for command in &commands {
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) { if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
return command.run(&matches, sub_cmd_matches); process::exit( command.run(&matches, sub_cmd_matches)?);
} }
} }
cmd_default::CommandImpl::run(&matches) process::exit(cmd_default::CommandImpl::run(&matches)?);
} }