Handle piping error for commands that output to stdout (#6098)

* Handle piping error for commands that output to stdout

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Arkadiy Paronyan
2020-05-21 13:50:37 +02:00
committed by GitHub
parent 9dd21b1eed
commit 6c47b130e8
3 changed files with 9 additions and 7 deletions
@@ -22,7 +22,7 @@ use crate::{
use log::info;
use sc_service::{Configuration, ServiceBuilderCommand};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{fmt::Debug, str::FromStr};
use std::{fmt::Debug, str::FromStr, io::Write};
use structopt::StructOpt;
/// The `export-state` command used to export the state of a given block into
@@ -65,9 +65,9 @@ impl ExportStateCmd {
info!("Generating new chain spec...");
let json = sc_service::chain_ops::build_spec(&*input_spec, true)?;
print!("{}", json);
if std::io::stdout().write_all(json.as_bytes()).is_err() {
let _ = std::io::stderr().write_all(b"Error writing to stdout\n");
}
Ok(())
}
}