diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 47fa5f3ae7..ecbdd18958 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -1712,6 +1712,7 @@ dependencies = [ name = "node-cli" version = "0.1.0" dependencies = [ + "app_dirs 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "exit-future 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/core/cli/src/lib.rs b/substrate/core/cli/src/lib.rs index b70420f588..6dc1549a2c 100644 --- a/substrate/core/cli/src/lib.rs +++ b/substrate/core/cli/src/lib.rs @@ -79,6 +79,7 @@ use names::{Generator, Name}; use regex::Regex; use structopt::StructOpt; pub use params::{CoreParams, CoreCommands, ExecutionStrategy}; +use app_dirs::{AppInfo, AppDataType}; use futures::Future; @@ -129,10 +130,15 @@ fn load_spec(matches: &clap::ArgMatches, factory: F) -> Result PathBuf { +fn base_path(matches: &clap::ArgMatches, app_info: &AppInfo) -> PathBuf { matches.value_of("base_path") .map(|x| Path::new(x).to_owned()) - .unwrap_or_else(default_base_path) + .unwrap_or_else(|| + app_dirs::get_app_root( + AppDataType::UserData, + app_info, + ).expect("app directories exist on all supported platforms; qed") + ) } fn create_input_err>(msg: T) -> error::Error { @@ -189,7 +195,8 @@ pub fn parse_matches<'a, F, S>( spec_factory: S, version: VersionInfo, impl_name: &'static str, - matches: &clap::ArgMatches<'a> + matches: &clap::ArgMatches<'a>, + app_info: &AppInfo, ) -> error::Result<(ChainSpec<::Genesis>, FactoryFullConfiguration)> where F: ServiceFactory, @@ -219,7 +226,7 @@ where ) } - let base_path = base_path(&matches); + let base_path = base_path(&matches, &app_info); config.keystore_path = matches.value_of("keystore") .map(|x| Path::new(x).to_owned()) @@ -336,7 +343,8 @@ where fn get_db_path_for_subcommand( main_cmd: &clap::ArgMatches, - sub_cmd: &clap::ArgMatches + sub_cmd: &clap::ArgMatches, + app_info: &AppInfo, ) -> error::Result { if main_cmd.is_present("chain") && sub_cmd.is_present("chain") { bail!(create_input_err("`--chain` option is present two times")); @@ -367,9 +375,9 @@ fn get_db_path_for_subcommand( } let base_path = if sub_cmd.is_present("base_path") { - base_path(sub_cmd) + base_path(sub_cmd, app_info) } else { - base_path(main_cmd) + base_path(main_cmd, app_info) }; Ok(db_path(&base_path, &spec_id)) @@ -388,7 +396,8 @@ pub fn execute_default<'a, F, E>( spec: ChainSpec>, exit: E, matches: &clap::ArgMatches<'a>, - config: &FactoryFullConfiguration + config: &FactoryFullConfiguration, + app_info: &AppInfo, ) -> error::Result> where E: IntoExit, @@ -405,7 +414,7 @@ where return Ok(Action::ExecutedInternally); } else if let Some(sub_matches) = matches.subcommand_matches("export-blocks") { export_blocks::( - get_db_path_for_subcommand(matches, sub_matches)?, + get_db_path_for_subcommand(matches, sub_matches, app_info)?, matches, spec, exit.into_exit() @@ -413,7 +422,7 @@ where return Ok(Action::ExecutedInternally); } else if let Some(sub_matches) = matches.subcommand_matches("import-blocks") { import_blocks::( - get_db_path_for_subcommand(matches, sub_matches)?, + get_db_path_for_subcommand(matches, sub_matches, app_info)?, matches, spec, exit.into_exit() @@ -421,13 +430,13 @@ where return Ok(Action::ExecutedInternally); } else if let Some(sub_matches) = matches.subcommand_matches("revert") { revert_chain::( - get_db_path_for_subcommand(matches, sub_matches)?, + get_db_path_for_subcommand(matches, sub_matches, app_info)?, sub_matches, spec )?; return Ok(Action::ExecutedInternally); } else if let Some(sub_matches) = matches.subcommand_matches("purge-chain") { - purge_chain::(get_db_path_for_subcommand(matches, sub_matches)?)?; + purge_chain::(get_db_path_for_subcommand(matches, sub_matches, app_info)?)?; return Ok(Action::ExecutedInternally); } @@ -623,20 +632,6 @@ fn network_path(base_path: &Path, chain_id: &str) -> PathBuf { path } -fn default_base_path() -> PathBuf { - use app_dirs::{AppInfo, AppDataType}; - - let app_info = AppInfo { - name: "Substrate", - author: "Parity Technologies", - }; - - app_dirs::get_app_root( - AppDataType::UserData, - &app_info, - ).expect("app directories exist on all supported platforms; qed") -} - fn init_logger(pattern: &str) { use ansi_term::Colour; diff --git a/substrate/node/cli/Cargo.toml b/substrate/node/cli/Cargo.toml index f0d570a8d2..80828216f0 100644 --- a/substrate/node/cli/Cargo.toml +++ b/substrate/node/cli/Cargo.toml @@ -13,6 +13,7 @@ exit-future = "0.1" substrate-cli = { path = "../../core/cli" } parity-codec = { version = "2.2" } slog = "^2" +app_dirs = "1.2" sr-io = { path = "../../core/sr-io" } substrate-client = { path = "../../core/client" } substrate-primitives = { path = "../../core/primitives" } diff --git a/substrate/node/cli/src/lib.rs b/substrate/node/cli/src/lib.rs index 88059728f8..4eb9a7f243 100644 --- a/substrate/node/cli/src/lib.rs +++ b/substrate/node/cli/src/lib.rs @@ -20,6 +20,7 @@ #![warn(unused_extern_crates)] extern crate tokio; +extern crate app_dirs; extern crate substrate_cli as cli; extern crate substrate_primitives as primitives; @@ -57,6 +58,13 @@ use substrate_service::{ServiceFactory, Roles as ServiceRoles}; use params::{Params as NodeParams}; use structopt::StructOpt; use std::ops::Deref; +use app_dirs::AppInfo; + + +const APP_INFO: AppInfo = AppInfo { + name: "Substrate Node", + author: "Parity Technologies" +}; /// The chain specification option. #[derive(Clone, Debug)] @@ -122,15 +130,15 @@ pub fn run(args: I, exit: E, version: cli::VersionInfo) -> error::Resul }; let (spec, config) = cli::parse_matches::( - load_spec, version, "substrate-node", &matches + load_spec, version, "substrate-node", &matches, &APP_INFO )?; - match cli::execute_default::(spec, exit, &matches, &config)? { + match cli::execute_default::(spec, exit, &matches, &config, &APP_INFO)? { cli::Action::ExecutedInternally => (), cli::Action::RunService(exit) => { - info!("Substrate Node"); + info!("{}", APP_INFO.name); info!(" version {}", config.full_version()); - info!(" by Parity Technologies, 2017, 2018"); + info!(" by {}, 2017, 2018", APP_INFO.author); info!("Chain specification: {}", config.chain_spec.name()); info!("Node name: {}", config.name); info!("Roles: {:?}", config.roles);