From 25450dc87d7a3ff10835d6fda689c82837675663 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Tue, 28 Aug 2018 17:53:28 +0800 Subject: [PATCH] add cli for purge chain (#609) --- substrate/substrate/cli/src/cli.yml | 14 +++++++++++++ substrate/substrate/cli/src/lib.rs | 31 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/substrate/substrate/cli/src/cli.yml b/substrate/substrate/cli/src/cli.yml index e6b8e395d2..0a05404721 100644 --- a/substrate/substrate/cli/src/cli.yml +++ b/substrate/substrate/cli/src/cli.yml @@ -211,3 +211,17 @@ subcommands: value_name: PATH help: Specify custom base path. takes_value: true + - purge-chain: + about: Remove the whole chain data. + args: + - chain: + long: chain + value_name: CHAIN_SPEC + help: Specify the chain specification. + takes_value: true + - base-path: + long: base-path + short: d + value_name: PATH + help: Specify custom base path. + takes_value: true diff --git a/substrate/substrate/cli/src/lib.rs b/substrate/substrate/cli/src/lib.rs index d5fe981b57..ac47a6ff5c 100644 --- a/substrate/substrate/cli/src/lib.rs +++ b/substrate/substrate/cli/src/lib.rs @@ -65,6 +65,7 @@ use network::NonReservedPeerMode; use std::io::{Write, Read, stdin, stdout}; use std::iter; +use std::fs; use std::fs::File; use std::net::{Ipv4Addr, SocketAddr}; use std::path::{Path, PathBuf}; @@ -213,6 +214,12 @@ where return Ok(Action::ExecutedInternally); } + if let Some(matches) = matches.subcommand_matches("purge-chain") { + let spec = load_spec(&matches, spec_factory)?; + purge_chain::(matches, spec)?; + return Ok(Action::ExecutedInternally); + } + let spec = load_spec(&matches, spec_factory)?; let mut config = service::Configuration::default_with_spec(spec); @@ -417,6 +424,30 @@ fn revert_chain(matches: &clap::ArgMatches, spec: ChainSpec Ok(service::chain_ops::revert_chain::(config, As::sa(blocks))?) } +fn purge_chain(matches: &clap::ArgMatches, spec: ChainSpec>) -> error::Result<()> + where F: ServiceFactory, +{ + let base_path = base_path(matches); + let database_path = db_path(&base_path, spec.id()); + + print!("Are you sure to remove {:?}? (y/n)", &database_path); + stdout().flush().expect("failed to flush stdout"); + + let mut input = String::new(); + stdin().read_line(&mut input)?; + let input = input.trim(); + + match input.chars().nth(0) { + Some('y') | Some('Y') => { + fs::remove_dir_all(&database_path)?; + println!("{:?} removed.", &database_path); + }, + _ => println!("Aborted"), + } + + Ok(()) +} + fn parse_address(default: &str, port_param: &str, matches: &clap::ArgMatches) -> Result { let mut address: SocketAddr = default.parse().ok().ok_or_else(|| format!("Invalid address specified for --{}.", port_param))?; if let Some(port) = matches.value_of(port_param) {