diff --git a/cli/src/commands/shared_recovery/create.rs b/cli/src/commands/shared_recovery/create.rs index d6a9c1478f3..e525e83abeb 100644 --- a/cli/src/commands/shared_recovery/create.rs +++ b/cli/src/commands/shared_recovery/create.rs @@ -1,10 +1,11 @@ use std::{collections::HashMap, num::NonZeroU8}; -use dialoguer::Input; +use dialoguer::{Confirm, Input}; +use itertools::Itertools; use libparsec::{UserID, UserProfile}; use crate::utils::{ - poll_server_for_new_certificates, start_spinner, StartedClient, GREEN_CHECKMARK, + poll_server_for_new_certificates, start_spinner, StartedClient, BULLET_CHAR, GREEN_CHECKMARK, }; // TODO: should provide the recipients and their share count as a single parameter @@ -25,6 +26,9 @@ crate::clap_parser_with_shared_opts_builder!( /// Threshold number of shares required to proceed with recovery. #[arg(short, long, requires = "recipients")] threshold: Option, + /// Whether to ask for confirmation or not + #[arg(long, default_value_t)] + no_confirmation: bool, } ); @@ -35,8 +39,10 @@ pub async fn create_shared_recovery(args: Args, client: &StartedClient) -> anyho recipients, weights, threshold, + no_confirmation, .. } = args; + dbg!(no_confirmation); poll_server_for_new_certificates(client).await?; @@ -93,6 +99,29 @@ pub async fn create_shared_recovery(args: Args, client: &StartedClient) -> anyho per_recipient_shares.len() )) .interact_text()? }; + + println!( + "The following shared recovery setup will be created:\n{BULLET_CHAR} Threshold: {threshold}\n{}", + per_recipient_shares + .iter() + .map(|(recipient, share)| { + let user = &users + .iter() + .find(|x| x.id == *recipient) + .expect("missing recipient") + .human_handle; + format!("{BULLET_CHAR} User {user} will have {share} share(s)") // TODO: special case if there is only one share + }) + .join("\n")); + if !no_confirmation + && !Confirm::new() + .with_prompt("Do you want to proceed?") + .interact()? + { + println!("Shared recovery creation aborted."); + return Ok(()); + } + let mut handle = start_spinner("Creating shared recovery setup".into()); client diff --git a/cli/tests/integration/mod.rs b/cli/tests/integration/mod.rs index a611cb1c7d0..835f7fcb59c 100644 --- a/cli/tests/integration/mod.rs +++ b/cli/tests/integration/mod.rs @@ -177,7 +177,8 @@ fn shared_recovery_create( "1", "1", "--threshold", - "1" + "1", + "--no-confirmation" ) .stdout(predicates::str::contains( "Shared recovery setup has been created", @@ -194,7 +195,8 @@ fn shared_recovery_create( "--weights", "1", "--threshold", - "1" + "1", + "--no-confirmation" ) .stdout(predicates::str::contains( "Shared recovery setup has been created", diff --git a/cli/tests/integration/shared_recovery/create.rs b/cli/tests/integration/shared_recovery/create.rs index f20663ba15b..9740cc243d4 100644 --- a/cli/tests/integration/shared_recovery/create.rs +++ b/cli/tests/integration/shared_recovery/create.rs @@ -27,7 +27,8 @@ async fn create_shared_recovery_ok(tmp_path: TmpPath) { "1", "1", "--threshold", - "1" + "1", + "--no-confirmation" ) .stdout(predicates::str::contains( "Shared recovery setup has been created", @@ -57,7 +58,8 @@ async fn create_shared_recovery_incoherent_weights(tmp_path: TmpPath) { "--weights", "1", "--threshold", - "1" + "1", + "--no-confirmation" ) .stderr(predicates::str::contains("incoherent weights count")); } @@ -79,7 +81,8 @@ async fn create_shared_recovery_inexistent_email(tmp_path: TmpPath) { "--weights", "1", "--threshold", - "1" + "1", + "--no-confirmation" ) .stderr(predicates::str::contains("A user is missing")); } @@ -116,6 +119,9 @@ async fn create_shared_recovery_default(tmp_path: TmpPath) { p.exp_regex(".*The threshold is the minimum number of recipients that one must gather to recover the account.*").unwrap(); p.send_line("1").unwrap(); + p.exp_string("The following shared recovery setup will be created") + .unwrap(); + p.send_line("y").unwrap(); p.exp_regex(".*Shared recovery setup has been created.*") .unwrap(); p.exp_eof().unwrap(); diff --git a/cli/tests/integration/shared_recovery/info.rs b/cli/tests/integration/shared_recovery/info.rs index 37723f8514a..190f9e923b3 100644 --- a/cli/tests/integration/shared_recovery/info.rs +++ b/cli/tests/integration/shared_recovery/info.rs @@ -40,7 +40,8 @@ async fn info_shared_recovery_ok(tmp_path: TmpPath) { "1", "1", "--threshold", - "1" + "1", + "--no-confirmation" ) .stdout(predicates::str::contains( "Shared recovery setup has been created",