Skip to content

Commit

Permalink
Remove loopback and duplicate addrs in net peers output
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancisMurillo committed Jul 27, 2021
1 parent ffc3019 commit 5f130dc
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions forest/src/cli/net_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,12 +51,37 @@ impl NetCommands {
Self::Peers => match net_peers(()).await {
Ok(addrs) => {
let output: Vec<String> = addrs
.iter()
.map(|info| {
let addresses: Vec<String> =
info.addrs.iter().map(|addr| addr.to_string()).collect();
.into_iter()
.filter_map(|info| {
let addresses: Vec<String> = info
.addrs
.into_iter()
.filter_map(|addr| {
match addr.iter().next().unwrap() {
Protocol::Ip4(ip_addr) => {
if ip_addr.is_loopback() {
return None;
}
}
Protocol::Ip6(ip_addr) => {
if ip_addr.is_loopback() {
return None;
}
}
_ => (),
};

Some(addr.to_string())
})
.collect::<HashSet<_>>()
.into_iter()
.collect();

if addresses.is_empty() {
return None;
}

format!("{}, [{}]", info.id, addresses.join(", "))
Some(format!("{}, [{}]", info.id, addresses.join(", ")))
})
.collect();

Expand Down

0 comments on commit 5f130dc

Please sign in to comment.