Skip to content

Commit

Permalink
refactored input_owner_account_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Jun 29, 2023
1 parent 6e5a13b commit 5fb1bc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
17 changes: 4 additions & 13 deletions src/commands/account/add_key/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::str::FromStr;

use inquire::Text;
use strum::{EnumDiscriminants, EnumIter, EnumMessage};

mod access_key_type;
Expand Down Expand Up @@ -43,16 +40,10 @@ impl AddKeyCommand {
pub fn input_owner_account_id(
context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
let account_id = crate::types::account_id::AccountId::from_str(
&Text::new("Which account should You add an access key to?")
.with_autocomplete(&crate::common::suggester)
.prompt()?,
)?;
crate::common::update_used_account_list(
&context.config.credentials_home_dir,
account_id.clone().into(),
)?;
Ok(Some(account_id))
crate::common::input_account_id_from_used_account_list(
context,
"Which account should You add an access key to?",
)
}
}

Expand Down
32 changes: 24 additions & 8 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use near_primitives::{hash::CryptoHash, types::BlockReference, views::AccessKeyP

pub type CliResult = color_eyre::eyre::Result<()>;

use inquire::{CustomUserError, Select};
use inquire::{CustomUserError, Select, Text};
use strum::IntoEnumIterator;

pub fn get_near_exec_path() -> String {
Expand Down Expand Up @@ -1900,7 +1900,29 @@ pub fn get_used_account_list(
Ok(VecDeque::new())
}

pub fn suggester(val: &str) -> Result<Vec<String>, CustomUserError> {
pub fn is_used_account_list_exist(credentials_home_dir: &std::path::PathBuf) -> bool {
let mut path = std::path::PathBuf::from(credentials_home_dir);
path.push("accounts.json");
path.exists()
}

pub fn input_account_id_from_used_account_list(
context: &crate::GlobalContext,
message: &str,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
let account_id = crate::types::account_id::AccountId::from_str(
&Text::new(message)
.with_autocomplete(&suggester)
.prompt()?,
)?;
update_used_account_list(
&context.config.credentials_home_dir,
account_id.clone().into(),
)?;
Ok(Some(account_id))
}

fn suggester(val: &str) -> Result<Vec<String>, CustomUserError> {
let val_lower = val.to_lowercase();

let config = get_config_toml()?;
Expand All @@ -1916,12 +1938,6 @@ pub fn suggester(val: &str) -> Result<Vec<String>, CustomUserError> {
.collect())
}

pub fn is_used_account_list_exist(credentials_home_dir: &std::path::PathBuf) -> bool {
let mut path = std::path::PathBuf::from(credentials_home_dir);
path.push("accounts.json");
path.exists()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 5fb1bc4

Please sign in to comment.