Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
fix(cli): expose version, author and app_name to be customized (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
LycrusHamster authored Sep 15, 2020
1 parent ffb1b45 commit 93c551e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
33 changes: 25 additions & 8 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,27 @@ impl<'a, Mapping> Cli<'a, Mapping>
where
Mapping: 'static + ServiceMapping,
{
pub fn run(service_mapping: Mapping, target_commands: Option<Vec<&str>>) {
let cli = Self::new(service_mapping, target_commands);
pub fn run(
service_mapping: Mapping,
app_name: &str,
version: &str,
author: &str,
target_commands: Option<Vec<&str>>,
) {
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<Vec<&str>>) -> Self {
let matches = Self::generate_matches(target_commands);
pub fn new(
service_mapping: Mapping,
app_name: &str,
version: &str,
author: &str,
target_commands: Option<Vec<&str>>,
) -> Self {
let matches = Self::generate_matches(app_name, version, author, target_commands);

let config_path = matches.value_of("config").expect("missing config path");

Expand Down Expand Up @@ -136,10 +148,15 @@ where
}
}

pub fn generate_matches(cmds: Option<Vec<&str>>) -> ArgMatches<'a> {
let app = clap::App::new("muta-chain")
.version("v0.2.0-rc.2.1")
.author("Muta Dev <[email protected]>")
pub fn generate_matches(
app_name: &str,
version: &str,
author: &str,
cmds: Option<Vec<&str>>,
) -> ArgMatches<'a> {
let app = clap::App::new(app_name)
.version(version)
.author(author)
.arg(
clap::Arg::with_name("config")
.short("c")
Expand Down
54 changes: 48 additions & 6 deletions core/cli/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()
}
8 changes: 7 additions & 1 deletion examples/muta-chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
None,
)
}

#[derive(Debug, Display, From)]
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use cli::Cli;

pub fn run<Mapping: 'static + ServiceMapping>(
service_mapping: Mapping,
app_name: &str,
version: &str,
author: &str,
target_commands: Option<Vec<&str>>,
) {
Cli::run(service_mapping, target_commands)
Cli::run(service_mapping, app_name, version, author, target_commands)
}

0 comments on commit 93c551e

Please sign in to comment.