diff --git a/__crypto/simple_contract/Cargo.lock b/__crypto/simple_contract/Cargo.lock index 0472123..2ae7e01 100644 --- a/__crypto/simple_contract/Cargo.lock +++ b/__crypto/simple_contract/Cargo.lock @@ -425,10 +425,11 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.6.25" +version = "0.6.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26a080a375b53e1a584388ac4c611f55e41629c79b40ed1c630d497b995c8cf8" +checksum = "3cf5c22991d8fa9a12da6e745e9ea6180df21ed48a8e28431c64fa5a2985d5d9" dependencies = [ + "clap", "lazy_static", "libc", "term", @@ -528,9 +529,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "syn" -version = "1.0.56" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9802ddde94170d186eeee5005b798d9c159fa970403f1be19976d0cfb939b72" +checksum = "4211ce9909eb971f111059df92c45640aad50a619cf55cd76476be803c4c68e6" dependencies = [ "proc-macro2", "quote", diff --git a/__crypto/simple_contract/Cargo.toml b/__crypto/simple_contract/Cargo.toml index 763b7d2..c2c566a 100644 --- a/__crypto/simple_contract/Cargo.toml +++ b/__crypto/simple_contract/Cargo.toml @@ -16,5 +16,5 @@ ripemd160 = "0.8.0" sha2 = "0.8.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -rust_util = "0.6" +rust_util = { version = "0.6", features = ["use_clap"] } clap = "2.33" diff --git a/__crypto/simple_contract/src/cmd.rs b/__crypto/simple_contract/src/cmd.rs deleted file mode 100644 index 3a0da05..0000000 --- a/__crypto/simple_contract/src/cmd.rs +++ /dev/null @@ -1,11 +0,0 @@ -use clap::{ArgMatches, App}; -use rust_util::XResult; -pub type CommandError = XResult<()>; - -pub trait Command { - fn name(&self) -> &str; - - fn subcommand<'a>(&self) -> App<'a, 'a>; - - fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError; -} diff --git a/__crypto/simple_contract/src/cmd_credit_create.rs b/__crypto/simple_contract/src/cmd_credit_create.rs index 3d7edb6..d5e0a40 100644 --- a/__crypto/simple_contract/src/cmd_credit_create.rs +++ b/__crypto/simple_contract/src/cmd_credit_create.rs @@ -1,6 +1,6 @@ use std::fs; use clap::{App, Arg, ArgMatches, SubCommand}; -use crate::cmd::{Command, CommandError}; +use rust_util::util_clap::{Command, CommandError}; use crate::util::JsonKeyPair; use crate::tx::{Transaction, TransactionBody}; use crate::engine::ContractEngine; @@ -49,6 +49,6 @@ impl Command for CommandImpl { let engine = ContractEngine::new(); engine.execute(&tx)?; - Ok(()) + Ok(None) } } diff --git a/__crypto/simple_contract/src/cmd_credit_issue.rs b/__crypto/simple_contract/src/cmd_credit_issue.rs index d5fdef9..8abc415 100644 --- a/__crypto/simple_contract/src/cmd_credit_issue.rs +++ b/__crypto/simple_contract/src/cmd_credit_issue.rs @@ -1,6 +1,6 @@ use std::fs; use clap::{App, Arg, ArgMatches, SubCommand}; -use crate::cmd::{Command, CommandError}; +use rust_util::util_clap::{Command, CommandError}; use crate::util::JsonKeyPair; use crate::tx::{Transaction, TransactionBody}; use crate::engine::ContractEngine; @@ -54,6 +54,6 @@ impl Command for CommandImpl { let engine = ContractEngine::new(); engine.execute(&tx)?; - Ok(()) + Ok(None) } } diff --git a/__crypto/simple_contract/src/cmd_credit_query.rs b/__crypto/simple_contract/src/cmd_credit_query.rs index 6c69a75..d9efe89 100644 --- a/__crypto/simple_contract/src/cmd_credit_query.rs +++ b/__crypto/simple_contract/src/cmd_credit_query.rs @@ -1,6 +1,6 @@ use std::fs; use clap::{App, Arg, ArgMatches, SubCommand}; -use crate::cmd::{Command, CommandError}; +use rust_util::util_clap::{Command, CommandError}; use crate::util::JsonKeyPair; use crate::tx::{Transaction, TransactionBody}; use crate::engine::ContractEngine; @@ -49,6 +49,6 @@ impl Command for CommandImpl { let engine = ContractEngine::new(); engine.execute(&tx)?; - Ok(()) + Ok(None) } } diff --git a/__crypto/simple_contract/src/cmd_credit_queryall.rs b/__crypto/simple_contract/src/cmd_credit_queryall.rs index 0ae0e78..2440c58 100644 --- a/__crypto/simple_contract/src/cmd_credit_queryall.rs +++ b/__crypto/simple_contract/src/cmd_credit_queryall.rs @@ -1,6 +1,6 @@ use std::fs; use clap::{App, Arg, ArgMatches, SubCommand}; -use crate::cmd::{Command, CommandError}; +use rust_util::util_clap::{Command, CommandError}; use crate::util::JsonKeyPair; use crate::tx::{Transaction, TransactionBody}; use crate::engine::ContractEngine; @@ -44,6 +44,6 @@ impl Command for CommandImpl { let engine = ContractEngine::new(); engine.execute(&tx)?; - Ok(()) + Ok(None) } } diff --git a/__crypto/simple_contract/src/cmd_credit_transfer.rs b/__crypto/simple_contract/src/cmd_credit_transfer.rs index 5194ccc..45e1aaf 100644 --- a/__crypto/simple_contract/src/cmd_credit_transfer.rs +++ b/__crypto/simple_contract/src/cmd_credit_transfer.rs @@ -1,6 +1,6 @@ use std::fs; use clap::{App, Arg, ArgMatches, SubCommand}; -use crate::cmd::{Command, CommandError}; +use rust_util::util_clap::{Command, CommandError}; use crate::util::JsonKeyPair; use crate::tx::{Transaction, TransactionBody}; use crate::engine::ContractEngine; @@ -54,6 +54,6 @@ impl Command for CommandImpl { let engine = ContractEngine::new(); engine.execute(&tx)?; - Ok(()) + Ok(None) } } diff --git a/__crypto/simple_contract/src/cmd_default.rs b/__crypto/simple_contract/src/cmd_default.rs deleted file mode 100644 index 7e8925a..0000000 --- a/__crypto/simple_contract/src/cmd_default.rs +++ /dev/null @@ -1,17 +0,0 @@ -use clap::{App, Arg, ArgMatches}; -use crate::cmd::CommandError; - -pub struct CommandImpl; -impl CommandImpl { - - pub fn process_command<'a>(app: App<'a, 'a>) -> App<'a, 'a> { - app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info")) - } - - pub fn run(arg_matches: &ArgMatches) -> CommandError { - let verbose_count = arg_matches.occurrences_of("verbose"); - information!("Verbose count: {}", verbose_count); - information!("This is default command cli ..."); - Ok(()) - } -} \ No newline at end of file diff --git a/__crypto/simple_contract/src/cmd_genkey.rs b/__crypto/simple_contract/src/cmd_genkey.rs index 3c947fb..7960e5a 100644 --- a/__crypto/simple_contract/src/cmd_genkey.rs +++ b/__crypto/simple_contract/src/cmd_genkey.rs @@ -1,7 +1,7 @@ use std::fs; use std::fs::File; use clap::{App, Arg, ArgMatches, SubCommand}; -use crate::cmd::{Command, CommandError}; +use rust_util::util_clap::{Command, CommandError}; use crate::util::{JsonKeyPair, make_key_pair}; pub struct CommandImpl; @@ -21,12 +21,12 @@ impl Command for CommandImpl { let tag = sub_arg_matches.value_of("tag"); if let Ok(_) = File::open(output) { failure!("Output file exists: {}", output); - return Ok(()); + return Ok(None); } let (pri_key, pub_key) = make_key_pair(); let json_key_pair = JsonKeyPair::from(pri_key, pub_key, tag.map(|t| t.into())); success!("Write gen key file: {}", output); fs::write(output, json_key_pair.to_json()?.as_bytes())?; - Ok(()) + Ok(None) } } diff --git a/__crypto/simple_contract/src/main.rs b/__crypto/simple_contract/src/main.rs index 5572317..c8db560 100644 --- a/__crypto/simple_contract/src/main.rs +++ b/__crypto/simple_contract/src/main.rs @@ -7,8 +7,6 @@ mod credit_util; mod engine; mod engine_plugin_credit; -mod cmd; -mod cmd_default; mod cmd_genkey; mod cmd_credit_create; mod cmd_credit_issue; @@ -16,30 +14,32 @@ mod cmd_credit_transfer; mod cmd_credit_query; mod cmd_credit_queryall; -use clap::App; -use cmd::{Command, CommandError}; +use clap::{App, Arg, ArgMatches}; +use rust_util::{XResult, util_clap::{CommandError, CommandExecutor, DefaultCommand}}; -fn main() -> CommandError { - let commands: Vec> = vec![ - Box::new(cmd_genkey::CommandImpl), - Box::new(cmd_credit_create::CommandImpl), - Box::new(cmd_credit_issue::CommandImpl), - Box::new(cmd_credit_transfer::CommandImpl), - Box::new(cmd_credit_query::CommandImpl), - Box::new(cmd_credit_queryall::CommandImpl), - ]; - let mut app = App::new(env!("CARGO_PKG_NAME")) - .version(env!("CARGO_PKG_VERSION")) - .about(env!("CARGO_PKG_DESCRIPTION")); - app = cmd_default::CommandImpl::process_command(app); - for command in &commands { - app = app.subcommand(command.subcommand()); +pub struct DefaultCommandImpl; +impl DefaultCommand for DefaultCommandImpl { + fn process_command<'a>(&self, app: App<'a, 'a>) -> App<'a, 'a> { + app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info")) } - 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); - } + + fn run(&self, arg_matches: &ArgMatches) -> CommandError { + let verbose_count = arg_matches.occurrences_of("verbose"); + information!("Verbose count: {}", verbose_count); + information!("Try use help (--help) print usage"); + Ok(None) } - cmd_default::CommandImpl::run(&matches) +} + +fn main() -> XResult<()> { + let mut c = CommandExecutor::new(Some(Box::new(DefaultCommandImpl))); + c.add(Box::new(cmd_genkey::CommandImpl)); + c.add(Box::new(cmd_credit_create::CommandImpl)); + c.add(Box::new(cmd_credit_issue::CommandImpl)); + c.add(Box::new(cmd_credit_transfer::CommandImpl)); + c.add(Box::new(cmd_credit_query::CommandImpl)); + c.add(Box::new(cmd_credit_queryall::CommandImpl)); + + c.run()?; + Ok(()) }