Skip to content

Commit

Permalink
cli: Write output to file (#1018)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv authored Jun 16, 2023
1 parent f8b1b2b commit d383470
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cli/src/commands/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clap::Parser as ClapParser;
use codec::{Decode, Encode};
use color_eyre::eyre::{self, bail};
use frame_metadata::{v15::RuntimeMetadataV15, RuntimeMetadata, RuntimeMetadataPrefixed};
use std::io::Write;
use std::{io::Write, path::PathBuf};
use subxt_metadata::Metadata;

/// Download metadata from a substrate node, for use with `subxt` codegen.
Expand All @@ -32,6 +32,9 @@ pub struct Opts {
/// when using the option.
#[clap(long, use_value_delimiter = true, value_parser)]
runtime_apis: Option<Vec<String>>,
/// Write the output of the metadata command to the provided file path.
#[clap(long, short, value_parser)]
pub output_file: Option<PathBuf>,
}

pub async fn run(opts: Opts, output: &mut impl Write) -> color_eyre::Result<()> {
Expand Down Expand Up @@ -69,6 +72,11 @@ pub async fn run(opts: Opts, output: &mut impl Write) -> color_eyre::Result<()>
}
}

let mut output: Box<dyn Write> = match opts.output_file {
Some(path) => Box::new(std::fs::File::create(path)?),
None => Box::new(output),
};

match opts.format.as_str() {
"json" => {
let json = serde_json::to_string_pretty(&metadata)?;
Expand Down

0 comments on commit d383470

Please sign in to comment.