-
-
Notifications
You must be signed in to change notification settings - Fork 561
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 #6142
- Loading branch information
Showing
11 changed files
with
327 additions
and
199 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ mod default; | |
mod delete; | ||
mod list; | ||
mod logs; | ||
pub mod models; | ||
mod show; | ||
mod start; | ||
mod stop; | ||
|
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; |
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
use std::net::SocketAddr; | ||
|
||
use ockam_api::{ | ||
addr_to_multiaddr, | ||
nodes::models::portal::{InletStatus, OutletStatus}, | ||
route_to_multiaddr, | ||
}; | ||
use ockam_core::Route; | ||
use ockam_multiaddr::MultiAddr; | ||
use serde::Serialize; | ||
|
||
/// Information to display of the inlets in the `ockam node show` command | ||
#[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)), | ||
} | ||
} | ||
} | ||
|
||
/// Information to display of the inlets in the `ockam node show` command | ||
#[derive(Debug, Serialize)] | ||
pub struct ShowOutletStatus { | ||
pub forward_address: SocketAddr, | ||
pub address: Option<MultiAddr>, | ||
} | ||
|
||
impl From<OutletStatus> for ShowOutletStatus { | ||
fn from(value: OutletStatus) -> Self { | ||
Self { | ||
forward_address: value.socket_addr, | ||
address: addr_to_multiaddr(value.worker_addr), | ||
} | ||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
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,175 @@ | ||
use std::fmt::Display; | ||
|
||
use colorful::Colorful; | ||
|
||
use ockam_multiaddr::{ | ||
proto::{DnsAddr, Node, Tcp}, | ||
MultiAddr, | ||
}; | ||
use serde::Serialize; | ||
|
||
use ockam_api::{ | ||
addr_to_multiaddr, | ||
nodes::models::{ | ||
secure_channel::SecureChannelListenersList, | ||
services::ServiceList, | ||
transport::TransportList, | ||
}, | ||
}; | ||
|
||
use crate::output::Output; | ||
|
||
use super::portal::{ShowInletStatus, ShowOutletStatus}; | ||
|
||
#[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>, | ||
pub outlets: Vec<ShowOutletStatus>, | ||
#[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: Default::default(), | ||
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}")?; | ||
} | ||
} | ||
|
||
writeln!(buffer, " Outlets:")?; | ||
for e in &self.outlets { | ||
writeln!(buffer, " Outlet:")?; | ||
writeln!(buffer, " Forward Address: {}", e.forward_address)?; | ||
if let Some(ma) = &e.address { | ||
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.