From 0aded434360405b6ccb532f928600a9059f3d7a2 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 25 Aug 2018 14:16:44 +0800 Subject: [PATCH] add cli for purge chain --- substrate/cli/src/cli.yml | 14 ++++++++++++++ substrate/cli/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/substrate/cli/src/cli.yml b/substrate/cli/src/cli.yml index e6b8e395d2a36..0a05404721ca1 100644 --- a/substrate/cli/src/cli.yml +++ b/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/cli/src/lib.rs b/substrate/cli/src/lib.rs index f2dee51e454a9..619d50e97890f 100644 --- a/substrate/cli/src/lib.rs +++ b/substrate/cli/src/lib.rs @@ -66,6 +66,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}; @@ -214,6 +215,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); @@ -418,6 +425,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) {