Skip to content

Commit

Permalink
feat(rust): ockam vault delete interactive (tuify)
Browse files Browse the repository at this point in the history
Make ockam vault delete (no args) interactive by asking the user to choose from a list of vaults to delete (tuify) build-trust#6462
  • Loading branch information
Darioazzali committed Nov 17, 2023
1 parent 2ba5baa commit 27cdce3
Showing 1 changed file with 76 additions and 14 deletions.
90 changes: 76 additions & 14 deletions implementations/rust/ockam/ockam_command/src/vault/delete.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use clap::Args;
use colorful::Colorful;

use console::Term;
use ockam::Context;
use ockam_api::cli_state::traits::StateDirTrait;

use crate::terminal::tui::DeleteCommandTui;
use crate::util::node_rpc;
use crate::{docs, fmt_ok, CommandGlobalOpts};
use crate::{docs, fmt_ok, fmt_warn, CommandGlobalOpts, Terminal, TerminalStream};

const LONG_ABOUT: &str = include_str!("./static/delete/long_about.txt");
const AFTER_LONG_HELP: &str = include_str!("./static/delete/after_long_help.txt");
Expand All @@ -18,11 +20,14 @@ after_long_help = docs::after_help(AFTER_LONG_HELP)
)]
pub struct DeleteCommand {
/// Name of the vault
pub name: String,
pub name: Option<String>,

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

#[arg(long, short, group = "vaults")]
all: bool,
}

impl DeleteCommand {
Expand All @@ -31,6 +36,18 @@ impl DeleteCommand {
}
}

pub struct DeleteTui {
opts: CommandGlobalOpts,
cmd: DeleteCommand,
}

impl DeleteTui {
pub async fn run(opts: CommandGlobalOpts, cmd: DeleteCommand) -> miette::Result<()> {
let tui = Self { opts, cmd };
tui.delete().await
}
}

async fn rpc(ctx: Context, (opts, cmd): (CommandGlobalOpts, DeleteCommand)) -> miette::Result<()> {
run_impl(&ctx, opts, cmd).await
}
Expand All @@ -40,19 +57,64 @@ async fn run_impl(
opts: CommandGlobalOpts,
cmd: DeleteCommand,
) -> miette::Result<()> {
let DeleteCommand { name, yes } = cmd;
if opts
.terminal
.confirmed_with_flag_or_prompt(yes, "Are you sure you want to delete this vault?")?
{
opts.state.vaults.get(&name)?;
opts.state.vaults.delete(&name)?;
opts.terminal
DeleteTui::run(opts, cmd).await
}

#[ockam_core::async_trait]
impl DeleteCommandTui for DeleteTui {
const ITEM_NAME: &'static str = "vaults";
fn cmd_arg_item_name(&self) -> Option<&str> {
self.cmd.name.as_deref()
}

fn cmd_arg_delete_all(&self) -> bool {
self.cmd.all
}

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> {
Ok(self
.cmd
.name
.clone()
.unwrap_or(self.opts.state.vaults.default()?.name().to_string()))
}

async fn list_items_names(&self) -> miette::Result<Vec<String>> {
Ok(self.opts.state.vaults.list_items_names()?)
}

async fn delete_single(&self, item_name: &str) -> miette::Result<()> {
self.opts.state.vaults.delete(item_name)?;
self.terminal()
.stdout()
.plain(fmt_ok!("Vault with name '{name}' has been deleted"))
.machine(&name)
.json(serde_json::json!({ "name": &name }))
.plain(fmt_ok!("Vault with name '{item_name}' has been deleted"))
.machine(item_name)
.json(serde_json::json!({ "name": &item_name }))
.write_line()?;

Ok(())
}

async fn delete_multiple(&self, selected_items_names: Vec<String>) -> miette::Result<()> {
let plain = selected_items_names
.iter()
.map(|name| {
if self.opts.state.vaults.delete(name).is_ok() {
Ok(fmt_ok!("Vault '{name}' deleted\n"))
} else {
Ok(fmt_warn!("Failed to delete vault '{name}'\n"))
}
})
.collect::<miette::Result<String>>()?;
self.terminal().stdout().plain(plain).write_line()?;
Ok(())
}
Ok(())
}

0 comments on commit 27cdce3

Please sign in to comment.