Skip to content

Commit

Permalink
refactor(rust): tuify relay delete and relay show commands
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Nov 10, 2023
1 parent 9ab6d8b commit 1051862
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,4 @@ impl BackgroundNode {
let route = self.create_route().await?;
Ok(Client::new(&route, timeout))
}

/// Get the node name
pub fn name(&self) -> &str {
&self.node_name
}
}
4 changes: 2 additions & 2 deletions implementations/rust/ockam/ockam_command/src/node/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ impl ShowCommandTui for ShowTui {
Ok(())
}

async fn show_multiple(&self, selected_items_names: Vec<String>) -> miette::Result<()> {
let nodes = list::get_nodes_info(&self.ctx, &self.opts, selected_items_names).await?;
async fn show_multiple(&self, items_names: Vec<String>) -> miette::Result<()> {
let nodes = list::get_nodes_info(&self.ctx, &self.opts, items_names).await?;
list::print_nodes_info(&self.opts, nodes)?;
Ok(())
}
Expand Down
168 changes: 123 additions & 45 deletions implementations/rust/ockam/ockam_command/src/relay/delete.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
use clap::Args;
use colorful::Colorful;
use miette::{miette, IntoDiagnostic};
use console::Term;
use miette::miette;

use ockam::Context;
use ockam_api::nodes::models::relay::RelayInfo;
use ockam_api::nodes::BackgroundNode;
use ockam_core::api::Request;

use crate::node::get_node_name;
use crate::relay::util::relay_name_parser;
use crate::terminal::tui::DeleteCommandTui;
use crate::util::{node_rpc, parse_node_name};
use crate::{docs, fmt_ok, CommandGlobalOpts};
use crate::{docs, fmt_ok, fmt_warn, CommandGlobalOpts, Terminal, TerminalStream};

const AFTER_LONG_HELP: &str = include_str!("./static/delete/after_long_help.txt");

/// Delete a Relay
#[derive(Clone, Debug, Args)]
#[command(
arg_required_else_help = false,
after_long_help = docs::after_help(AFTER_LONG_HELP)
)]
#[command(after_long_help = docs::after_help(AFTER_LONG_HELP))]
pub struct DeleteCommand {
/// Name assigned to Relay that will be deleted
#[arg(display_order = 900, required = true)]
relay_name: String,
/// Name assigned to the Relay, prefixed with 'forward_to_'. Example: 'forward_to_myrelay'
#[arg(value_parser = relay_name_parser)]
relay_name: Option<String>,

/// Node on which to delete the Relay. If not provided, the default node will be used
#[arg(global = true, long, value_name = "NODE")]
pub at: Option<String>,

/// Confirm the deletion without prompting
#[arg(display_order = 901, long, short)]
#[arg(long, short)]
yes: bool,
}

Expand All @@ -43,43 +43,121 @@ pub async fn run_impl(
ctx: Context,
(opts, cmd): (CommandGlobalOpts, DeleteCommand),
) -> miette::Result<()> {
let relay_name = cmd.relay_name.clone();
let at = get_node_name(&opts.state, &cmd.at);
let node_name = parse_node_name(&at)?;
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;

// Check if relay exists
node.ask_and_get_reply::<_, RelayInfo>(
&ctx,
Request::get(format!("/node/forwarder/{relay_name}")),
)
.await?
.found()
.into_diagnostic()?
.ok_or(miette!("Relay with name '{}' does not exist", relay_name))?;

// Proceed with the deletion
if opts
.terminal
.confirmed_with_flag_or_prompt(cmd.yes, "Are you sure you want to delete this relay?")?
{
node.tell(
&ctx,
Request::delete(format!("/node/forwarder/{relay_name}",)),
)
.await?;

opts.terminal
DeleteTui::run(ctx, opts, cmd).await
}

struct DeleteTui {
ctx: Context,
opts: CommandGlobalOpts,
node: BackgroundNode,
cmd: DeleteCommand,
}

impl DeleteTui {
pub async fn run(
ctx: Context,
opts: CommandGlobalOpts,
cmd: DeleteCommand,
) -> miette::Result<()> {
let node_name = {
let name = get_node_name(&opts.state, &cmd.at);
parse_node_name(&name)?
};
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;
let tui = Self {
ctx,
opts,
node,
cmd,
};
tui.delete().await
}
}

#[ockam_core::async_trait]
impl DeleteCommandTui for DeleteTui {
const ITEM_NAME: &'static str = "relay";

fn cmd_arg_item_name(&self) -> Option<&str> {
self.cmd.relay_name.as_deref()
}

fn cmd_arg_delete_all(&self) -> bool {
false
}

fn cmd_arg_confirm_deletion(&self) -> bool {
self.cmd.yes
}

fn terminal(&self) -> Terminal<TerminalStream<Term>> {
self.opts.terminal.clone()
}

async fn get_arg_item_name_or_default(&self) -> miette::Result<String> {
self.cmd
.relay_name
.clone()
.ok_or(miette!("No relay name provided"))
}

async fn list_items_names(&self) -> miette::Result<Vec<String>> {
let relays: Vec<RelayInfo> = self
.node
.ask(&self.ctx, Request::get("/node/forwarder"))
.await?;
let names = relays
.into_iter()
.map(|i| i.remote_address().to_string())
.collect();
Ok(names)
}

async fn delete_single(&self, item_name: &str) -> miette::Result<()> {
let node_name = self.node.node_name();
self.node
.tell(
&self.ctx,
Request::delete(format!("/node/forwarder/{item_name}")),
)
.await?;
self.terminal()
.stdout()
.plain(fmt_ok!(
"Relay with name {} on Node {} has been deleted.",
relay_name,
node_name
"Relay with name {} on Node {} has been deleted",
item_name.light_magenta(),
node_name.light_magenta()
))
.machine(&relay_name)
.json(serde_json::json!({ "name": relay_name, "node": node_name }))
.write_line()
.unwrap();
.write_line()?;
Ok(())
}

async fn delete_multiple(&self, items_names: Vec<String>) -> miette::Result<()> {
let node_name = self.node.node_name();
let mut plain = String::new();
for item_name in items_names {
let res = self
.node
.tell(
&self.ctx,
Request::delete(format!("/node/forwarder/{item_name}")),
)
.await;
if res.is_ok() {
plain.push_str(&fmt_ok!(
"Relay with name {} on Node {} has been deleted\n",
item_name.light_magenta(),
node_name.light_magenta()
));
} else {
plain.push_str(&fmt_warn!(
"Failed to delete relay with name {} on Node {}\n",
item_name.light_magenta(),
node_name.light_magenta()
));
}
}
self.terminal().stdout().plain(plain).write_line()?;
Ok(())
}
Ok(())
}
1 change: 1 addition & 0 deletions implementations/rust/ockam/ockam_command/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod create;
mod delete;
mod list;
mod show;
mod util;

const LONG_ABOUT: &str = include_str!("./static/long_about.txt");
const AFTER_LONG_HELP: &str = include_str!("./static/after_long_help.txt");
Expand Down
Loading

0 comments on commit 1051862

Please sign in to comment.