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
+1 -3
View File
@@ -28,7 +28,6 @@ substrate-consensus-aura = { path = "../../core/consensus/aura" }
substrate-finality-grandpa = { path = "../../core/finality-grandpa" }
sr-primitives = { path = "../../core/sr-primitives" }
node-executor = { path = "../executor" }
structopt = "0.2.13"
substrate-keystore = { path = "../../core/keystore" }
[dev-dependencies]
@@ -36,5 +35,4 @@ substrate-service-test = { path = "../../core/service/test" }
[build-dependencies]
substrate-cli = { path = "../../core/cli" }
structopt = "0.2.13"
clap = "~2.32"
structopt = "0.2"
+16 -24
View File
@@ -14,16 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
extern crate clap;
extern crate substrate_cli as cli;
extern crate structopt;
use std::fs;
use std::env;
use clap::Shell;
use std::path::Path;
use cli::{NoCustom, CoreParams};
include!("src/params.rs");
use std::{fs, env, path::Path};
use structopt::{StructOpt, clap::Shell};
fn main() {
build_shell_completion();
@@ -32,30 +30,24 @@ fn main() {
/// Build shell completion scripts for all known shells
/// Full list in https://github.com/kbknapp/clap-rs/blob/e9d0562a1dc5dfe731ed7c767e6cee0af08f0cf9/src/app/parser.rs#L123
fn build_shell_completion() {
let shells = [Shell::Bash, Shell::Fish, Shell::Zsh, Shell::Elvish, Shell::PowerShell];
for shell in shells.iter() {
build_completion(shell);
for shell in &[Shell::Bash, Shell::Fish, Shell::Zsh, Shell::Elvish, Shell::PowerShell] {
build_completion(shell);
}
}
/// Build the shell auto-completion for a given Shell
fn build_completion(shell: &Shell) {
let outdir = match env::var_os("OUT_DIR") {
None => return,
Some(dir) => dir,
};
let path = Path::new(&outdir)
.parent().unwrap()
.parent().unwrap()
.parent().unwrap()
.join("completion-scripts");
None => return,
Some(dir) => dir,
};
let path = Path::new(&outdir)
.parent().unwrap()
.parent().unwrap()
.parent().unwrap()
.join("completion-scripts");
fs::create_dir(&path).ok();
fs::create_dir(&path).ok();
let mut app = Params::clap();
app.gen_completions(
"substrate-node",
*shell,
&path);
CoreParams::<NoCustom, NoCustom>::clap().gen_completions("substrate-node", *shell, &path);
}
+18 -34
View File
@@ -44,19 +44,15 @@ extern crate substrate_inherents as inherents;
#[macro_use]
extern crate log;
extern crate structopt;
pub use cli::error;
pub mod chain_spec;
mod service;
mod params;
use tokio::prelude::Future;
use tokio::runtime::Runtime;
pub use cli::{VersionInfo, IntoExit};
pub use cli::{VersionInfo, IntoExit, NoCustom};
use substrate_service::{ServiceFactory, Roles as ServiceRoles};
use params::{Params as NodeParams};
use structopt::StructOpt;
use std::ops::Deref;
/// The chain specification option.
@@ -107,43 +103,31 @@ pub fn run<I, T, E>(args: I, exit: E, version: cli::VersionInfo) -> error::Resul
T: Into<std::ffi::OsString> + Clone,
E: IntoExit,
{
let full_version = substrate_service::config::full_version_from_strs(
version.version,
version.commit
);
let matches = match NodeParams::clap()
.name(version.executable_name)
.author(version.author)
.about(version.description)
.version(&(full_version + "\n")[..])
.get_matches_from_safe(args) {
Ok(m) => m,
Err(e) => e.exit(),
};
let (spec, config) = cli::parse_matches::<service::Factory, _>(
load_spec, &version, "substrate-node", &matches
)?;
match cli::execute_default::<service::Factory, _>(spec, exit, &matches, &config)? {
cli::Action::ExecutedInternally => (),
cli::Action::RunService(exit) => {
cli::parse_and_execute::<service::Factory, NoCustom, NoCustom, _, _, _, _, _>(
load_spec, &version, "substrate-node", args, exit,
|exit, _custom_args, config| {
info!("{}", version.name);
info!(" version {}", config.full_version());
info!(" by {}, 2017, 2018", version.author);
info!("Chain specification: {}", config.chain_spec.name());
info!("Node name: {}", config.name);
info!("Roles: {:?}", config.roles);
let mut runtime = Runtime::new()?;
let runtime = Runtime::new().map_err(|e| format!("{:?}", e))?;
let executor = runtime.executor();
match config.roles == ServiceRoles::LIGHT {
true => run_until_exit(runtime, service::Factory::new_light(config, executor)?, exit)?,
false => run_until_exit(runtime, service::Factory::new_full(config, executor)?, exit)?,
}
match config.roles {
ServiceRoles::LIGHT => run_until_exit(
runtime,
service::Factory::new_light(config, executor).map_err(|e| format!("{:?}", e))?,
exit
),
_ => run_until_exit(
runtime,
service::Factory::new_full(config, executor).map_err(|e| format!("{:?}", e))?,
exit
),
}.map_err(|e| format!("{:?}", e))
}
}
Ok(())
).map_err(Into::into).map(|_| ())
}
fn run_until_exit<T, C, E>(
-25
View File
@@ -1,25 +0,0 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use structopt::StructOpt;
use cli::CoreParams;
/// Extend params for Node
#[derive(Debug, StructOpt)]
pub struct Params {
#[structopt(flatten)]
core: CoreParams
}