Allow implementation to provide AppInfo to figure out default paths (#1468)

This commit is contained in:
Benjamin Kampmann
2019-01-17 16:55:55 +01:00
committed by Bastian Köcher
parent c88b44f6db
commit 15856747ae
4 changed files with 35 additions and 30 deletions
+1
View File
@@ -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)",
+21 -26
View File
@@ -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<F, G>(matches: &clap::ArgMatches, factory: F) -> Result<ChainSpec<G
Ok(spec)
}
fn base_path(matches: &clap::ArgMatches) -> 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<T: Into<String>>(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<<F as service::ServiceFactory>::Genesis>, FactoryFullConfiguration<F>)>
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<PathBuf> {
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<FactoryGenesis<F>>,
exit: E,
matches: &clap::ArgMatches<'a>,
config: &FactoryFullConfiguration<F>
config: &FactoryFullConfiguration<F>,
app_info: &AppInfo,
) -> error::Result<Action<E>>
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::<F, _>(
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::<F, _>(
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::<F>(
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::<F>(get_db_path_for_subcommand(matches, sub_matches)?)?;
purge_chain::<F>(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;
+1
View File
@@ -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" }
+12 -4
View File
@@ -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<I, T, E>(args: I, exit: E, version: cli::VersionInfo) -> error::Resul
};
let (spec, config) = cli::parse_matches::<service::Factory, _>(
load_spec, version, "substrate-node", &matches
load_spec, version, "substrate-node", &matches, &APP_INFO
)?;
match cli::execute_default::<service::Factory, _>(spec, exit, &matches, &config)? {
match cli::execute_default::<service::Factory, _>(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);