From d5e6f09c1387efa3c58181cb537f0d4296693ecf Mon Sep 17 00:00:00 2001 From: Shanin Roman Date: Tue, 4 Jun 2024 11:18:33 +0300 Subject: [PATCH] feat(client cli): extend client cli to request json queries Signed-off-by: Shanin Roman --- client_cli/README.md | 10 ++++++++-- client_cli/src/main.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/client_cli/README.md b/client_cli/README.md index cc1e45cd9d6..869fe9b3763 100644 --- a/client_cli/README.md +++ b/client_cli/README.md @@ -45,7 +45,7 @@ iroha [OPTIONS] | `blocks` | Get block stream from Iroha peer | | `domain` | Execute commands related to domains: register a new one, list all domains | | `events` | Get event stream from Iroha peer | -| `json` | Submit multi-instructions as JSON | +| `json` | Submit multi-instructions or request query as JSON | | `peer` | Execute commands related to peer administration and networking | | `wasm` | Execute commands related to WASM | | `help` | Print the help message for `iroha` and/or the current subcommand other than `help` subcommand | @@ -183,5 +183,11 @@ The reference implementation of the Rust client, `iroha`, is often used for diag To test transactions in the JSON format (used in the genesis block and by other SDKs), pipe the transaction into the client and add the `json` subcommand to the arguments: ```bash -cat /path/to/file.json | ./iroha json +cat /path/to/file.json | ./iroha json transaction +``` + +### Request arbitrary query + +```bash +echo '{ "FindAllParameters": null }' | ./iroha --config client.toml json query ``` diff --git a/client_cli/src/main.rs b/client_cli/src/main.rs index 281258b5060..50b7bdaba9b 100644 --- a/client_cli/src/main.rs +++ b/client_cli/src/main.rs @@ -1046,11 +1046,23 @@ mod wasm { mod json { use std::io::{BufReader, Read as _}; + use clap::Subcommand; + use iroha::data_model::query::QueryBox; + use super::*; /// Subcommand for submitting multi-instructions #[derive(Clone, Copy, Debug, clap::Args)] - pub struct Args; + pub struct Args { + #[clap(subcommand)] + variant: Variant, + } + + #[derive(Clone, Copy, Debug, Subcommand)] + enum Variant { + Transaction, + Query, + } impl RunArgs for Args { fn run(self, context: &mut dyn RunContext) -> Result<()> { @@ -1059,9 +1071,24 @@ mod json { reader.read_to_end(&mut raw_content)?; let string_content = String::from_utf8(raw_content)?; - let instructions: Vec = json5::from_str(&string_content)?; - submit(instructions, UnlimitedMetadata::new(), context) - .wrap_err("Failed to submit parsed instructions") + + match self.variant { + Variant::Transaction => { + let instructions: Vec = json5::from_str(&string_content)?; + submit(instructions, UnlimitedMetadata::new(), context) + .wrap_err("Failed to submit parsed instructions") + } + Variant::Query => { + let client = Client::new(context.configuration().clone()); + let query: QueryBox = json5::from_str(&string_content)?; + let response = client + .request(query) + .and_then(core::convert::identity) + .wrap_err("Failed to query response")?; + context.print_data(&response)?; + Ok(()) + } + } } } }