Skip to content

Commit

Permalink
Make export blocks default to json on stdout (paritytech#5090)
Browse files Browse the repository at this point in the history
* Make export blocks default to json on stdout

* Multiline instead of single line to stay under 100 cols

* Change --json flag to --binary, defaulting to json
  • Loading branch information
pscott authored and General-Beck committed Mar 4, 2020
1 parent 7700712 commit 2bc0a15
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions client/cli/src/commands/export_blocks_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pub struct ExportBlocksCmd {
#[structopt(long = "to", value_name = "BLOCK")]
pub to: Option<BlockNumber>,

/// Use JSON output rather than binary.
#[structopt(long = "json")]
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)]
Expand Down Expand Up @@ -85,15 +85,15 @@ 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<dyn io::Write> = match &self.output {
Some(filename) => Box::new(fs::File::create(filename)?),
None => Box::new(io::stdout()),
};

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))
})
}

Expand Down
2 changes: 1 addition & 1 deletion client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ pub trait ServiceBuilderCommand {
output: impl Write + 'static,
from: NumberFor<Self::Block>,
to: Option<NumberFor<Self::Block>>,
json: bool
binary: bool
) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>;

/// Performs a revert of `blocks` blocks.
Expand Down
12 changes: 6 additions & 6 deletions client/service/src/chain_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<
mut output: impl Write + 'static,
from: NumberFor<TBl>,
to: Option<NumberFor<TBl>>,
json: bool
binary: bool
) -> Pin<Box<dyn Future<Output = Result<(), Error>>>> {
let client = self.client;
let mut block = from;
Expand All @@ -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::<u64>();
let block_: u64 = block.saturated_into::<u64>();
let len: u64 = last_ - block_ + 1;
Expand All @@ -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(())),
}
Expand Down

0 comments on commit 2bc0a15

Please sign in to comment.