Skip to content

Commit

Permalink
fix: improve UX for CLI restore command (#614)
Browse files Browse the repository at this point in the history
* fix: improve UX for CLI restore command

* Update cli/src/command/wallet.rs

Co-authored-by: Thibault Martinez <[email protected]>

* refactor: check_file_exists

* fix: typo

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
qrayven and thibault-martinez authored Jun 21, 2023
1 parent f1d3dee commit 7c8fba4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use log::LevelFilter;

use crate::{
error::Error,
helper::{enter_or_generate_mnemonic, generate_mnemonic, get_password, import_mnemonic},
helper::{check_file_exists, enter_or_generate_mnemonic, generate_mnemonic, get_password, import_mnemonic},
println_log_info,
};

Expand Down Expand Up @@ -208,6 +208,8 @@ pub async fn node_info_command(storage_path: &Path) -> Result<Wallet, Error> {
}

pub async fn restore_command(storage_path: &Path, snapshot_path: &Path, backup_path: &Path) -> Result<Wallet, Error> {
check_file_exists(backup_path).await?;

let password = get_password("Stronghold password", false)?;
let secret_manager = SecretManager::Stronghold(
StrongholdSecretManager::builder()
Expand All @@ -226,6 +228,11 @@ pub async fn restore_command(storage_path: &Path, snapshot_path: &Path, backup_p

wallet.restore_backup(backup_path.into(), password, None, None).await?;

println_log_info!(
"Wallet has been restored from the backup file \"{}\".",
backup_path.display()
);

Ok(wallet)
}

Expand Down
19 changes: 18 additions & 1 deletion cli/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::path::Path;

use chrono::{DateTime, NaiveDateTime, Utc};
use clap::Parser;
use dialoguer::{console::Term, theme::ColorfulTheme, Input, Select};
Expand All @@ -9,7 +11,7 @@ use iota_sdk::{
wallet::{Account, Wallet},
};
use tokio::{
fs::OpenOptions,
fs::{self, OpenOptions},
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
};

Expand Down Expand Up @@ -244,3 +246,18 @@ pub fn to_utc_date_time(ts_millis: u128) -> Result<DateTime<Utc>, Error> {

Ok(DateTime::from_utc(naive_time, Utc))
}

pub async fn check_file_exists(path: &Path) -> Result<(), Error> {
if !fs::try_exists(path).await.map_err(|e| {
Error::Miscellaneous(format!(
"Error while accessing the file '{path}': '{e}'",
path = path.display()
))
})? {
return Err(Error::Miscellaneous(format!(
"File '{path}' does not exist.",
path = path.display()
)));
}
Ok(())
}

0 comments on commit 7c8fba4

Please sign in to comment.