From 5cdc6cedd9503ab3e51cea953055d35a68ef7852 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Mon, 16 Aug 2021 13:52:27 +0300 Subject: [PATCH] Refactor the cli modules structure --- crates/humanode-peer/src/cli/mod.rs | 11 +++ crates/humanode-peer/src/cli/root.rs | 74 +++++++++++++++ .../src/{command.rs => cli/run.rs} | 90 ++++--------------- crates/humanode-peer/src/{ => cli}/runner.rs | 2 +- .../src/{cli.rs => cli/subcommand/mod.rs} | 19 +--- crates/humanode-peer/src/main.rs | 4 +- 6 files changed, 108 insertions(+), 92 deletions(-) create mode 100644 crates/humanode-peer/src/cli/mod.rs create mode 100644 crates/humanode-peer/src/cli/root.rs rename crates/humanode-peer/src/{command.rs => cli/run.rs} (59%) rename crates/humanode-peer/src/{ => cli}/runner.rs (98%) rename crates/humanode-peer/src/{cli.rs => cli/subcommand/mod.rs} (72%) diff --git a/crates/humanode-peer/src/cli/mod.rs b/crates/humanode-peer/src/cli/mod.rs new file mode 100644 index 000000000..bd86f8aa0 --- /dev/null +++ b/crates/humanode-peer/src/cli/mod.rs @@ -0,0 +1,11 @@ +//! Command line interface. + +mod root; +mod run; +mod runner; +mod subcommand; + +pub use root::*; +pub use run::*; +pub use runner::*; +pub use subcommand::*; diff --git a/crates/humanode-peer/src/cli/root.rs b/crates/humanode-peer/src/cli/root.rs new file mode 100644 index 000000000..151c6a327 --- /dev/null +++ b/crates/humanode-peer/src/cli/root.rs @@ -0,0 +1,74 @@ +//! Commands hierarchy root. + +use sc_cli::{ChainSpec, CliConfiguration, RuntimeVersion, SubstrateCli}; +use structopt::StructOpt; + +use crate::chain_spec; + +use super::{runner::Runner, subcommand::Subcommand}; + +/// The root of the CLI commands hierarchy. +#[derive(Debug, StructOpt)] +pub struct Root { + /// Additional subcommands. + #[structopt(subcommand)] + pub subcommand: Option, + + /// The `run` command used to run a node. + #[structopt(flatten)] + pub run: sc_cli::RunCmd, +} + +impl SubstrateCli for Root { + fn impl_name() -> String { + "Humanode Node".into() + } + + fn impl_version() -> String { + "0".to_owned() + } + + fn description() -> String { + "Biologically verified human-nodes as a basis for a fair financial system.".into() + } + + fn author() -> String { + env!("CARGO_PKG_AUTHORS").into() + } + + fn support_url() -> String { + "https://github.com/humanode-network/humanode/issues/new".into() + } + + fn copyright_start_year() -> i32 { + 2021 + } + + fn load_spec(&self, id: &str) -> std::result::Result, String> { + if id != "local" && !id.is_empty() { + return Err(format!( + "chain {:?} is not supported, only {:?} is currently available", + id, "local" + )); + } + + Ok(Box::new(chain_spec::local_testnet_config()?)) + } + + fn native_runtime_version(_chain_spec: &Box) -> &'static RuntimeVersion { + &humanode_runtime::VERSION + } +} + +impl Root { + /// Create a [`Runner`] for the command provided in argument. + /// This will create a [`Configuration`] from the command line arguments and the rest of + /// the environemnt. + pub fn create_humanode_runner( + &self, + command: &T, + ) -> sc_cli::Result> { + command.init::()?; + Runner::new(self, command) + } +} diff --git a/crates/humanode-peer/src/command.rs b/crates/humanode-peer/src/cli/run.rs similarity index 59% rename from crates/humanode-peer/src/command.rs rename to crates/humanode-peer/src/cli/run.rs index 4cb7e62a2..7d524c6d9 100644 --- a/crates/humanode-peer/src/command.rs +++ b/crates/humanode-peer/src/cli/run.rs @@ -1,80 +1,24 @@ -//! Command line arguments processing. - -use crate::{ - chain_spec, - cli::{Cli, Subcommand}, - runner, service, -}; +//! The main entrypoint. use humanode_runtime::Block; -use sc_cli::{ChainSpec, CliConfiguration, RuntimeVersion, SubstrateCli}; use sc_service::PartialComponents; -impl SubstrateCli for Cli { - fn impl_name() -> String { - "Humanode Node".into() - } - - fn impl_version() -> String { - "0".to_owned() - } - - fn description() -> String { - "Biologically verified human-nodes as a basis for a fair financial system.".into() - } +use crate::service; - fn author() -> String { - env!("CARGO_PKG_AUTHORS").into() - } - - fn support_url() -> String { - "https://github.com/humanode-network/humanode/issues/new".into() - } - - fn copyright_start_year() -> i32 { - 2021 - } - - fn load_spec(&self, id: &str) -> std::result::Result, String> { - if id != "local" && !id.is_empty() { - return Err(format!( - "chain {:?} is not supported, only {:?} is currently available", - id, "local" - )); - } - - Ok(Box::new(chain_spec::local_testnet_config()?)) - } - - fn native_runtime_version(_chain_spec: &Box) -> &'static RuntimeVersion { - &humanode_runtime::VERSION - } -} - -impl Cli { - /// Create a runner for the command provided in argument. This will create a Configuration and - /// a tokio runtime - fn create_humanode_runner( - &self, - command: &T, - ) -> sc_cli::Result> { - command.init::()?; - runner::Runner::new(self, command) - } -} +use super::{Root, Subcommand}; -/// Parse and run command line arguments +/// Parse command line arguments and run the requested operation. pub async fn run() -> sc_cli::Result<()> { - let cli = Cli::from_args(); + let root: Root = sc_cli::SubstrateCli::from_args(); - match &cli.subcommand { - Some(Subcommand::Key(cmd)) => cmd.run(&cli), + match &root.subcommand { + Some(Subcommand::Key(cmd)) => cmd.run(&root), Some(Subcommand::BuildSpec(cmd)) => { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner.sync_run(|config| cmd.run(config.chain_spec, config.network)) } Some(Subcommand::CheckBlock(cmd)) => { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner .async_run(|config| async move { let PartialComponents { @@ -88,7 +32,7 @@ pub async fn run() -> sc_cli::Result<()> { .await } Some(Subcommand::ExportBlocks(cmd)) => { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner .async_run(|config| async move { let PartialComponents { @@ -101,7 +45,7 @@ pub async fn run() -> sc_cli::Result<()> { .await } Some(Subcommand::ExportState(cmd)) => { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner .async_run(|config| async move { let PartialComponents { @@ -114,7 +58,7 @@ pub async fn run() -> sc_cli::Result<()> { .await } Some(Subcommand::ImportBlocks(cmd)) => { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner .async_run(|config| async move { let PartialComponents { @@ -128,11 +72,11 @@ pub async fn run() -> sc_cli::Result<()> { .await } Some(Subcommand::PurgeChain(cmd)) => { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner.sync_run(|config| cmd.run(config.database)) } Some(Subcommand::Revert(cmd)) => { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner .async_run(|config| async move { let PartialComponents { @@ -147,7 +91,7 @@ pub async fn run() -> sc_cli::Result<()> { } Some(Subcommand::Benchmark(cmd)) => { if cfg!(feature = "runtime-benchmarks") { - let runner = cli.create_humanode_runner(cmd)?; + let runner = root.create_humanode_runner(cmd)?; runner.sync_run(|config| cmd.run::(config)) } else { Err( @@ -158,8 +102,8 @@ pub async fn run() -> sc_cli::Result<()> { } } None => { - let runner = cli.create_humanode_runner(&cli.run)?; - sc_cli::print_node_infos::(runner.config()); + let runner = root.create_humanode_runner(&root.run)?; + sc_cli::print_node_infos::(runner.config()); runner .run_node(|config| async move { service::new_full(config) diff --git a/crates/humanode-peer/src/runner.rs b/crates/humanode-peer/src/cli/runner.rs similarity index 98% rename from crates/humanode-peer/src/runner.rs rename to crates/humanode-peer/src/cli/runner.rs index 8a89be869..6e96d49ba 100644 --- a/crates/humanode-peer/src/runner.rs +++ b/crates/humanode-peer/src/cli/runner.rs @@ -103,7 +103,7 @@ impl Runner { runner(self.config) } - /// Get an immutable reference to the node Configuration + /// Get an immutable reference to the node [`Configuration`]. pub fn config(&self) -> &Configuration { &self.config } diff --git a/crates/humanode-peer/src/cli.rs b/crates/humanode-peer/src/cli/subcommand/mod.rs similarity index 72% rename from crates/humanode-peer/src/cli.rs rename to crates/humanode-peer/src/cli/subcommand/mod.rs index 75b5b4e9f..0a5da8e98 100644 --- a/crates/humanode-peer/src/cli.rs +++ b/crates/humanode-peer/src/cli/subcommand/mod.rs @@ -1,21 +1,10 @@ -//! Substrate-based Cli definition. +//! Humanode peer subcommands. +//! The `substrate` built-in commands are embedded as-is, additional commands are introduced as +//! nested `mod`s in this `mod`. -use sc_cli::RunCmd; use structopt::StructOpt; -/// A cli struct. -#[derive(Debug, StructOpt)] -pub struct Cli { - /// Additional subcommands. - #[structopt(subcommand)] - pub subcommand: Option, - - /// The `run` command used to run a node. - #[structopt(flatten)] - pub run: RunCmd, -} - -/// Subcommand enum. +/// Humanode peer subcommands. #[derive(Debug, StructOpt)] pub enum Subcommand { /// Key management cli utilities diff --git a/crates/humanode-peer/src/main.rs b/crates/humanode-peer/src/main.rs index 4e4ac6f46..43687127b 100644 --- a/crates/humanode-peer/src/main.rs +++ b/crates/humanode-peer/src/main.rs @@ -8,13 +8,11 @@ mod chain_spec; mod cli; -mod command; mod qrcode; -mod runner; mod service; mod validator_key; #[tokio::main] async fn main() -> sc_cli::Result<()> { - command::run().await + cli::run().await }