-
-
Notifications
You must be signed in to change notification settings - Fork 562
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
9 changed files
with
277 additions
and
197 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
implementations/rust/ockam/ockam_api/src/nodes/models/address.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,19 @@ | ||
use ockam_core::{Address, Route}; | ||
use serde::Serializer; | ||
|
||
use crate::{addr_to_multiaddr, route_to_multiaddr}; | ||
|
||
pub fn serialize_as_multiaddr<T, S>(t: &T, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
T: Into<Address> + Clone, | ||
S: Serializer, | ||
{ | ||
s.serialize_some(&addr_to_multiaddr(t.clone())) | ||
} | ||
|
||
pub fn serialize_route_as_multiaddr<S>(route: &String, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
s.serialize_some(&Route::parse(route).and_then(|r| route_to_multiaddr(&r))) | ||
} |
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
76 changes: 76 additions & 0 deletions
76
implementations/rust/ockam/ockam_api/src/nodes/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,76 @@ | ||
use ockam_multiaddr::{ | ||
proto::{DnsAddr, Node, Tcp}, | ||
MultiAddr, | ||
}; | ||
use serde::Serialize; | ||
|
||
use super::{ | ||
portal::{InletList, OutletList}, | ||
secure_channel::SecureChannelListenersList, | ||
services::ServiceList, | ||
transport::TransportList, | ||
}; | ||
|
||
#[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>, | ||
#[serde(skip_serializing_if = "Option::is_none", flatten)] | ||
pub inlets: Option<InletList>, | ||
#[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: None, | ||
outlets: None, | ||
service_list: None, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.