Skip to content

Commit 8503aa0

Browse files
enabling export-chain-spec in polkadot-parachain
1 parent 6a87107 commit 8503aa0

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

cumulus/polkadot-parachain/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ xcm = { workspace = true, default-features = true }
5252

5353
# Cumulus
5454
cumulus-primitives-core = { workspace = true, default-features = true }
55+
clap = { version = "4.5.13", features = ["derive"] }
56+
anyhow = "1.0.86"
5557

5658
[build-dependencies]
5759
substrate-build-script-utils = { workspace = true, default-features = true }

cumulus/polkadot-parachain/src/main.rs

+35-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (C) Parity Technologies (UK) Ltd.
22
// This file is part of Cumulus.
33
// SPDX-License-Identifier: Apache-2.0
4-
4+
//
55
// Licensed under the Apache License, Version 2.0 (the "License");
66
// you may not use this file except in compliance with the License.
77
// You may obtain a copy of the License at
88
//
9-
// http://www.apache.org/licenses/LICENSE-2.0
9+
// http://www.apache.org/licenses/LICENSE-2.0
1010
//
1111
// Unless required by applicable law or agreed to in writing, software
1212
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,8 +21,15 @@
2121

2222
mod chain_spec;
2323

24-
use polkadot_omni_node_lib::{run, CliConfig as CliConfigT, RunConfig, NODE_VERSION};
24+
use clap::{Command, CommandFactory, FromArgMatches};
25+
use color_eyre::eyre;
26+
use polkadot_omni_node_lib::{
27+
chain_spec::LoadSpec, cli::Cli as OmniCli, run, CliConfig as CliConfigT, RunConfig,
28+
NODE_VERSION,
29+
};
30+
use sc_cli::ExportChainSpecCmd;
2531

32+
/// Custom CLI configuration for metadata.
2633
struct CliConfig;
2734

2835
impl CliConfigT for CliConfig {
@@ -44,12 +51,31 @@ impl CliConfigT for CliConfig {
4451
}
4552
}
4653

47-
fn main() -> color_eyre::eyre::Result<()> {
54+
fn main() -> eyre::Result<()> {
4855
color_eyre::install()?;
4956

50-
let config = RunConfig::new(
51-
Box::new(chain_spec::RuntimeResolver),
52-
Box::new(chain_spec::ChainSpecLoader),
53-
);
54-
Ok(run::<CliConfig>(config)?)
57+
// Build the omni-node CLI command with version info.
58+
let mut cmd: Command = OmniCli::<CliConfig>::command().version(NODE_VERSION);
59+
60+
// Add our export command under the new name "export-chain-spec".
61+
cmd = cmd.subcommand(ExportChainSpecCmd::command().name("export-chain-spec"));
62+
63+
// Parse the combined CLI.
64+
let matches = cmd.get_matches();
65+
66+
// If the export-chain-spec subcommand is invoked, execute that branch.
67+
if let Some(export_matches) = matches.subcommand_matches("export-chain-spec") {
68+
// Clone the matches to get an owned mutable instance.
69+
let mut export_matches_owned = export_matches.clone();
70+
let export_cmd = ExportChainSpecCmd::from_arg_matches_mut(&mut export_matches_owned)?;
71+
let loader = chain_spec::ChainSpecLoader;
72+
let spec = loader.load_spec(&export_cmd.chain).map_err(|e: String| eyre::eyre!(e))?;
73+
export_cmd.run(spec).map_err(Into::into)
74+
} else {
75+
let config = RunConfig::new(
76+
Box::new(chain_spec::RuntimeResolver),
77+
Box::new(chain_spec::ChainSpecLoader),
78+
);
79+
Ok(run::<CliConfig>(config)?)
80+
}
5581
}

0 commit comments

Comments
 (0)