Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions implementations/rust/ockam/ockam_command/src/node/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::util::{self, api, connect_to, exitcode, OckamConfig};
use crate::CommandGlobalOpts;
use anyhow::Context;
use clap::Args;
use colorful::Colorful;
use ockam::Route;
use ockam_api::config::cli::NodeConfig;
use ockam_api::nodes::{models::base::NodeStatus, NODEMANAGER_ADDR};
Expand Down Expand Up @@ -32,44 +31,45 @@ impl ShowCommand {

// TODO: This function should be replaced with a better system of
// printing the node state in the future but for now we can just tell
// clippy to stop complainaing about it.
// clippy to stop complaining about it.
#[allow(clippy::too_many_arguments)]
fn print_node_info(node_cfg: &NodeConfig, node_name: &str, status: &str, default_id: &str) {
println!(
r#"
Node:
Name: {}
Status: {}
Services:
Service:
Type: TCP Listener
Address: /ip4/127.0.0.1/tcp/{}
Service:
Type: Secure Channel Listener
Address: /service/api
Route: /ip4/127.0.0.1/tcp/{}/service/api
Identity: {}
Authorized Identities:
- {}
Service:
Type: Uppercase
Address: /service/uppercase
Service:
Type: Echo
Address: /service/echo
Secure Channel Listener Address: /service/api
"#,
node_name,
match status {
"UP" => status.light_green(),
"DOWN" => status.light_red(),
_ => status.white(),
},
node_cfg.port,
node_cfg.port,
default_id,
default_id,
);
use util::dyn_info::{NodeService, ServiceType};
let tcp_addr = format!("/ip4/127.0.0.1/tcp/{}", node_cfg.port);
let sec_chanl_list = format!("/ip4/127.0.0.1/tcp/{}/service/api", node_cfg.port);
let node_out = util::dyn_info::DynNodeInfo::new(node_name)
.status(status)
.service(NodeService::new(
ServiceType::TCPListener,
&tcp_addr,
None,
None,
None,
))
.service(NodeService::new(
ServiceType::SecureChannelListener,
"/service/api",
Some(&sec_chanl_list),
Some(default_id),
Some(vec![default_id]),
))
.service(NodeService::new(
ServiceType::Uppercase,
"/service/uppercase",
None,
None,
None,
))
.service(NodeService::new(
ServiceType::Echo,
"/service/echo",
None,
None,
None,
))
.secure_channel_addr_listener("/service/api");

println!("{}", node_out);
}

pub async fn query_status(
Expand Down
141 changes: 141 additions & 0 deletions implementations/rust/ockam/ockam_command/src/util/dyn_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use colorful::Colorful;
use std::fmt::{self, Write as _};
#[derive(Debug, Clone)]
pub struct DynNodeInfo<'a> {
name: &'a str,
status: &'a str,
services: Vec<NodeService<'a>>,
secure_channel_addr_listener: &'a str,
}

#[derive(Debug, Clone)]
pub struct NodeService<'a> {
service_type: ServiceType,
address: &'a str,
route: Option<&'a str>,
identity: Option<&'a str>,
auth_identity: Option<Vec<&'a str>>,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum ServiceType {
TCPConnection,
TCPListener,
SecureChannelConnection,
SecureChannelListener,
Uppercase,
Echo,
}

// Can't accept arbitrary status
// #[derive(Debug)]
// pub enum Status {
// UP,
// DOWN,
// NONE,
// }

impl<'a> DynNodeInfo<'a> {
/// Name of the Node
pub fn new(name: &'a str) -> Self {
Self {
name,
status: "",
services: Vec::new(),
secure_channel_addr_listener: "",
}
}
/// Status can either be UP, DOWN, TODO
pub fn status(mut self, status: &'a str) -> Self {
self.status = status;
self
}

/// Use NodeService::new()
pub fn service(mut self, service: NodeService<'a>) -> Self {
self.services.push(service);
self
}

pub fn secure_channel_addr_listener(mut self, addr: &'a str) -> Self {
self.secure_channel_addr_listener = addr;
self
}
}

impl<'a> NodeService<'a> {
pub fn new(
service_type: ServiceType,
address: &'a str,
route: Option<&'a str>,
identity: Option<&'a str>,
auth_identity: Option<Vec<&'a str>>,
) -> Self {
Self {
service_type,
address,
route,
identity,
auth_identity,
}
}
}

impl<'a> fmt::Display for DynNodeInfo<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut format_services = String::new();
for service in &self.services {
format_services = format_services + &format!("{}", service);
}

write!(
f,
"Node:\n\tName: {}\n\tStatus: {}\n\tServices:\n{}\tSecure Channel Listener Address:{}",
self.name,
match self.status {
"UP" => self.status.light_green(),
"DOWN" => self.status.light_red(),
_ => self.status.white(),
},
format_services,
self.secure_channel_addr_listener,
)
}
}

impl<'a> fmt::Display for NodeService<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut out = format!(
"\t\tService:\n\t\t\tType: {}\n\t\t\tAddress: {}\n",
self.service_type, self.address
);
if let (Some(route), Some(identity), Some(auth_identity)) =
(self.route, self.identity, self.auth_identity.clone())
{
let mut format_auth_identity = String::new();
for iden in auth_identity {
format_auth_identity = format_auth_identity + &format!("\t\t\t\t- {}\n", iden);
}

let _ = write!(
out,
"\t\t\tRoute: {}\n\t\t\tIdentity: {}\n\t\t\tAuthorized Identities: \n{}",
route, identity, format_auth_identity
);
}
write!(f, "{}", out)
}
}

impl fmt::Display for ServiceType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ServiceType::TCPConnection => write!(f, "TCP Connection"),
ServiceType::TCPListener => write!(f, "TCP Listener"),
ServiceType::SecureChannelConnection => write!(f, "Secure Channel Connection"),
ServiceType::SecureChannelListener => write!(f, "Secure Channel Listener"),
ServiceType::Uppercase => write!(f, "Uppercase"),
ServiceType::Echo => write!(f, "Echo"),
}
}
}
1 change: 1 addition & 0 deletions implementations/rust/ockam/ockam_command/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod api;
pub mod dyn_info;
pub mod exitcode;
pub mod startup;

Expand Down