From 1a100e6cbf145ce74eb9286f1638d4dbeee1c8f9 Mon Sep 17 00:00:00 2001 From: Scott Piriou Date: Fri, 28 Feb 2020 16:15:27 +0100 Subject: [PATCH 1/3] Make export blocks default to json on stdout --- client/cli/src/commands/export_blocks_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cli/src/commands/export_blocks_cmd.rs b/client/cli/src/commands/export_blocks_cmd.rs index cdfa463c6d561..d2a1bd8b76740 100644 --- a/client/cli/src/commands/export_blocks_cmd.rs +++ b/client/cli/src/commands/export_blocks_cmd.rs @@ -51,7 +51,7 @@ pub struct ExportBlocksCmd { pub to: Option, /// Use JSON output rather than binary. - #[structopt(long = "json")] + #[structopt(long = "json", parse(try_from_str), default_value("true"), default_value_if("output", None, "false"))] pub json: bool, #[allow(missing_docs)] From 846286e225d248740d4397604c088d425d76aa48 Mon Sep 17 00:00:00 2001 From: Scott Piriou Date: Fri, 28 Feb 2020 16:22:02 +0100 Subject: [PATCH 2/3] Multiline instead of single line to stay under 100 cols --- client/cli/src/commands/export_blocks_cmd.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/client/cli/src/commands/export_blocks_cmd.rs b/client/cli/src/commands/export_blocks_cmd.rs index d2a1bd8b76740..190f4e8e76627 100644 --- a/client/cli/src/commands/export_blocks_cmd.rs +++ b/client/cli/src/commands/export_blocks_cmd.rs @@ -51,7 +51,10 @@ pub struct ExportBlocksCmd { pub to: Option, /// Use JSON output rather than binary. - #[structopt(long = "json", parse(try_from_str), default_value("true"), default_value_if("output", None, "false"))] + #[structopt(long = "json", + parse(try_from_str), + default_value("true"), + default_value_if("output", None, "false"))] pub json: bool, #[allow(missing_docs)] From 7f1b9cb612ebcc0d7ae1901db315dcf8edb5da4a Mon Sep 17 00:00:00 2001 From: Scott Piriou Date: Sun, 1 Mar 2020 18:31:53 +0100 Subject: [PATCH 3/3] Change --json flag to --binary, defaulting to json --- client/cli/src/commands/export_blocks_cmd.rs | 13 +++++-------- client/service/src/builder.rs | 2 +- client/service/src/chain_ops.rs | 12 ++++++------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/client/cli/src/commands/export_blocks_cmd.rs b/client/cli/src/commands/export_blocks_cmd.rs index 190f4e8e76627..21195cccd4664 100644 --- a/client/cli/src/commands/export_blocks_cmd.rs +++ b/client/cli/src/commands/export_blocks_cmd.rs @@ -50,12 +50,9 @@ pub struct ExportBlocksCmd { #[structopt(long = "to", value_name = "BLOCK")] pub to: Option, - /// Use JSON output rather than binary. - #[structopt(long = "json", - parse(try_from_str), - default_value("true"), - default_value_if("output", None, "false"))] - pub json: bool, + /// Use binary output rather than JSON. + #[structopt(long = "binary", value_name = "BOOL", parse(try_from_str), default_value("false"))] + pub binary: bool, #[allow(missing_docs)] #[structopt(flatten)] @@ -88,7 +85,7 @@ impl ExportBlocksCmd { let from = self.from.as_ref().and_then(|f| f.parse().ok()).unwrap_or(1); let to = self.to.as_ref().and_then(|t| t.parse().ok()); - let json = self.json; + let binary = self.binary; let file: Box = match &self.output { Some(filename) => Box::new(fs::File::create(filename)?), @@ -96,7 +93,7 @@ impl ExportBlocksCmd { }; run_until_exit(config, |config| { - Ok(builder(config)?.export_blocks(file, from.into(), to, json)) + Ok(builder(config)?.export_blocks(file, from.into(), to, binary)) }) } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 8c85c38b18844..e5e4e132f9cd9 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -742,7 +742,7 @@ pub trait ServiceBuilderCommand { output: impl Write + 'static, from: NumberFor, to: Option>, - json: bool + binary: bool ) -> Pin>>>; /// Performs a revert of `blocks` blocks. diff --git a/client/service/src/chain_ops.rs b/client/service/src/chain_ops.rs index a0724f3e1decc..03db9232a1051 100644 --- a/client/service/src/chain_ops.rs +++ b/client/service/src/chain_ops.rs @@ -203,7 +203,7 @@ impl< mut output: impl Write + 'static, from: NumberFor, to: Option>, - json: bool + binary: bool ) -> Pin>>> { let client = self.client; let mut block = from; @@ -230,7 +230,7 @@ impl< if !wrote_header { info!("Exporting blocks from #{} to #{}", block, last); - if !json { + if binary { let last_: u64 = last.saturated_into::(); let block_: u64 = block.saturated_into::(); let len: u64 = last_ - block_ + 1; @@ -241,13 +241,13 @@ impl< match client.block(&BlockId::number(block))? { Some(block) => { - if json { + if binary { + output.write_all(&block.encode())?; + } else { serde_json::to_writer(&mut output, &block) .map_err(|e| format!("Error writing JSON: {}", e))?; - } else { - output.write_all(&block.encode())?; } - }, + }, // Reached end of the chain. None => return std::task::Poll::Ready(Ok(())), }