Refactor CLI handling (#1368)

* Rework cli handling

* Update readme

* Adds support for custom subcommands and extra run parameters

* Update readme

* Fixes compilation after master merge

* Make "Run" the default subcommand

Actually its hidden to the outside that is an subcommand.

* Rewrite CLI to work without breaking old CLI behavior

* Some cleanup

* Fix incorrect config setup

* Update README

* Fixes after merge

* Fixes incorrect README
This commit is contained in:
Bastian Köcher
2019-01-25 11:48:46 +01:00
committed by Gav Wood
parent 375e01e6b1
commit 27a882bfac
13 changed files with 814 additions and 575 deletions
+397 -201
View File
@@ -14,230 +14,426 @@
// 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 std::path::PathBuf;
use structopt::StructOpt;
/// CLI Parameters provided by default
#[derive(Debug, StructOpt)]
#[structopt(name = "Substrate")]
pub struct CoreParams {
///Sets a custom logging filter
#[structopt(short = "l", long = "log", value_name = "LOG_PATTERN")]
log: Option<String>,
/// Specify custom keystore path
#[structopt(long = "keystore-path", value_name = "PATH", parse(from_os_str))]
keystore_path: Option<PathBuf>,
/// Specify additional key seed
#[structopt(long = "key", value_name = "STRING")]
key: Option<String>,
/// Specify node secret key (64-character hex string)
#[structopt(long = "node-key", value_name = "KEY")]
node_key: Option<String>,
/// Enable validator mode
#[structopt(long = "validator")]
validator: bool,
/// Run in light client mode
#[structopt(long = "light")]
light: bool,
/// Limit the memory the database cache can use
#[structopt(long = "db-cache", value_name = "MiB")]
database_cache_size: Option<u32>,
/// Listen on this multiaddress
#[structopt(long = "listen-addr", value_name = "LISTEN_ADDR")]
listen_addr: Vec<String>,
/// Specify p2p protocol TCP port. Only used if --listen-addr is not specified.
#[structopt(long = "port", value_name = "PORT")]
port: Option<u32>,
/// Listen to all RPC interfaces (default is local)
#[structopt(long = "rpc-external")]
rpc_external: bool,
/// Listen to all Websocket interfaces (default is local)
#[structopt(long = "ws-external")]
ws_external: bool,
/// Specify HTTP RPC server TCP port
#[structopt(long = "rpc-port", value_name = "PORT")]
rpc_port: Option<u32>,
/// Specify WebSockets RPC server TCP port
#[structopt(long = "ws-port", value_name = "PORT")]
ws_port: Option<u32>,
/// Specify a list of bootnodes
#[structopt(long = "bootnodes", value_name = "URL")]
bootnodes: Vec<String>,
/// Specify a list of reserved node addresses
#[structopt(long = "reserved-nodes", value_name = "URL")]
reserved_nodes: Vec<String>,
/// Specify the number of outgoing connections we're trying to maintain
#[structopt(long = "out-peers", value_name = "OUT_PEERS")]
out_peers: Option<u8>,
/// Specify the maximum number of incoming connections we're accepting
#[structopt(long = "in-peers", value_name = "IN_PEERS")]
in_peers: Option<u8>,
/// Specify the pruning mode, a number of blocks to keep or 'archive'. Default is 256.
#[structopt(long = "pruning", value_name = "PRUNING_MODE")]
pruning: Option<String>,
/// The human-readable name for this node, as reported to the telemetry server, if enabled
#[structopt(long = "name", value_name = "NAME")]
name: Option<String>,
/// Should not connect to the Substrate telemetry server (telemetry is on by default on global chains)
#[structopt(long = "no-telemetry")]
no_telemetry: bool,
/// The URL of the telemetry server to connect to
#[structopt(long = "telemetry-url", value_name = "TELEMETRY_URL")]
telemetry_url: Option<String>,
/// The means of execution used when calling into the runtime. Can be either wasm, native or both.
#[structopt(long = "execution", value_name = "STRATEGY")]
execution: Option<ExecutionStrategy>,
#[allow(missing_docs)]
#[structopt(flatten)]
shared_flags: SharedFlags,
#[structopt(subcommand)]
cmds: Option<CoreCommands>,
}
/// How to execute blocks
#[derive(Debug, StructOpt)]
pub enum ExecutionStrategy {
/// Execute native only
Native,
/// Execute wasm only
Wasm,
/// Execute natively when possible, wasm otherwise
Both,
}
impl Default for ExecutionStrategy {
fn default() -> Self {
ExecutionStrategy::Both
}
}
impl std::str::FromStr for ExecutionStrategy {
type Err = String;
fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"native" => Ok(ExecutionStrategy::Native),
"wasm" | "webassembly" => Ok(ExecutionStrategy::Wasm),
"both" => Ok(ExecutionStrategy::Both),
_ => Err("Please specify either 'native', 'wasm' or 'both".to_owned())
use structopt::{StructOpt, clap::{arg_enum, _clap_count_exprs, App, AppSettings, SubCommand}};
use client;
/// Auxialary 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()
}
}
}
}
/// Flags used by `CoreParams` and almost all `CoreCommands`.
#[derive(Debug, StructOpt)]
pub struct SharedFlags {
arg_enum! {
/// How to execute blocks
#[derive(Debug, Clone)]
pub enum ExecutionStrategy {
Native,
Wasm,
Both,
}
}
impl Into<client::ExecutionStrategy> for ExecutionStrategy {
fn into(self) -> client::ExecutionStrategy {
match self {
ExecutionStrategy::Native => client::ExecutionStrategy::NativeWhenPossible,
ExecutionStrategy::Wasm => client::ExecutionStrategy::AlwaysWasm,
ExecutionStrategy::Both => client::ExecutionStrategy::Both,
}
}
}
/// Shared parameters used by all `CoreParams`.
#[derive(Debug, StructOpt, Clone)]
pub struct SharedParams {
/// Specify the chain specification (one of dev, local or staging)
#[structopt(long = "chain", value_name = "CHAIN_SPEC")]
chain: Option<String>,
pub chain: Option<String>,
/// Specify the development chain
#[structopt(long = "dev")]
dev: bool,
pub dev: bool,
/// Specify custom base path.
#[structopt(long = "base-path", short = "d", value_name = "PATH")]
base_path: Option<String>,
#[structopt(long = "base-path", short = "d", value_name = "PATH", parse(from_os_str))]
pub base_path: Option<PathBuf>,
///Sets a custom logging filter
#[structopt(short = "l", long = "log", value_name = "LOG_PATTERN")]
pub log: Option<String>,
}
/// Subcommands provided by Default
#[derive(Debug, StructOpt)]
pub enum CoreCommands {
/// Build a spec.json file, outputing to stdout
#[structopt(name = "build-spec")]
BuildSpec {
/// Force raw genesis storage output.
#[structopt(long = "raw")]
raw: bool,
},
impl GetLogFilter for SharedParams {
fn get_log_filter(&self) -> Option<String> {
self.log.clone()
}
}
/// Export blocks to a file
#[structopt(name = "export-blocks")]
ExportBlocks {
/// Output file name or stdout if unspecified.
#[structopt(parse(from_os_str))]
output: Option<PathBuf>,
/// Parameters used to create the network configuration.
#[derive(Debug, StructOpt, Clone)]
pub struct NetworkConfigurationParams {
/// Specify a list of bootnodes
#[structopt(long = "bootnodes", value_name = "URL")]
pub bootnodes: Vec<String>,
/// Specify starting block number. 1 by default.
#[structopt(long = "from", value_name = "BLOCK")]
from: Option<u128>,
/// Specify a list of reserved node addresses
#[structopt(long = "reserved-nodes", value_name = "URL")]
pub reserved_nodes: Vec<String>,
/// Specify last block number. Best block by default.
#[structopt(long = "to", value_name = "BLOCK")]
to: Option<u128>,
/// Listen on this multiaddress
#[structopt(long = "listen-addr", value_name = "LISTEN_ADDR")]
pub listen_addr: Vec<String>,
/// Use JSON output rather than binary.
#[structopt(long = "json")]
json: bool,
/// Specify p2p protocol TCP port. Only used if --listen-addr is not specified.
#[structopt(long = "port", value_name = "PORT")]
pub port: Option<u16>,
#[allow(missing_docs)]
#[structopt(flatten)]
shared_flags: SharedFlags,
},
/// Specify node secret key (64-character hex string)
#[structopt(long = "node-key", value_name = "KEY")]
pub node_key: Option<String>,
/// Specify the number of outgoing connections we're trying to maintain
#[structopt(long = "out-peers", value_name = "OUT_PEERS", default_value = "25")]
pub out_peers: u32,
/// Specify the maximum number of incoming connections we're accepting
#[structopt(long = "in-peers", value_name = "IN_PEERS", default_value = "25")]
pub in_peers: u32,
}
/// The `run` command used to run a node.
#[derive(Debug, StructOpt, Clone)]
pub struct RunCmd {
/// Specify custom keystore path
#[structopt(long = "keystore-path", value_name = "PATH", parse(from_os_str))]
pub keystore_path: Option<PathBuf>,
/// Specify additional key seed
#[structopt(long = "key", value_name = "STRING")]
pub key: Option<String>,
/// Enable validator mode
#[structopt(long = "validator")]
pub validator: bool,
/// Run in light client mode
#[structopt(long = "light")]
pub light: bool,
/// Limit the memory the database cache can use
#[structopt(long = "db-cache", value_name = "MiB")]
pub database_cache_size: Option<u32>,
/// Listen to all RPC interfaces (default is local)
#[structopt(long = "rpc-external")]
pub rpc_external: bool,
/// Listen to all Websocket interfaces (default is local)
#[structopt(long = "ws-external")]
pub ws_external: bool,
/// Specify HTTP RPC server TCP port
#[structopt(long = "rpc-port", value_name = "PORT")]
pub rpc_port: Option<u16>,
/// Specify WebSockets RPC server TCP port
#[structopt(long = "ws-port", value_name = "PORT")]
pub ws_port: Option<u16>,
/// Specify the pruning mode, a number of blocks to keep or 'archive'. Default is 256.
#[structopt(long = "pruning", value_name = "PRUNING_MODE")]
pub pruning: Option<String>,
/// The human-readable name for this node, as reported to the telemetry server, if enabled
#[structopt(long = "name", value_name = "NAME")]
pub name: Option<String>,
/// Should not connect to the Substrate telemetry server (telemetry is on by default on global chains)
#[structopt(long = "no-telemetry")]
pub no_telemetry: bool,
/// The URL of the telemetry server to connect to
#[structopt(long = "telemetry-url", value_name = "TELEMETRY_URL")]
pub telemetry_url: Option<String>,
/// The means of execution used when calling into the runtime. Can be either wasm, native or both.
#[structopt(
long = "execution",
value_name = "STRATEGY",
raw(
possible_values = "&ExecutionStrategy::variants()",
case_insensitive = "true",
default_value = r#""Both""#
)
)]
pub execution: ExecutionStrategy,
#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: SharedParams,
#[allow(missing_docs)]
#[structopt(flatten)]
pub network_config: NetworkConfigurationParams,
}
impl_augment_clap!(RunCmd);
impl_get_log_filter!(RunCmd);
/// The `build-spec` command used to build a specification.
#[derive(Debug, StructOpt, Clone)]
pub struct BuildSpecCmd {
/// Force raw genesis storage output.
#[structopt(long = "raw")]
pub raw: bool,
#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: SharedParams,
/// Specify node secret key (64-character hex string)
#[structopt(long = "node-key", value_name = "KEY")]
pub node_key: Option<String>,
}
impl_get_log_filter!(BuildSpecCmd);
/// The `export-blocks` command used to export blocks.
#[derive(Debug, StructOpt, Clone)]
pub struct ExportBlocksCmd {
/// Output file name or stdout if unspecified.
#[structopt(parse(from_os_str))]
pub output: Option<PathBuf>,
/// Specify starting block number. 1 by default.
#[structopt(long = "from", value_name = "BLOCK")]
pub from: Option<u64>,
/// Specify last block number. Best block by default.
#[structopt(long = "to", value_name = "BLOCK")]
pub to: Option<u64>,
/// Use JSON output rather than binary.
#[structopt(long = "json")]
pub json: bool,
#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: SharedParams,
}
impl_get_log_filter!(ExportBlocksCmd);
/// The `import-blocks` command used to import blocks.
#[derive(Debug, StructOpt, Clone)]
pub struct ImportBlocksCmd {
/// Input file or stdin if unspecified.
#[structopt(parse(from_os_str))]
pub input: Option<PathBuf>,
/// The means of execution used when executing blocks. Can be either wasm, native or both.
#[structopt(
long = "execution",
value_name = "STRATEGY",
raw(
possible_values = "&ExecutionStrategy::variants()",
case_insensitive = "true",
default_value = r#""Both""#
)
)]
pub execution: ExecutionStrategy,
/// The means of execution used when calling into the runtime. Can be either wasm, native or both.
#[structopt(
long = "api-execution",
value_name = "STRATEGY",
raw(
possible_values = "&ExecutionStrategy::variants()",
case_insensitive = "true",
default_value = r#""Both""#
)
)]
pub api_execution: ExecutionStrategy,
/// The maximum number of 64KB pages to ever allocate for Wasm execution. Don't alter this unless you know what you're doing.
#[structopt(long = "max-heap-pages", value_name = "COUNT")]
pub max_heap_pages: Option<u32>,
#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: SharedParams,
}
impl_get_log_filter!(ImportBlocksCmd);
/// The `revert` command used revert the chain to a previos state.
#[derive(Debug, StructOpt, Clone)]
pub struct RevertCmd {
/// Number of blocks to revert.
#[structopt(default_value = "256")]
pub num: u64,
#[allow(missing_docs)]
#[structopt(flatten)]
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 {
#[allow(missing_docs)]
#[structopt(flatten)]
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
/// the CLI user perspective, it is not visible that `Run` is a subcommand. So, all parameters of
/// `Run` are exported as main executable parameters.
#[derive(Debug, Clone)]
pub enum CoreParams<CC, RP> {
/// Run a node.
Run(MergeParameters<RunCmd, RP>),
/// Build a spec.json file, outputing to stdout.
BuildSpec(BuildSpecCmd),
/// Export blocks to a file.
ExportBlocks(ExportBlocksCmd),
/// Import blocks from file.
#[structopt(name = "import-blocks")]
ImportBlocks {
/// Input file or stdin if unspecified.
#[structopt(parse(from_os_str))]
input: Option<PathBuf>,
ImportBlocks(ImportBlocksCmd),
/// The means of execution used when executing blocks. Can be either wasm, native or both.
#[structopt(long = "execution", value_name = "STRATEGY")]
execution: ExecutionStrategy,
/// The means of execution used when calling into the runtime. Can be either wasm, native or both.
#[structopt(long = "api-execution", value_name = "STRATEGY")]
api_execution: ExecutionStrategy,
/// The maximum number of 64KB pages to ever allocate for Wasm execution. Don't alter this unless you know what you're doing.
#[structopt(long = "max-heap-pages", value_name = "COUNT")]
max_heap_pages: Option<u32>,
#[allow(missing_docs)]
#[structopt(flatten)]
shared_flags: SharedFlags,
},
///Revert chain to the previous state
#[structopt(name = "revert")]
Revert {
/// Number of blocks to revert. Default is 256.
num: Option<u32>,
#[allow(missing_docs)]
#[structopt(flatten)]
shared_flags: SharedFlags,
},
/// Revert chain to the previous state.
Revert(RevertCmd),
/// Remove the whole chain data.
#[structopt(name = "purge-chain")]
PurgeChain {
#[allow(missing_docs)]
#[structopt(flatten)]
shared_flags: SharedFlags,
},
PurgeChain(PurgeChainCmd),
/// Further custom subcommands.
Custom(CC),
}
impl<CC, RP> StructOpt for CoreParams<CC, RP> where
CC: StructOpt + GetLogFilter,
RP: StructOpt + AugmentClap
{
fn clap<'a, 'b>() -> App<'a, 'b> {
RP::augment_clap(
RunCmd::augment_clap(
CC::clap().unset_setting(AppSettings::SubcommandRequiredElseHelp)
)
).subcommand(
BuildSpecCmd::augment_clap(SubCommand::with_name("build-spec"))
.about("Build a spec.json file, outputing to stdout.")
)
.subcommand(
ExportBlocksCmd::augment_clap(SubCommand::with_name("export-blocks"))
.about("Export blocks to a file.")
)
.subcommand(
ImportBlocksCmd::augment_clap(SubCommand::with_name("import-blocks"))
.about("Import blocks from file.")
)
.subcommand(
RevertCmd::augment_clap(SubCommand::with_name("revert"))
.about("Revert chain to the previous state.")
)
.subcommand(
PurgeChainCmd::augment_clap(SubCommand::with_name("purge-chain"))
.about("Remove the whole chain data.")
)
}
fn from_clap(matches: &::structopt::clap::ArgMatches) -> Self {
match matches.subcommand() {
("build-spec", Some(matches)) =>
CoreParams::BuildSpec(BuildSpecCmd::from_clap(matches)),
("export-blocks", Some(matches)) =>
CoreParams::ExportBlocks(ExportBlocksCmd::from_clap(matches)),
("import-blocks", Some(matches)) =>
CoreParams::ImportBlocks(ImportBlocksCmd::from_clap(matches)),
("revert", Some(matches)) => CoreParams::Revert(RevertCmd::from_clap(matches)),
("purge-chain", Some(matches)) =>
CoreParams::PurgeChain(PurgeChainCmd::from_clap(matches)),
(_, None) => CoreParams::Run(MergeParameters::from_clap(matches)),
_ => CoreParams::Custom(CC::from_clap(matches)),
}
}
}
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::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)]
pub struct NoCustom {}
impl StructOpt for NoCustom {
fn clap<'a, 'b>() -> App<'a, 'b> {
App::new("NoCustom")
}
fn from_clap(_: &::structopt::clap::ArgMatches) -> Self {
NoCustom {}
}
}
impl AugmentClap for NoCustom {
fn augment_clap<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
app
}
}
impl GetLogFilter for NoCustom {
fn get_log_filter(&self) -> Option<String> {
None
}
}
/// Merge all CLI parameters of `L` and `R` into the same level.
#[derive(Clone, Debug)]
pub struct MergeParameters<L, R> {
/// The left side parameters.
pub left: L,
/// The right side parameters.
pub right: R,
}
impl<L, R> StructOpt for MergeParameters<L, R> where L: StructOpt + AugmentClap, R: StructOpt {
fn clap<'a, 'b>() -> App<'a, 'b> {
L::augment_clap(R::clap())
}
fn from_clap(matches: &::structopt::clap::ArgMatches) -> Self {
MergeParameters {
left: L::from_clap(matches),
right: R::from_clap(matches),
}
}
}