soil_cli/commands/export_chain_spec_cmd.rs
1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use crate::error::Result;
8use clap::Parser;
9use soil_service::{chain_ops, ChainSpec};
10use std::{
11 fs,
12 io::{self, Write},
13 path::PathBuf,
14};
15
16/// Export a chain-spec to a JSON file in plain or in raw storage format.
17///
18/// Nodes that expose this command usually have embedded runtimes WASM blobs with
19/// genesis config presets which can be referenced via `--chain <id>` . The logic for
20/// loading the chain spec into memory based on an `id` is specific to each
21/// node and is a prerequisite to enable this command.
22///
23/// Same functionality can be achieved currently via
24/// [`crate::commands::build_spec_cmd::BuildSpecCmd`] but we recommend
25/// `export-chain-spec` in its stead. `build-spec` is known
26/// to be a legacy mix of exporting chain specs to JSON files or
27/// converting them to raw, which will be better
28/// represented under `export-chain-spec`.
29#[derive(Debug, Clone, Parser)]
30pub struct ExportChainSpecCmd {
31 /// The chain spec identifier to export.
32 #[arg(long, default_value = "local")]
33 pub chain: String,
34
35 /// `chain-spec` JSON file path. If omitted, prints to stdout.
36 #[arg(long)]
37 pub output: Option<PathBuf>,
38
39 /// Export in raw genesis storage format.
40 #[arg(long)]
41 pub raw: bool,
42}
43
44impl ExportChainSpecCmd {
45 /// Run the export-chain-spec command
46 pub fn run(&self, spec: Box<dyn ChainSpec>) -> Result<()> {
47 let json = chain_ops::build_spec(spec.as_ref(), self.raw)?;
48 if let Some(ref path) = self.output {
49 fs::write(path, json)?;
50 println!("Exported chain spec to {}", path.display());
51 } else {
52 io::stdout().write_all(json.as_bytes()).map_err(|e| format!("{}", e))?;
53 }
54 Ok(())
55 }
56}