Skip to content

Commit 1eed4fb

Browse files
committed
cli: add json flag to query command
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent e553f51 commit 1eed4fb

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* sdk: add support to embedded tor client ([Yuki Kishimoto])
6363
* ffi(nostr): add `EventBuilder::seal` constructor ([Yuki Kishimoto])
6464
* cli: add `generate` command ([Yuki Kishimoto])
65+
* cli: add `json` flag to `query` command ([Yuki Kishimoto])
6566
* book: add some python examples ([RydalWater])
6667

6768
### Fixed

crates/nostr-cli/src/cli/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub enum Command {
8585
/// Print result
8686
#[clap(long)]
8787
print: bool,
88+
/// Print result as JSON (require `print` flag!)
89+
#[clap(long)]
90+
json: bool,
8891
},
8992
/// Database
9093
#[command(arg_required_else_help = true)]

crates/nostr-cli/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ async fn handle_command(command: Command, client: &Client) -> Result<()> {
188188
reverse,
189189
database,
190190
print,
191+
json,
191192
} => {
192193
let db = client.database();
193194

@@ -246,7 +247,7 @@ async fn handle_command(command: Command, client: &Client) -> Result<()> {
246247
);
247248
if print {
248249
// Print events
249-
util::print_events(events);
250+
util::print_events(events, json);
250251
}
251252
} else {
252253
// Query relays

crates/nostr-cli/src/util.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22
// Copyright (c) 2023-2024 Rust Nostr Developers
33
// Distributed under the MIT software license
44

5-
use nostr_sdk::Event;
5+
use nostr_sdk::prelude::*;
66
use prettytable::{row, Table};
77

8-
pub fn print_events<I>(events: I)
8+
pub fn print_events<I>(events: I, json: bool)
99
where
1010
I: IntoIterator<Item = Event>,
1111
{
12-
let mut table = Table::new();
12+
if json {
13+
for (index, event) in events.into_iter().enumerate() {
14+
println!("{}. {}", index + 1, event.as_pretty_json());
15+
}
16+
} else {
17+
let mut table: Table = Table::new();
1318

14-
table.set_titles(row!["#", "ID", "Author", "Kind", "Created At",]);
19+
table.set_titles(row!["#", "ID", "Author", "Kind", "Created At"]);
1520

16-
for (index, event) in events.into_iter().enumerate() {
17-
table.add_row(row![
18-
index + 1,
19-
event.id,
20-
event.author(),
21-
event.kind(),
22-
event.created_at.to_human_datetime()
23-
]);
24-
}
21+
for (index, event) in events.into_iter().enumerate() {
22+
table.add_row(row![
23+
index + 1,
24+
event.id,
25+
event.author(),
26+
event.kind(),
27+
event.created_at.to_human_datetime()
28+
]);
29+
}
2530

26-
table.printstd();
31+
table.printstd();
32+
}
2733
}

0 commit comments

Comments
 (0)