From 7d417b189c430b327dcda406f77ac41c06ba47d3 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Fri, 16 Jun 2023 13:34:21 +0300 Subject: [PATCH] cli: Write output to file Signed-off-by: Alexandru Vasile --- cli/src/commands/metadata.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cli/src/commands/metadata.rs b/cli/src/commands/metadata.rs index 98f5447243..8bace30f6f 100644 --- a/cli/src/commands/metadata.rs +++ b/cli/src/commands/metadata.rs @@ -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. @@ -32,6 +32,9 @@ pub struct Opts { /// when using the option. #[clap(long, use_value_delimiter = true, value_parser)] runtime_apis: Option>, + /// Write the output of the metadata command to the provided file path. + #[clap(long, short, value_parser)] + pub output_file: Option, } pub async fn run(opts: Opts, output: &mut impl Write) -> color_eyre::Result<()> { @@ -69,6 +72,11 @@ pub async fn run(opts: Opts, output: &mut impl Write) -> color_eyre::Result<()> } } + let mut output: Box = 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)?;