From 6071829a074b42847760bf0dd0e26fe922fc96cf Mon Sep 17 00:00:00 2001 From: arkpar Date: Thu, 21 May 2020 08:51:54 +0200 Subject: [PATCH 1/2] Handle piping error for commands that output to stdout --- .gitignore | 1 + client/cli/src/commands/build_spec_cmd.rs | 7 ++++--- client/cli/src/commands/export_state_cmd.rs | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 6398c09fe7962..353d49df28f34 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ rls*.log .local **/hfuzz_target/ **/hfuzz_workspace/ +.cargo/ diff --git a/client/cli/src/commands/build_spec_cmd.rs b/client/cli/src/commands/build_spec_cmd.rs index 2f9e2fa059b30..d921cae5a7eea 100644 --- a/client/cli/src/commands/build_spec_cmd.rs +++ b/client/cli/src/commands/build_spec_cmd.rs @@ -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)] @@ -66,9 +67,9 @@ impl BuildSpecCmd { } let json = sc_service::chain_ops::build_spec(&*spec, raw_output)?; - - print!("{}", json); - + if let Err(_) = std::io::stdout().write_all(json.as_bytes()) { + let _ = std::io::stderr().write_all(b"Error writing to stdout\n"); + } Ok(()) } } diff --git a/client/cli/src/commands/export_state_cmd.rs b/client/cli/src/commands/export_state_cmd.rs index 3ad6772882543..b2377603005d9 100644 --- a/client/cli/src/commands/export_state_cmd.rs +++ b/client/cli/src/commands/export_state_cmd.rs @@ -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 let Err(_) = std::io::stdout().write_all(json.as_bytes()) { + let _ = std::io::stderr().write_all(b"Error writing to stdout\n"); + } Ok(()) } } From 4781de6d77383fb4b24d12083081fc429c9ac581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Thu, 21 May 2020 11:35:26 +0200 Subject: [PATCH 2/2] Apply suggestions from code review --- client/cli/src/commands/build_spec_cmd.rs | 2 +- client/cli/src/commands/export_state_cmd.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/cli/src/commands/build_spec_cmd.rs b/client/cli/src/commands/build_spec_cmd.rs index d921cae5a7eea..d2e2ef3a5467c 100644 --- a/client/cli/src/commands/build_spec_cmd.rs +++ b/client/cli/src/commands/build_spec_cmd.rs @@ -67,7 +67,7 @@ impl BuildSpecCmd { } let json = sc_service::chain_ops::build_spec(&*spec, raw_output)?; - if let Err(_) = std::io::stdout().write_all(json.as_bytes()) { + if std::io::stdout().write_all(json.as_bytes()).is_err() { let _ = std::io::stderr().write_all(b"Error writing to stdout\n"); } Ok(()) diff --git a/client/cli/src/commands/export_state_cmd.rs b/client/cli/src/commands/export_state_cmd.rs index b2377603005d9..33111e7737b1c 100644 --- a/client/cli/src/commands/export_state_cmd.rs +++ b/client/cli/src/commands/export_state_cmd.rs @@ -65,7 +65,7 @@ impl ExportStateCmd { info!("Generating new chain spec..."); let json = sc_service::chain_ops::build_spec(&*input_spec, true)?; - if let Err(_) = std::io::stdout().write_all(json.as_bytes()) { + if std::io::stdout().write_all(json.as_bytes()).is_err() { let _ = std::io::stderr().write_all(b"Error writing to stdout\n"); } Ok(())