Skip to content

Commit

Permalink
Handle piping error for commands that output to stdout (paritytech#6098)
Browse files Browse the repository at this point in the history
* Handle piping error for commands that output to stdout

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
arkpar and bkchr authored May 21, 2020
1 parent a97a493 commit 1789c6d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ rls*.log
.local
**/hfuzz_target/
**/hfuzz_workspace/
.cargo/
7 changes: 4 additions & 3 deletions client/cli/src/commands/build_spec_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use log::info;
use sc_network::config::build_multiaddr;
use sc_service::{config::MultiaddrWithPeerId, Configuration};
use structopt::StructOpt;
use std::io::Write;

/// The `build-spec` command used to build a specification.
#[derive(Debug, StructOpt, Clone)]
Expand Down Expand Up @@ -66,9 +67,9 @@ impl BuildSpecCmd {
}

let json = sc_service::chain_ops::build_spec(&*spec, raw_output)?;

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(())
}
}
Expand Down
8 changes: 4 additions & 4 deletions client/cli/src/commands/export_state_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(())
}
}
Expand Down

0 comments on commit 1789c6d

Please sign in to comment.