From 93c551e09ae0d79e5d1e3a03f3882c3ddc883da0 Mon Sep 17 00:00:00 2001 From: Lycrus Hamster Date: Tue, 15 Sep 2020 18:54:59 +0800 Subject: [PATCH] fix(cli): expose version, author and app_name to be customized (#456) --- core/cli/src/lib.rs | 33 ++++++++++++++++++------ core/cli/src/tests/mod.rs | 54 ++++++++++++++++++++++++++++++++++----- examples/muta-chain.rs | 8 +++++- src/lib.rs | 5 +++- 4 files changed, 84 insertions(+), 16 deletions(-) diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index 53e73b57b..deef5e69f 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -38,15 +38,27 @@ impl<'a, Mapping> Cli<'a, Mapping> where Mapping: 'static + ServiceMapping, { - pub fn run(service_mapping: Mapping, target_commands: Option>) { - let cli = Self::new(service_mapping, target_commands); + pub fn run( + service_mapping: Mapping, + app_name: &str, + version: &str, + author: &str, + target_commands: Option>, + ) { + let cli = Self::new(service_mapping, app_name, version, author, target_commands); if let Err(e) = cli.start() { log::error!("{:?}", e) } } - pub fn new(service_mapping: Mapping, target_commands: Option>) -> Self { - let matches = Self::generate_matches(target_commands); + pub fn new( + service_mapping: Mapping, + app_name: &str, + version: &str, + author: &str, + target_commands: Option>, + ) -> Self { + let matches = Self::generate_matches(app_name, version, author, target_commands); let config_path = matches.value_of("config").expect("missing config path"); @@ -136,10 +148,15 @@ where } } - pub fn generate_matches(cmds: Option>) -> ArgMatches<'a> { - let app = clap::App::new("muta-chain") - .version("v0.2.0-rc.2.1") - .author("Muta Dev ") + pub fn generate_matches( + app_name: &str, + version: &str, + author: &str, + cmds: Option>, + ) -> ArgMatches<'a> { + let app = clap::App::new(app_name) + .version(version) + .author(author) .arg( clap::Arg::with_name("config") .short("c") diff --git a/core/cli/src/tests/mod.rs b/core/cli/src/tests/mod.rs index 712dd7f25..8341a4590 100644 --- a/core/cli/src/tests/mod.rs +++ b/core/cli/src/tests/mod.rs @@ -95,7 +95,14 @@ fn block_get() -> Block { "11", ]; - let maintenance_cli = Cli::new(DefaultServiceMapping {}, Some(cmd)).generate_maintenance_cli(); + let maintenance_cli = Cli::new( + DefaultServiceMapping {}, + "Rodents", + "Big Cheek", + "Hamsters", + Some(cmd), + ) + .generate_maintenance_cli(); let block = if let ("block", Some(sub_cmd)) = maintenance_cli.matches.subcommand() { let mut rt = tokio::runtime::Runtime::new().expect("new tokio runtime"); @@ -134,7 +141,14 @@ fn block_set() { "#, ]; - let maintenance_cli = Cli::new(DefaultServiceMapping {}, Some(cmd)).generate_maintenance_cli(); + let maintenance_cli = Cli::new( + DefaultServiceMapping {}, + "Rodents", + "Big Cheek", + "Hamsters", + Some(cmd), + ) + .generate_maintenance_cli(); let mut rt = tokio::runtime::Runtime::new().expect("new tokio runtime"); rt.block_on(async move { @@ -171,7 +185,14 @@ fn latest_get(expect: u64) -> Block { "get", ]; - let maintenance_cli = Cli::new(DefaultServiceMapping {}, Some(cmd)).generate_maintenance_cli(); + let maintenance_cli = Cli::new( + DefaultServiceMapping {}, + "Rodents", + "Big Cheek", + "Hamsters", + Some(cmd), + ) + .generate_maintenance_cli(); let block = if let ("latest_block", Some(sub_cmd)) = maintenance_cli.matches.subcommand() { if let ("get", Some(_cmd)) = sub_cmd.subcommand() { @@ -206,7 +227,14 @@ fn latest_set() { "10", ]; - let maintenance_cli = Cli::new(DefaultServiceMapping {}, Some(cmd)).generate_maintenance_cli(); + let maintenance_cli = Cli::new( + DefaultServiceMapping {}, + "Rodents", + "Big Cheek", + "Hamsters", + Some(cmd), + ) + .generate_maintenance_cli(); if let ("latest_block", Some(sub_cmd)) = maintenance_cli.matches.subcommand() { if let ("set", Some(_cmd)) = sub_cmd.subcommand() { @@ -245,7 +273,14 @@ fn prepare() { "get", ]; - let maintenance_cli = Cli::new(DefaultServiceMapping {}, Some(cmd)).generate_maintenance_cli(); + let maintenance_cli = Cli::new( + DefaultServiceMapping {}, + "Rodents", + "Big Cheek", + "Hamsters", + Some(cmd), + ) + .generate_maintenance_cli(); let storage = maintenance_cli.storage; @@ -321,5 +356,12 @@ fn clean() { } fn run(cmd: Vec<&str>) -> ProtocolResult<()> { - Cli::new(service_mapping::DefaultServiceMapping {}, Some(cmd)).start() + Cli::new( + service_mapping::DefaultServiceMapping {}, + "Rodents", + "Big Cheek", + "Hamsters", + Some(cmd), + ) + .start() } diff --git a/examples/muta-chain.rs b/examples/muta-chain.rs index 64689619f..8a0e45d8f 100644 --- a/examples/muta-chain.rs +++ b/examples/muta-chain.rs @@ -52,7 +52,13 @@ impl ServiceMapping for DefaultServiceMapping { } pub fn main() { - muta::run(DefaultServiceMapping, None) + muta::run( + DefaultServiceMapping, + "muta-chain", + "v0.2.0-rc.2.1", + "Muta Dev ", + None, + ) } #[derive(Debug, Display, From)] diff --git a/src/lib.rs b/src/lib.rs index 2a910960c..882980dbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,10 @@ use cli::Cli; pub fn run( service_mapping: Mapping, + app_name: &str, + version: &str, + author: &str, target_commands: Option>, ) { - Cli::run(service_mapping, target_commands) + Cli::run(service_mapping, app_name, version, author, target_commands) }