|
| 1 | +// Copyright 2021 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Cumulus. |
| 3 | + |
| 4 | +// Cumulus is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Cumulus is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Cumulus. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Cumulus CLI library. |
| 18 | +
|
| 19 | +#![warn(missing_docs)] |
| 20 | + |
| 21 | +use sc_cli; |
| 22 | +use std::{ |
| 23 | + fs, |
| 24 | + io::{self, Write}, |
| 25 | +}; |
| 26 | +use structopt::StructOpt; |
| 27 | + |
| 28 | +/// The `purge-chain` command used to remove the whole chain: the parachain and the relaychain. |
| 29 | +#[derive(Debug, StructOpt)] |
| 30 | +pub struct PurgeChainCmd { |
| 31 | + /// The base struct of the purge-chain command. |
| 32 | + #[structopt(flatten)] |
| 33 | + pub base: sc_cli::PurgeChainCmd, |
| 34 | + |
| 35 | + /// Only delete the para chain database |
| 36 | + #[structopt(long, aliases = &["para"])] |
| 37 | + pub parachain: bool, |
| 38 | + |
| 39 | + /// Only delete the relay chain database |
| 40 | + #[structopt(long, aliases = &["relay"])] |
| 41 | + pub relaychain: bool, |
| 42 | +} |
| 43 | + |
| 44 | +impl PurgeChainCmd { |
| 45 | + /// Run the purge command |
| 46 | + pub fn run( |
| 47 | + &self, |
| 48 | + para_config: sc_service::Configuration, |
| 49 | + relay_config: sc_service::Configuration, |
| 50 | + ) -> sc_cli::Result<()> { |
| 51 | + let databases = match (self.parachain, self.relaychain) { |
| 52 | + (true, true) | (false, false) => vec![ |
| 53 | + ("parachain", para_config.database), |
| 54 | + ("relaychain", relay_config.database), |
| 55 | + ], |
| 56 | + (true, false) => vec![("parachain", para_config.database)], |
| 57 | + (false, true) => vec![("relaychain", relay_config.database)], |
| 58 | + }; |
| 59 | + |
| 60 | + let db_paths = databases |
| 61 | + .iter() |
| 62 | + .map(|(chain_label, database)| { |
| 63 | + database.path().ok_or_else(|| sc_cli::Error::Input(format!( |
| 64 | + "Cannot purge custom database implementation of: {}", |
| 65 | + chain_label, |
| 66 | + ))) |
| 67 | + }) |
| 68 | + .collect::<sc_cli::Result<Vec<_>>>()?; |
| 69 | + |
| 70 | + if !self.base.yes { |
| 71 | + for db_path in &db_paths { |
| 72 | + println!("{}", db_path.display()); |
| 73 | + } |
| 74 | + print!("Are you sure to remove? [y/N]: "); |
| 75 | + io::stdout().flush().expect("failed to flush stdout"); |
| 76 | + |
| 77 | + let mut input = String::new(); |
| 78 | + io::stdin().read_line(&mut input)?; |
| 79 | + let input = input.trim(); |
| 80 | + |
| 81 | + match input.chars().nth(0) { |
| 82 | + Some('y') | Some('Y') => {} |
| 83 | + _ => { |
| 84 | + println!("Aborted"); |
| 85 | + return Ok(()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + for db_path in &db_paths { |
| 91 | + match fs::remove_dir_all(&db_path) { |
| 92 | + Ok(_) => { |
| 93 | + println!("{:?} removed.", &db_path); |
| 94 | + } |
| 95 | + Err(ref err) if err.kind() == io::ErrorKind::NotFound => { |
| 96 | + eprintln!("{:?} did not exist.", &db_path); |
| 97 | + } |
| 98 | + Err(err) => return Err(err.into()), |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl sc_cli::CliConfiguration for PurgeChainCmd { |
| 107 | + fn shared_params(&self) -> &sc_cli::SharedParams { |
| 108 | + &self.base.shared_params |
| 109 | + } |
| 110 | + |
| 111 | + fn database_params(&self) -> Option<&sc_cli::DatabaseParams> { |
| 112 | + Some(&self.base.database_params) |
| 113 | + } |
| 114 | +} |
0 commit comments