feat: use rust_util use_clap

This commit is contained in:
2021-01-03 10:58:29 +08:00
parent 8b70dfa145
commit 740d5baf65
11 changed files with 44 additions and 71 deletions

View File

@@ -425,10 +425,11 @@ dependencies = [
[[package]] [[package]]
name = "rust_util" name = "rust_util"
version = "0.6.25" version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26a080a375b53e1a584388ac4c611f55e41629c79b40ed1c630d497b995c8cf8" checksum = "3cf5c22991d8fa9a12da6e745e9ea6180df21ed48a8e28431c64fa5a2985d5d9"
dependencies = [ dependencies = [
"clap",
"lazy_static", "lazy_static",
"libc", "libc",
"term", "term",
@@ -528,9 +529,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.56" version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9802ddde94170d186eeee5005b798d9c159fa970403f1be19976d0cfb939b72" checksum = "4211ce9909eb971f111059df92c45640aad50a619cf55cd76476be803c4c68e6"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@@ -16,5 +16,5 @@ ripemd160 = "0.8.0"
sha2 = "0.8.1" sha2 = "0.8.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
rust_util = "0.6" rust_util = { version = "0.6", features = ["use_clap"] }
clap = "2.33" clap = "2.33"

View File

@@ -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;
}

View File

@@ -1,6 +1,6 @@
use std::fs; use std::fs;
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use crate::cmd::{Command, CommandError}; use rust_util::util_clap::{Command, CommandError};
use crate::util::JsonKeyPair; use crate::util::JsonKeyPair;
use crate::tx::{Transaction, TransactionBody}; use crate::tx::{Transaction, TransactionBody};
use crate::engine::ContractEngine; use crate::engine::ContractEngine;
@@ -49,6 +49,6 @@ impl Command for CommandImpl {
let engine = ContractEngine::new(); let engine = ContractEngine::new();
engine.execute(&tx)?; engine.execute(&tx)?;
Ok(()) Ok(None)
} }
} }

View File

@@ -1,6 +1,6 @@
use std::fs; use std::fs;
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use crate::cmd::{Command, CommandError}; use rust_util::util_clap::{Command, CommandError};
use crate::util::JsonKeyPair; use crate::util::JsonKeyPair;
use crate::tx::{Transaction, TransactionBody}; use crate::tx::{Transaction, TransactionBody};
use crate::engine::ContractEngine; use crate::engine::ContractEngine;
@@ -54,6 +54,6 @@ impl Command for CommandImpl {
let engine = ContractEngine::new(); let engine = ContractEngine::new();
engine.execute(&tx)?; engine.execute(&tx)?;
Ok(()) Ok(None)
} }
} }

View File

@@ -1,6 +1,6 @@
use std::fs; use std::fs;
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use crate::cmd::{Command, CommandError}; use rust_util::util_clap::{Command, CommandError};
use crate::util::JsonKeyPair; use crate::util::JsonKeyPair;
use crate::tx::{Transaction, TransactionBody}; use crate::tx::{Transaction, TransactionBody};
use crate::engine::ContractEngine; use crate::engine::ContractEngine;
@@ -49,6 +49,6 @@ impl Command for CommandImpl {
let engine = ContractEngine::new(); let engine = ContractEngine::new();
engine.execute(&tx)?; engine.execute(&tx)?;
Ok(()) Ok(None)
} }
} }

View File

@@ -1,6 +1,6 @@
use std::fs; use std::fs;
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use crate::cmd::{Command, CommandError}; use rust_util::util_clap::{Command, CommandError};
use crate::util::JsonKeyPair; use crate::util::JsonKeyPair;
use crate::tx::{Transaction, TransactionBody}; use crate::tx::{Transaction, TransactionBody};
use crate::engine::ContractEngine; use crate::engine::ContractEngine;
@@ -44,6 +44,6 @@ impl Command for CommandImpl {
let engine = ContractEngine::new(); let engine = ContractEngine::new();
engine.execute(&tx)?; engine.execute(&tx)?;
Ok(()) Ok(None)
} }
} }

View File

@@ -1,6 +1,6 @@
use std::fs; use std::fs;
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use crate::cmd::{Command, CommandError}; use rust_util::util_clap::{Command, CommandError};
use crate::util::JsonKeyPair; use crate::util::JsonKeyPair;
use crate::tx::{Transaction, TransactionBody}; use crate::tx::{Transaction, TransactionBody};
use crate::engine::ContractEngine; use crate::engine::ContractEngine;
@@ -54,6 +54,6 @@ impl Command for CommandImpl {
let engine = ContractEngine::new(); let engine = ContractEngine::new();
engine.execute(&tx)?; engine.execute(&tx)?;
Ok(()) Ok(None)
} }
} }

View File

@@ -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(())
}
}

View File

@@ -1,7 +1,7 @@
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use clap::{App, Arg, ArgMatches, SubCommand}; 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}; use crate::util::{JsonKeyPair, make_key_pair};
pub struct CommandImpl; pub struct CommandImpl;
@@ -21,12 +21,12 @@ impl Command for CommandImpl {
let tag = sub_arg_matches.value_of("tag"); let tag = sub_arg_matches.value_of("tag");
if let Ok(_) = File::open(output) { if let Ok(_) = File::open(output) {
failure!("Output file exists: {}", output); failure!("Output file exists: {}", output);
return Ok(()); return Ok(None);
} }
let (pri_key, pub_key) = make_key_pair(); let (pri_key, pub_key) = make_key_pair();
let json_key_pair = JsonKeyPair::from(pri_key, pub_key, tag.map(|t| t.into())); let json_key_pair = JsonKeyPair::from(pri_key, pub_key, tag.map(|t| t.into()));
success!("Write gen key file: {}", output); success!("Write gen key file: {}", output);
fs::write(output, json_key_pair.to_json()?.as_bytes())?; fs::write(output, json_key_pair.to_json()?.as_bytes())?;
Ok(()) Ok(None)
} }
} }

View File

@@ -7,8 +7,6 @@ mod credit_util;
mod engine; mod engine;
mod engine_plugin_credit; mod engine_plugin_credit;
mod cmd;
mod cmd_default;
mod cmd_genkey; mod cmd_genkey;
mod cmd_credit_create; mod cmd_credit_create;
mod cmd_credit_issue; mod cmd_credit_issue;
@@ -16,30 +14,32 @@ mod cmd_credit_transfer;
mod cmd_credit_query; mod cmd_credit_query;
mod cmd_credit_queryall; mod cmd_credit_queryall;
use clap::App; use clap::{App, Arg, ArgMatches};
use cmd::{Command, CommandError}; use rust_util::{XResult, util_clap::{CommandError, CommandExecutor, DefaultCommand}};
fn main() -> CommandError { pub struct DefaultCommandImpl;
let commands: Vec<Box<dyn Command>> = vec![ impl DefaultCommand for DefaultCommandImpl {
Box::new(cmd_genkey::CommandImpl), fn process_command<'a>(&self, app: App<'a, 'a>) -> App<'a, 'a> {
Box::new(cmd_credit_create::CommandImpl), app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info"))
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());
} }
let matches = app.get_matches();
for command in &commands { fn run(&self, arg_matches: &ArgMatches) -> CommandError {
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) { let verbose_count = arg_matches.occurrences_of("verbose");
return command.run(&matches, sub_cmd_matches); 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(())
} }