Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Write output to file #1018

Merged
merged 1 commit into from
Jun 16, 2023
Merged
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
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