Shared params in CLI API (#4466)

* Common shared parames getter

* Expose more types from `service-builder`

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Arkadiy Paronyan
2019-12-20 16:05:01 +01:00
committed by Bastian Köcher
parent bfad5f3ffc
commit 9950ea98fc
6 changed files with 47 additions and 69 deletions
+22 -5
View File
@@ -57,7 +57,7 @@ use params::{
NodeKeyParams, NodeKeyType, Cors, CheckBlockCmd,
};
pub use params::{NoCustom, CoreParams, SharedParams, ImportParams, ExecutionStrategy};
pub use traits::{GetLogFilter, AugmentClap};
pub use traits::{GetSharedParams, AugmentClap};
use app_dirs::{AppInfo, AppDataType};
use log::info;
use lazy_static::lazy_static;
@@ -195,7 +195,7 @@ pub fn parse_and_prepare<'a, CC, RP, I>(
args: I,
) -> ParseAndPrepare<'a, CC, RP>
where
CC: StructOpt + Clone + GetLogFilter,
CC: StructOpt + Clone + GetSharedParams,
RP: StructOpt + Clone + AugmentClap,
I: IntoIterator,
<I as IntoIterator>::Item: Into<std::ffi::OsString> + Clone,
@@ -216,10 +216,9 @@ where
.setting(AppSettings::SubcommandsNegateReqs)
.get_matches_from(args);
let cli_args = CoreParams::<CC, RP>::from_clap(&matches);
init_logger(cli_args.get_log_filter().as_ref().map(|v| v.as_ref()).unwrap_or(""));
fdlimit::raise_fd_limit();
match cli_args {
let args = match cli_args {
params::CoreParams::Run(params) => ParseAndPrepare::Run(
ParseAndPrepareRun { params, impl_name, version }
),
@@ -242,7 +241,9 @@ where
ParseAndPrepareRevert { params, version }
),
params::CoreParams::Custom(params) => ParseAndPrepare::CustomCommand(params),
}
};
init_logger(args.shared_params().and_then(|p| p.log.as_ref()).map(|v| v.as_ref()).unwrap_or(""));
args
}
/// Returns a string displaying the node role, special casing the sentry mode
@@ -277,6 +278,22 @@ pub enum ParseAndPrepare<'a, CC, RP> {
CustomCommand(CC),
}
impl<'a, CC, RP> ParseAndPrepare<'a, CC, RP> where CC: GetSharedParams {
/// Return common set of parameters shared by all commands.
pub fn shared_params(&self) -> Option<&SharedParams> {
match self {
ParseAndPrepare::Run(c) => Some(&c.params.left.shared_params),
ParseAndPrepare::BuildSpec(c) => Some(&c.params.shared_params),
ParseAndPrepare::ExportBlocks(c) => Some(&c.params.shared_params),
ParseAndPrepare::ImportBlocks(c) => Some(&c.params.shared_params),
ParseAndPrepare::CheckBlock(c) => Some(&c.params.shared_params),
ParseAndPrepare::PurgeChain(c) => Some(&c.params.shared_params),
ParseAndPrepare::RevertChain(c) => Some(&c.params.shared_params),
ParseAndPrepare::CustomCommand(c) => c.shared_params(),
}
}
}
/// Command ready to run the main client.
pub struct ParseAndPrepareRun<'a, RP> {
params: MergeParameters<RunCmd, RP>,
+4 -49
View File
@@ -14,24 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use crate::traits::{AugmentClap, GetLogFilter};
use crate::traits::{AugmentClap, GetSharedParams};
use std::{str::FromStr, path::PathBuf};
use structopt::{StructOpt, clap::{arg_enum, App, AppSettings, SubCommand, Arg}};
pub use crate::execution_strategy::ExecutionStrategy;
/// Auxiliary macro to implement `GetLogFilter` for all types that have the `shared_params` field.
macro_rules! impl_get_log_filter {
( $type:ident ) => {
impl $crate::GetLogFilter for $type {
fn get_log_filter(&self) -> Option<String> {
self.shared_params.get_log_filter()
}
}
}
}
impl Into<sc_client_api::ExecutionStrategy> for ExecutionStrategy {
fn into(self) -> sc_client_api::ExecutionStrategy {
match self {
@@ -153,12 +142,6 @@ pub struct ImportParams {
pub state_cache_size: usize,
}
impl GetLogFilter for SharedParams {
fn get_log_filter(&self) -> Option<String> {
self.log.clone()
}
}
/// Parameters used to create the network configuration.
#[derive(Debug, StructOpt, Clone)]
pub struct NetworkConfigurationParams {
@@ -723,7 +706,6 @@ fn parse_cors(s: &str) -> Result<Cors, Box<dyn std::error::Error>> {
}
impl_augment_clap!(RunCmd);
impl_get_log_filter!(RunCmd);
/// The `build-spec` command used to build a specification.
#[derive(Debug, StructOpt, Clone)]
@@ -748,8 +730,6 @@ pub struct BuildSpecCmd {
pub node_key_params: NodeKeyParams,
}
impl_get_log_filter!(BuildSpecCmd);
/// Wrapper type of `String` which holds an arbitary sized unsigned integer formatted as decimal.
#[derive(Debug, Clone)]
pub struct BlockNumber(String);
@@ -813,8 +793,6 @@ pub struct ExportBlocksCmd {
pub shared_params: SharedParams,
}
impl_get_log_filter!(ExportBlocksCmd);
/// The `import-blocks` command used to import blocks.
#[derive(Debug, StructOpt, Clone)]
pub struct ImportBlocksCmd {
@@ -837,8 +815,6 @@ pub struct ImportBlocksCmd {
pub import_params: ImportParams,
}
impl_get_log_filter!(ImportBlocksCmd);
/// The `check-block` command used to validate blocks.
#[derive(Debug, StructOpt, Clone)]
pub struct CheckBlockCmd {
@@ -861,8 +837,6 @@ pub struct CheckBlockCmd {
pub import_params: ImportParams,
}
impl_get_log_filter!(CheckBlockCmd);
/// The `revert` command used revert the chain to a previous state.
#[derive(Debug, StructOpt, Clone)]
pub struct RevertCmd {
@@ -875,8 +849,6 @@ pub struct RevertCmd {
pub shared_params: SharedParams,
}
impl_get_log_filter!(RevertCmd);
/// The `purge-chain` command used to remove the whole chain.
#[derive(Debug, StructOpt, Clone)]
pub struct PurgeChainCmd {
@@ -889,8 +861,6 @@ pub struct PurgeChainCmd {
pub shared_params: SharedParams,
}
impl_get_log_filter!(PurgeChainCmd);
/// All core commands that are provided by default.
///
/// The core commands are split into multiple subcommands and `Run` is the default subcommand. From
@@ -924,7 +894,7 @@ pub enum CoreParams<CC, RP> {
}
impl<CC, RP> StructOpt for CoreParams<CC, RP> where
CC: StructOpt + GetLogFilter,
CC: StructOpt + GetSharedParams,
RP: StructOpt + AugmentClap
{
fn clap<'a, 'b>() -> App<'a, 'b> {
@@ -979,21 +949,6 @@ impl<CC, RP> StructOpt for CoreParams<CC, RP> where
}
}
impl<CC, RP> GetLogFilter for CoreParams<CC, RP> where CC: GetLogFilter {
fn get_log_filter(&self) -> Option<String> {
match self {
CoreParams::Run(c) => c.left.get_log_filter(),
CoreParams::BuildSpec(c) => c.get_log_filter(),
CoreParams::ExportBlocks(c) => c.get_log_filter(),
CoreParams::ImportBlocks(c) => c.get_log_filter(),
CoreParams::CheckBlock(c) => c.get_log_filter(),
CoreParams::PurgeChain(c) => c.get_log_filter(),
CoreParams::Revert(c) => c.get_log_filter(),
CoreParams::Custom(c) => c.get_log_filter(),
}
}
}
/// A special commandline parameter that expands to nothing.
/// Should be used as custom subcommand/run arguments if no custom values are required.
#[derive(Clone, Debug, Default)]
@@ -1015,8 +970,8 @@ impl AugmentClap for NoCustom {
}
}
impl GetLogFilter for NoCustom {
fn get_log_filter(&self) -> Option<String> {
impl GetSharedParams for NoCustom {
fn shared_params(&self) -> Option<&SharedParams> {
None
}
}
+5 -4
View File
@@ -15,6 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use structopt::{StructOpt, clap::App};
use crate::params::SharedParams;
/// Something that can augment a clap app with further parameters.
/// `derive(StructOpt)` is implementing this function by default, so a macro `impl_augment_clap!`
@@ -37,8 +38,8 @@ macro_rules! impl_augment_clap {
}
}
/// Returns the log filter given by the user as commandline argument.
pub trait GetLogFilter {
/// Returns the set log filter.
fn get_log_filter(&self) -> Option<String>;
/// Supports getting common params.
pub trait GetSharedParams {
/// Returns shared params if any.
fn shared_params(&self) -> Option<&SharedParams>;
}