Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move cli stuff to its own crate
  • Loading branch information
cecton committed Jan 22, 2021
commit 358c18c67fcd60946cf3a3b515e86f39f68a091c
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"cli",
"consensus",
"message-broker",
"network",
Expand Down
12 changes: 12 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "cumulus-cli"
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
structopt = "0.3.3"

# Substrate dependencies
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" }
104 changes: 104 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Cumulus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use sc_cli;
use std::{
fs,
io::{self, Write},
};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct PurgeChainCmd {
#[structopt(flatten)]
base: sc_cli::PurgeChainCmd,

/// Only delete the para chain database
#[structopt(long = "parachain", aliases = &["para"])]
parachain: bool,

/// Only delete the relay chain database
#[structopt(long = "relaychain", aliases = &["relay"])]
relaychain: bool,
}

impl PurgeChainCmd {
/// Run the purge command
pub fn run(
&self,
para_config: sc_service::Configuration,
relay_config: sc_service::Configuration,
) -> sc_cli::Result<()> {
let databases = match (self.parachain, self.relaychain) {
(true, true) | (false, false) => vec![para_config.database, relay_config.database],
(true, false) => vec![para_config.database],
(false, true) => vec![relay_config.database],
};

let db_paths = databases
.iter()
.map(|x| {
x.path().ok_or_else(|| {
sc_cli::Error::Input("Cannot purge custom database implementation".into())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a hint which on this is, would be nice :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 670b674

})
})
.collect::<sc_cli::Result<Vec<_>>>()?;

if !self.base.yes {
for db_path in &db_paths {
println!("{}", db_path.display());
}
print!("Are you sure to remove? [y/N]: ");
io::stdout().flush().expect("failed to flush stdout");

let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim();

match input.chars().nth(0) {
Some('y') | Some('Y') => {}
_ => {
println!("Aborted");
return Ok(());
}
}
}

for db_path in &db_paths {
match fs::remove_dir_all(&db_path) {
Ok(_) => {
println!("{:?} removed.", &db_path);
}
Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
eprintln!("{:?} did not exist.", &db_path);
}
Err(err) => return Result::Err(err.into()),
}
}

Ok(())
}
}

impl sc_cli::CliConfiguration for PurgeChainCmd {
fn shared_params(&self) -> &sc_cli::SharedParams {
&self.base.shared_params
}

fn database_params(&self) -> Option<&sc_cli::DatabaseParams> {
Some(&self.base.database_params)
}
}
1 change: 1 addition & 0 deletions rococo-parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ sp-offchain = { git = "https://github.com/paritytech/substrate", branch = "maste
jsonrpc-core = "15.1.0"

# Cumulus dependencies
cumulus-cli = { path = "../cli" }
cumulus-consensus = { path = "../consensus" }
cumulus-collator = { path = "../collator" }
cumulus-network = { path = "../network" }
Expand Down
92 changes: 3 additions & 89 deletions rococo-parachains/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
Expand All @@ -16,11 +16,7 @@

use crate::chain_spec;
use sc_cli;
use std::{
fs,
io::{self, Write},
path::PathBuf,
};
use std::path::PathBuf;
use structopt::StructOpt;

/// Sub-commands supported by the collator.
Expand Down Expand Up @@ -50,7 +46,7 @@ pub enum Subcommand {
ImportBlocks(sc_cli::ImportBlocksCmd),

/// Remove the whole chain.
PurgeChain(PurgeChainCmd),
PurgeChain(cumulus_cli::PurgeChainCmd),

/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
Expand Down Expand Up @@ -165,85 +161,3 @@ impl RelayChainCli {
}
}
}

#[derive(Debug, StructOpt)]
pub struct PurgeChainCmd {
#[structopt(flatten)]
base: sc_cli::PurgeChainCmd,

/// Only delete the para chain database
#[structopt(long = "parachain", aliases = &["para"])]
parachain: bool,

/// Only delete the relay chain database
#[structopt(long = "relay-chain", aliases = &["relay", "relaychain"])]
relaychain: bool,
}

impl PurgeChainCmd {
/// Run the purge command
pub fn run(
&self,
para_config: sc_service::Configuration,
relay_config: sc_service::Configuration,
) -> sc_cli::Result<()> {
let databases = match (self.parachain, self.relaychain) {
(true, true) | (false, false) => vec![para_config.database, relay_config.database],
(true, false) => vec![para_config.database],
(false, true) => vec![relay_config.database],
};

let db_paths = databases
.iter()
.map(|x| {
x.path().ok_or_else(|| {
sc_cli::Error::Input("Cannot purge custom database implementation".into())
})
})
.collect::<sc_cli::Result<Vec<_>>>()?;

if !self.base.yes {
for db_path in &db_paths {
println!("{}", db_path.display());
}
print!("Are you sure to remove? [y/N]: ");
io::stdout().flush().expect("failed to flush stdout");

let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim();

match input.chars().nth(0) {
Some('y') | Some('Y') => {}
_ => {
println!("Aborted");
return Ok(());
}
}
}

for db_path in &db_paths {
match fs::remove_dir_all(&db_path) {
Ok(_) => {
println!("{:?} removed.", &db_path);
}
Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
eprintln!("{:?} did not exist.", &db_path);
}
Err(err) => return Result::Err(err.into()),
}
}

Ok(())
}
}

impl sc_cli::CliConfiguration for PurgeChainCmd {
fn shared_params(&self) -> &sc_cli::SharedParams {
&self.base.shared_params
}

fn database_params(&self) -> Option<&sc_cli::DatabaseParams> {
Some(&self.base.database_params)
}
}
2 changes: 1 addition & 1 deletion rococo-parachains/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
Expand Down