Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Move subcommands from sc-cli to nodes #6948

Merged
1 commit merged into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 25 additions & 1 deletion bin/node-template/node/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use structopt::StructOpt;
use sc_cli::{RunCmd, Subcommand};
use sc_cli::RunCmd;

#[derive(Debug, StructOpt)]
pub struct Cli {
Expand All @@ -9,3 +9,27 @@ pub struct Cli {
#[structopt(flatten)]
pub run: RunCmd,
}

#[derive(Debug, StructOpt)]
pub enum Subcommand {
/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),

/// Validate blocks.
CheckBlock(sc_cli::CheckBlockCmd),

/// Export blocks.
ExportBlocks(sc_cli::ExportBlocksCmd),

/// Export the state of a given block into a chain spec.
ExportState(sc_cli::ExportStateCmd),

/// Import blocks.
ImportBlocks(sc_cli::ImportBlocksCmd),

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

/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
}
56 changes: 48 additions & 8 deletions bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.

use crate::chain_spec;
use crate::cli::Cli;
use crate::cli::{Cli, Subcommand};
use crate::service;
use sc_cli::{SubstrateCli, RuntimeVersion, Role, ChainSpec};
use sc_service::PartialComponents;
Expand Down Expand Up @@ -66,15 +66,55 @@ impl SubstrateCli for Cli {
pub fn run() -> sc_cli::Result<()> {
let cli = Cli::from_args();

match cli.subcommand {
Some(ref subcommand) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
let PartialComponents { client, backend, task_manager, import_queue, .. }
match &cli.subcommand {
Some(Subcommand::BuildSpec(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
bkchr marked this conversation as resolved.
Show resolved Hide resolved
},
Some(Subcommand::CheckBlock(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, import_queue, ..}
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
Ok((cmd.run(client, import_queue), task_manager))
})
}
},
Some(Subcommand::ExportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, ..}
= new_partial(&config)?;
Ok((cmd.run(client, config.database), task_manager))
})
},
Some(Subcommand::ExportState(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, ..}
= new_partial(&config)?;
Ok((cmd.run(client, config.chain_spec), task_manager))
})
},
Some(Subcommand::ImportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, import_queue, ..}
= new_partial(&config)?;
Ok((cmd.run(client, import_queue), task_manager))
})
},
Some(Subcommand::PurgeChain(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.database))
},
Some(Subcommand::Revert(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, backend, ..}
= new_partial(&config)?;
Ok((cmd.run(client, backend), task_manager))
})
},
None => {
let runner = cli.create_runner(&cli.run)?;
runner.run_node_until_exit(|config| match config.role {
Expand Down
25 changes: 21 additions & 4 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ pub struct Cli {
/// Possible subcommands of the main binary.
#[derive(Debug, StructOpt)]
pub enum Subcommand {
/// A set of base subcommands handled by `sc_cli`.
#[structopt(flatten)]
Base(sc_cli::Subcommand),

/// Key management cli utilities
Key(KeySubcommand),

Expand All @@ -59,4 +55,25 @@ pub enum Subcommand {

/// Sign a message, with a given (secret) key.
Sign(SignCmd),

/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),

/// Validate blocks.
CheckBlock(sc_cli::CheckBlockCmd),

/// Export blocks.
ExportBlocks(sc_cli::ExportBlocksCmd),

/// Export the state of a given block into a chain spec.
ExportState(sc_cli::ExportStateCmd),

/// Import blocks.
ImportBlocks(sc_cli::ImportBlocksCmd),

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

/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
}
52 changes: 46 additions & 6 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,53 @@ pub fn run() -> Result<()> {
Some(Subcommand::Sign(cmd)) => cmd.run(),
Some(Subcommand::Verify(cmd)) => cmd.run(),
Some(Subcommand::Vanity(cmd)) => cmd.run(),
Some(Subcommand::Base(subcommand)) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
let PartialComponents { client, backend, task_manager, import_queue, ..}
Some(Subcommand::BuildSpec(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
},
Some(Subcommand::CheckBlock(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, import_queue, ..}
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
Ok((cmd.run(client, import_queue), task_manager))
})
}
},
Some(Subcommand::ExportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, ..}
= new_partial(&config)?;
Ok((cmd.run(client, config.database), task_manager))
})
},
Some(Subcommand::ExportState(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, ..}
= new_partial(&config)?;
Ok((cmd.run(client, config.chain_spec), task_manager))
})
},
Some(Subcommand::ImportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, import_queue, ..}
= new_partial(&config)?;
Ok((cmd.run(client, import_queue), task_manager))
})
},
Some(Subcommand::PurgeChain(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.database))
},
Some(Subcommand::Revert(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, backend, ..}
= new_partial(&config)?;
Ok((cmd.run(client, backend), task_manager))
})
},
}
}
Loading