Skip to content

Commit

Permalink
feat(rust): add json output to relay show command
Browse files Browse the repository at this point in the history
  • Loading branch information
0x61nas authored and adrianbenavides committed Oct 13, 2023
1 parent 5ef1b3d commit e263d67
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
49 changes: 39 additions & 10 deletions implementations/rust/ockam/ockam_command/src/relay/show.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use clap::Args;
use indoc::formatdoc;
use miette::IntoDiagnostic;

use ockam::Context;
use ockam_api::address::extract_address_value;
use ockam_api::nodes::models::relay::RelayInfo;
use ockam_api::nodes::BackgroundNode;
use ockam_core::api::Request;
use ockam_multiaddr::MultiAddr;

use serde::Serialize;

use crate::node::get_node_name;
use crate::output::Output;
use crate::util::node_rpc;
use crate::{docs, CommandGlobalOpts};

Expand Down Expand Up @@ -37,6 +42,29 @@ impl ShowCommand {
}
}

#[derive(Serialize)]
struct RelayShowOutput {
pub relay_route: String,
pub remote_address: MultiAddr,
pub worker_address: MultiAddr,
}

impl Output for RelayShowOutput {
fn output(&self) -> crate::error::Result<String> {
Ok(formatdoc!(
r#"
Relay:
Relay Route: {route}
Remote Address: {remote_addr}
Worker Address: {worker_addr}
"#,
route = self.relay_route,
remote_addr = self.remote_address,
worker_addr = self.worker_address,
))
}
}

async fn run_impl(
ctx: Context,
(opts, cmd): (CommandGlobalOpts, ShowCommand),
Expand All @@ -52,16 +80,17 @@ async fn run_impl(
)
.await?;

println!("Relay:");
println!(" Relay Route: {}", relay_info.forwarding_route());
println!(
" Remote Address: {}",
relay_info.remote_address_ma().into_diagnostic()?
);
println!(
" Worker Address: {}",
relay_info.worker_address_ma().into_diagnostic()?
);
let output = RelayShowOutput {
relay_route: relay_info.forwarding_route().to_string(),
remote_address: relay_info.remote_address_ma().into_diagnostic()?,
worker_address: relay_info.worker_address_ma().into_diagnostic()?,
};

opts.terminal
.stdout()
.plain(output.output()?)
.json(serde_json::to_string(&output).into_diagnostic()?)
.write_line()?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ teardown() {
run_success "$OCKAM" relay create blue --at /node/n1 --to /node/n2

run_success "$OCKAM" relay show forward_to_blue --at /node/n2
assert_output --regexp "Relay Route:.* => 0#forward_to_blue"
assert_output --partial "Remote Address: /service/forward_to_blue"
assert_output --regexp "Worker Address: /service/.*"
assert_output --regexp "\"relay_route\".* => 0#forward_to_blue"
assert_output --partial "\"remote_address\":\"/service/forward_to_blue\""
assert_output --regexp "\"worker_address\":\"/service/.*"

# Test showing non-existing with no relay
run_failure "$OCKAM" relay show relay_blank --at /node/n2
Expand Down

0 comments on commit e263d67

Please sign in to comment.