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

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

View File

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

View File

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

View File

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

View File

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

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::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)
}
}

View File

@@ -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<Box<dyn Command>> = 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(())
}