diff --git a/Cargo.lock b/Cargo.lock index 6028625..831219e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -942,9 +942,9 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.6.15" +version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "754278eaff41b86ced9e2913b3f5ee8bd7c2446be81f0739a567e9d0ad6cdb3a" +checksum = "d86f4838bd1e232610932d5a26fcf58305d1dbc936590612af966f8365afe1e7" dependencies = [ "lazy_static", "libc", diff --git a/Cargo.toml b/Cargo.toml index ee3cb39..7eb25f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,6 @@ repository = "https://git.hatter.ink/hatter/cargotool" [dependencies] clap = "2.33.3" toml = "0.5.6" -rust_util = "0.6.15" +rust_util = "0.6.22" crates_io_api = "0.6" diff --git a/src/cmd.rs b/src/cmd.rs index 6ec2e12..bb8bdbd 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -1,7 +1,7 @@ use clap::{ArgMatches, App}; use rust_util::XResult; -pub type CommandError = XResult<()>; +pub type CommandResult = XResult; pub trait Command { @@ -9,5 +9,5 @@ pub trait Command { fn subcommand<'a>(&self) -> App<'a, 'a>; - fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError; + fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandResult; } diff --git a/src/cmd_crate.rs b/src/cmd_crate.rs index 222e4a1..bbb1aab 100644 --- a/src/cmd_crate.rs +++ b/src/cmd_crate.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use std::time::Duration; use clap::{ArgMatches, SubCommand, App, Arg}; use crates_io_api::{SyncClient, CrateResponse}; -use crate::cmd::{Command, CommandError}; +use crate::cmd::{Command, CommandResult}; 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")) } - 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( "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") { Ok(s) => s, Err(e) => { failure!("Read file Cargo.toml failed: {}", e); - return Ok(()); + return Ok(-1); }, }; let mut toml: toml::Value = match toml_str.parse() { Ok(t) => t, Err(e) => { failure!("Parse Cargo.toml failed: {}", e); - return Ok(()); + return Ok(-1); }, }; @@ -83,7 +83,7 @@ impl Command for CommandImpl { } } - Ok(()) + Ok(0) } } diff --git a/src/cmd_default.rs b/src/cmd_default.rs index a8e3664..05f2cee 100644 --- a/src/cmd_default.rs +++ b/src/cmd_default.rs @@ -1,5 +1,5 @@ use clap::{App, ArgMatches}; -use crate::cmd::CommandError; +use crate::cmd::CommandResult; pub struct CommandImpl; @@ -10,7 +10,7 @@ impl CommandImpl { app } - pub fn run(_arg_matches: &ArgMatches) -> CommandError { + pub fn run(_arg_matches: &ArgMatches) -> CommandResult { let logo = include_str!("../cargotool.logo.txt"); println!("{}", logo .replace("{color1}", "\x1B[1m\x1B[91m") @@ -20,6 +20,6 @@ impl CommandImpl { information!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); information!("Please use `{} --help` to see help message", env!("CARGO_PKG_NAME")); println!(); - Ok(()) + Ok(0) } } \ No newline at end of file diff --git a/src/cmd_gitcheck.rs b/src/cmd_gitcheck.rs new file mode 100644 index 0000000..561fa4e --- /dev/null +++ b/src/cmd_gitcheck.rs @@ -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) + } +} diff --git a/src/cmd_sample.rs b/src/cmd_sample.rs index 10d6282..3f00b0c 100644 --- a/src/cmd_sample.rs +++ b/src/cmd_sample.rs @@ -1,5 +1,5 @@ use clap::{ArgMatches, SubCommand, App}; -use crate::cmd::{Command, CommandError}; +use crate::cmd::{Command, CommandResult}; pub struct CommandImpl; @@ -11,7 +11,7 @@ impl Command for CommandImpl { SubCommand::with_name(self.name()).about("Sample subcommand") } - fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { - Ok(()) + fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandResult { + Ok(0) } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c4cd85d..31f9b3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,19 @@ mod cmd; mod cmd_default; mod cmd_crate; +mod cmd_gitcheck; mod cmd_sample; +use std::process; use clap::App; -use cmd::{Command, CommandError}; +use cmd::Command; +use rust_util::XResult; -fn main() -> CommandError { +fn main() -> XResult<()> { let commands: Vec> = vec![ Box::new(cmd_sample::CommandImpl), Box::new(cmd_crate::CommandImpl), + Box::new(cmd_gitcheck::CommandImpl), ]; let mut app = App::new(env!("CARGO_PKG_NAME")) .version(env!("CARGO_PKG_VERSION")) @@ -24,9 +28,9 @@ fn main() -> CommandError { let matches = app.get_matches(); for command in &commands { 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)?); }