From 9841e1ef75c45105bbb3e053de4223a96ff45b82 Mon Sep 17 00:00:00 2001 From: Maxime Van Hees Date: Wed, 5 Jun 2024 08:20:31 +0200 Subject: [PATCH] Printing peers list in JSON using --json --- myceliumd/Cargo.lock | 4 ++-- myceliumd/src/cli/peer.rs | 46 ++++++++++++++++++++++++--------------- myceliumd/src/main.rs | 10 ++++++--- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/myceliumd/Cargo.lock b/myceliumd/Cargo.lock index 7fdb2ba..8a2634f 100644 --- a/myceliumd/Cargo.lock +++ b/myceliumd/Cargo.lock @@ -2528,9 +2528,9 @@ dependencies = [ [[package]] name = "unicode-width" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6" +checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" [[package]] name = "universal-hash" diff --git a/myceliumd/src/cli/peer.rs b/myceliumd/src/cli/peer.rs index 04f0222..fb94ae1 100644 --- a/myceliumd/src/cli/peer.rs +++ b/myceliumd/src/cli/peer.rs @@ -5,7 +5,10 @@ use std::net::SocketAddr; use tracing::{debug, error}; /// List the peers the current node is connected to -pub async fn list_peers(server_addr: SocketAddr) -> Result<(), Box> { +pub async fn list_peers( + server_addr: SocketAddr, + json_print: bool, +) -> Result<(), Box> { // Make API call let request_url = format!("http://{server_addr}/api/v1/admin/peers"); match reqwest::get(&request_url).await { @@ -21,26 +24,33 @@ pub async fn list_peers(server_addr: SocketAddr) -> Result<(), Box { - let mut table = Table::new(); - table.add_row(row![ - "Protocol", - "Socket", - "Type", - "Connection", - "Rx total", - "Tx total" - ]); - for peer in peers.iter() { + if json_print { + // Print peers in JSON format + let json_output = serde_json::to_string_pretty(&peers)?; + println!("{json_output}"); + } else { + // Print peers in table format + let mut table = Table::new(); table.add_row(row![ - peer.endpoint.proto(), - peer.endpoint.address(), - peer.pt, - peer.connection_state, - format_bytes(peer.rx_bytes), - format_bytes(peer.tx_bytes), + "Protocol", + "Socket", + "Type", + "Connection", + "Rx total", + "Tx total" ]); + for peer in peers.iter() { + table.add_row(row![ + peer.endpoint.proto(), + peer.endpoint.address(), + peer.pt, + peer.connection_state, + format_bytes(peer.rx_bytes), + format_bytes(peer.tx_bytes), + ]); + } + table.printstd(); } - table.printstd(); } } } diff --git a/myceliumd/src/main.rs b/myceliumd/src/main.rs index 7cd4e88..56e8ebd 100644 --- a/myceliumd/src/main.rs +++ b/myceliumd/src/main.rs @@ -137,7 +137,11 @@ pub enum MessageCommand { #[derive(Debug, Subcommand)] pub enum PeersCommand { /// List the connected peers - List, + List { + /// Print the peers list in JSON format + #[arg(long = "json", default_value_t = false)] + json: bool, + }, /// Add peer(s) Add { peers: Vec }, /// Remove peer(s) @@ -298,8 +302,8 @@ async fn main() -> Result<(), Box> { } }, Command::Peers { command } => match command { - PeersCommand::List {} => { - return cli::list_peers(cli.node_args.api_addr).await; + PeersCommand::List { json } => { + return cli::list_peers(cli.node_args.api_addr, json).await; } PeersCommand::Add { peers } => { return cli::add_peers(cli.node_args.api_addr, peers).await;