From 8e79908e67cde0dd84be94d6c8277cd12a9cd519 Mon Sep 17 00:00:00 2001 From: Francis Murillo Date: Sat, 24 Jul 2021 15:20:59 +0800 Subject: [PATCH] Remove loopback and duplicate addrs in `net peers` output --- forest/src/cli/net_cmd.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/forest/src/cli/net_cmd.rs b/forest/src/cli/net_cmd.rs index dce26122a522..4dcd410fd68c 100644 --- a/forest/src/cli/net_cmd.rs +++ b/forest/src/cli/net_cmd.rs @@ -3,6 +3,7 @@ use forest_libp2p::{Multiaddr, Protocol}; use rpc_api::data_types::AddrInfo; +use std::collections::HashSet; use structopt::StructOpt; use crate::cli::cli_error_and_die; @@ -50,12 +51,26 @@ impl NetCommands { Self::Peers => match net_peers(()).await { Ok(addrs) => { let output: Vec = addrs - .iter() - .map(|info| { - let addresses: Vec = - info.addrs.iter().map(|addr| addr.to_string()).collect(); + .into_iter() + .filter_map(|info| { + let addresses: Vec = info + .addrs + .into_iter() + .filter(|addr| match addr.iter().next().unwrap() { + Protocol::Ip4(ip_addr) => !ip_addr.is_loopback(), + Protocol::Ip6(ip_addr) => !ip_addr.is_loopback(), + _ => true, + }) + .map(|addr| addr.to_string()) + .collect::>() + .into_iter() + .collect(); + + if addresses.is_empty() { + return None; + } - format!("{}, [{}]", info.id, addresses.join(", ")) + Some(format!("{}, [{}]", info.id, addresses.join(", "))) }) .collect();