diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 172c5ad0d6..a742c457d3 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 1.1.0 - 2023-MM-DD +### Changed + +- `WalletCommand::Mnemonic` now takes 2 optional arguments to avoid user interaction; + ### Added - `WalletCommand::Accounts` variant to list all available accounts in a wallet; diff --git a/cli/src/command/account.rs b/cli/src/command/account.rs index 6192099b14..99473f6c53 100644 --- a/cli/src/command/account.rs +++ b/cli/src/command/account.rs @@ -182,7 +182,8 @@ pub enum AccountCommand { token_id: String, /// Amount to send, e.g. 1000000. amount: String, - /// Whether to gift the storage deposit for the output or not, e.g. ` true`. + /// Whether to gift the storage deposit for the output or not, e.g. `true`. + #[arg(value_parser = clap::builder::BoolishValueParser::new())] gift_storage_deposit: Option, }, /// Send an NFT. diff --git a/cli/src/command/wallet.rs b/cli/src/command/wallet.rs index 0200637b7a..9b9d386219 100644 --- a/cli/src/command/wallet.rs +++ b/cli/src/command/wallet.rs @@ -3,7 +3,7 @@ use std::path::Path; -use clap::{Args, Parser, Subcommand}; +use clap::{builder::BoolishValueParser, Args, Parser, Subcommand}; use iota_sdk::{ client::{ constants::SHIMMER_COIN_TYPE, @@ -63,7 +63,14 @@ pub enum WalletCommand { path: Option, }, /// Generate a random mnemonic. - Mnemonic, + Mnemonic { + // Output the mnemonic to the specified file. + #[arg(long)] + output_file_name: Option, + // Output the mnemonic to the stdout. + #[arg(long, num_args = 0..=1, default_missing_value = Some("true"), value_parser = BoolishValueParser::new())] + output_stdout: Option, + }, /// Create a new account. NewAccount { /// Account alias, next available account index if not provided. @@ -194,9 +201,8 @@ pub async fn migrate_stronghold_snapshot_v2_to_v3_command(path: Option) Ok(()) } -pub async fn mnemonic_command() -> Result<(), Error> { - generate_mnemonic().await?; - +pub async fn mnemonic_command(output_file_name: Option, output_stdout: Option) -> Result<(), Error> { + generate_mnemonic(output_file_name, output_stdout).await?; Ok(()) } diff --git a/cli/src/helper.rs b/cli/src/helper.rs index 05f84293f2..9449ad3302 100644 --- a/cli/src/helper.rs +++ b/cli/src/helper.rs @@ -121,7 +121,7 @@ pub async fn enter_or_generate_mnemonic() -> Result { .interact_on(&Term::stderr())?; let mnemnonic = match selected_choice { - 0 => generate_mnemonic().await?, + 0 => generate_mnemonic(None, None).await?, 1 => enter_mnemonic()?, _ => unreachable!(), }; @@ -129,28 +129,45 @@ pub async fn enter_or_generate_mnemonic() -> Result { Ok(mnemnonic) } -pub async fn generate_mnemonic() -> Result { +pub async fn generate_mnemonic( + output_file_name: Option, + output_stdout: Option, +) -> Result { let mnemonic = iota_sdk::client::generate_mnemonic()?; println_log_info!("Mnemonic has been generated."); - let choices = [ - "Write it to the console only", - "Write it to a file only", - "Write it to the console and a file", - ]; - let selected_choice = Select::with_theme(&ColorfulTheme::default()) - .with_prompt("Select how to proceed with it") - .items(&choices) - .default(0) - .interact_on(&Term::stderr())?; + let selected_choice = match (&output_file_name, &output_stdout) { + // Undecided, we give the user a choice + (None, None) | (None, Some(false)) => { + let choices = [ + "Write it to the console only", + "Write it to a file only", + "Write it to the console and a file", + ]; + + Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Select how to proceed with it") + .items(&choices) + .default(0) + .interact_on(&Term::stderr())? + } + // Only console + (None, Some(true)) => 0, + // Only file + (Some(_), Some(false)) | (Some(_), None) => 1, + // File and console + (Some(_), Some(true)) => 2, + }; if [0, 2].contains(&selected_choice) { println!("YOUR MNEMONIC:"); println!("{}", mnemonic.as_ref()); } if [1, 2].contains(&selected_choice) { - write_mnemonic_to_file(DEFAULT_MNEMONIC_FILE_PATH, &mnemonic).await?; - println_log_info!("Mnemonic has been written to '{DEFAULT_MNEMONIC_FILE_PATH}'."); + let file_path = output_file_name.unwrap_or(DEFAULT_MNEMONIC_FILE_PATH.to_string()); + + write_mnemonic_to_file(&file_path, &mnemonic).await?; + println_log_info!("Mnemonic has been written to '{file_path}'."); } println_log_info!("IMPORTANT:"); diff --git a/cli/src/wallet.rs b/cli/src/wallet.rs index 8dc2662f0e..6713515e12 100644 --- a/cli/src/wallet.rs +++ b/cli/src/wallet.rs @@ -58,8 +58,11 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option, Option { - mnemonic_command().await?; + WalletCommand::Mnemonic { + output_file_name, + output_stdout, + } => { + mnemonic_command(output_file_name, output_stdout).await?; return Ok((None, None)); } WalletCommand::NodeInfo => {