forked from build-trust/ockam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): Improve JSON output of ockam node show
Improves the output of the command `ockam node show --output json` adding all the information that is being displayed with the plain output. Refactors the plain response to build it using the Output trait erasing the a clippy alert in the process. Closes build-trust#6142
- Loading branch information
Showing
11 changed files
with
310 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
implementations/rust/ockam/ockam_command/src/node/models/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod portal; | ||
pub mod show; |
23 changes: 23 additions & 0 deletions
23
implementations/rust/ockam/ockam_command/src/node/models/portal.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use ockam_api::{ | ||
nodes::models::portal::{InletList, InletStatus}, | ||
route_to_multiaddr, | ||
}; | ||
use ockam_core::Route; | ||
use ockam_multiaddr::MultiAddr; | ||
use serde::{Serialize, Serializer}; | ||
|
||
/// Response body when interacting with a portal endpoint | ||
#[derive(Debug, Serialize)] | ||
pub struct ShowInletStatus { | ||
pub listen_address: String, | ||
pub route_to_outlet: Option<MultiAddr>, | ||
} | ||
|
||
impl From<InletStatus> for ShowInletStatus { | ||
fn from(value: InletStatus) -> Self { | ||
Self { | ||
listen_address: value.bind_addr, | ||
route_to_outlet: Route::parse(value.outlet_route).and_then(|r| route_to_multiaddr(&r)), | ||
} | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
implementations/rust/ockam/ockam_command/src/node/models/show.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
use std::fmt::Display; | ||
|
||
use colorful::Colorful; | ||
|
||
use ockam_core::Route; | ||
use ockam_multiaddr::{ | ||
proto::{DnsAddr, Node, Tcp}, | ||
MultiAddr, | ||
}; | ||
use serde::Serialize; | ||
|
||
use ockam_api::{ | ||
addr_to_multiaddr, | ||
nodes::models::{ | ||
portal::{InletList, OutletList}, | ||
secure_channel::SecureChannelListenersList, | ||
services::ServiceList, | ||
transport::TransportList, | ||
}, | ||
route_to_multiaddr, | ||
}; | ||
|
||
use crate::output::Output; | ||
|
||
use super::portal::ShowInletStatus; | ||
|
||
#[derive(Debug, Serialize)] | ||
pub struct ShowNodeResponse<'a> { | ||
pub is_default: bool, | ||
pub name: &'a str, | ||
pub is_up: bool, | ||
pub route: RouteToNode, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub identity: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none", flatten)] | ||
pub tcp_listeners: Option<TransportList>, | ||
#[serde(skip_serializing_if = "Option::is_none", flatten)] | ||
pub secure_channel_listeners: Option<SecureChannelListenersList>, | ||
pub inlets: Vec<ShowInletStatus>, | ||
#[serde(skip_serializing_if = "Option::is_none", flatten)] | ||
pub outlets: Option<OutletList>, | ||
#[serde(skip_serializing_if = "Option::is_none", flatten)] | ||
pub service_list: Option<ServiceList>, | ||
} | ||
#[derive(Debug, Serialize)] | ||
pub struct RouteToNode { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub short: Option<MultiAddr>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub verbose: Option<MultiAddr>, | ||
} | ||
|
||
impl<'a> ShowNodeResponse<'a> { | ||
pub fn new( | ||
is_default: bool, | ||
name: &'a str, | ||
is_up: bool, | ||
node_port: Option<u16>, | ||
) -> ShowNodeResponse { | ||
let mut m = MultiAddr::default(); | ||
let short = m.push_back(Node::new(name)).ok().map(|_| m); | ||
|
||
let verbose = node_port.and_then(|port| { | ||
let mut m = MultiAddr::default(); | ||
if m.push_back(DnsAddr::new("localhost")).is_ok() && m.push_back(Tcp::new(port)).is_ok() | ||
{ | ||
Some(m) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
ShowNodeResponse { | ||
is_default, | ||
name, | ||
is_up, | ||
route: RouteToNode { short, verbose }, | ||
identity: None, | ||
tcp_listeners: None, | ||
secure_channel_listeners: None, | ||
inlets: Default::default(), | ||
outlets: None, | ||
service_list: None, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Display for ShowNodeResponse<'a> { | ||
fn fmt(&self, buffer: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(buffer, "Node:")?; | ||
|
||
if self.is_default { | ||
writeln!(buffer, " Name: {} (default)", self.name)?; | ||
} else { | ||
writeln!(buffer, " Name: {}", self.name)?; | ||
} | ||
|
||
writeln!( | ||
buffer, | ||
" Status: {}", | ||
match self.is_up { | ||
true => "UP".light_green(), | ||
false => "DOWN".light_red(), | ||
} | ||
)?; | ||
|
||
writeln!(buffer, " Route To Node:")?; | ||
if let Some(short) = &self.route.short { | ||
writeln!(buffer, " Short: {short}")?; | ||
} | ||
if let Some(verbose) = &self.route.verbose { | ||
writeln!(buffer, " Verbose: {verbose}")?; | ||
} | ||
|
||
if let Some(identity) = &self.identity { | ||
writeln!(buffer, " Identity: {}", identity)?; | ||
} | ||
|
||
if let Some(list) = &self.tcp_listeners { | ||
writeln!(buffer, " Transports:")?; | ||
for e in &list.list { | ||
writeln!(buffer, " Transport:")?; | ||
writeln!(buffer, " Type: {}", &e.tt)?; | ||
writeln!(buffer, " Mode: {}", &e.tm)?; | ||
writeln!(buffer, " Socket: {}", &e.socket_addr)?; | ||
writeln!(buffer, " Worker: {}", &e.worker_addr)?; | ||
writeln!(buffer, " FlowControlId: {}", &e.flow_control_id)?; | ||
} | ||
} | ||
|
||
if let Some(list) = &self.secure_channel_listeners { | ||
writeln!(buffer, " Secure Channel Listeners:")?; | ||
for e in &list.list { | ||
writeln!(buffer, " Listener:")?; | ||
if let Some(ma) = addr_to_multiaddr(e.addr.clone()) { | ||
writeln!(buffer, " Address: {ma}")?; | ||
writeln!(buffer, " FlowControlId: {}", &e.flow_control_id)?; | ||
} | ||
} | ||
} | ||
|
||
writeln!(buffer, " Inlets:")?; | ||
for e in &self.inlets { | ||
writeln!(buffer, " Inlet:")?; | ||
writeln!(buffer, " Listen Address: {}", e.listen_address)?; | ||
if let Some(r) = &e.route_to_outlet { | ||
writeln!(buffer, " Route To Outlet: {r}")?; | ||
} | ||
} | ||
|
||
if let Some(outlets) = &self.outlets { | ||
writeln!(buffer, " Outlets:")?; | ||
for e in &outlets.list { | ||
writeln!(buffer, " Outlet:")?; | ||
writeln!(buffer, " Forward Address: {}", e.socket_addr)?; | ||
if let Some(ma) = addr_to_multiaddr(e.worker_addr.to_string()) { | ||
writeln!(buffer, " Address: {ma}")?; | ||
} | ||
} | ||
} | ||
|
||
if let Some(list) = &self.service_list { | ||
writeln!(buffer, " Services:")?; | ||
for e in &list.list { | ||
writeln!(buffer, " Service:")?; | ||
writeln!(buffer, " Type: {}", e.service_type)?; | ||
if let Some(ma) = addr_to_multiaddr(e.addr.as_str()) { | ||
writeln!(buffer, " Address: {ma}")?; | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'a> Output for ShowNodeResponse<'a> { | ||
fn output(&self) -> crate::error::Result<String> { | ||
Ok(self.to_string()) | ||
} | ||
} |
Oops, something went wrong.