From 5680355b3111081b3555789bca6c06e5f6e04650 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 27 Jul 2020 01:25:20 +0000 Subject: [PATCH] Fix naming of validators in CLI (#1332) ## Issue Addressed NA ## Proposed Changes - Adds a `lighthouse account validator list` command, to list all known validators. - Fixes the validator names; previously they were using a full path (e.g., `"/home/paul/.lighthouse/validators/0x8ce25415d078bdc83133758604578ba51707a55965eeca8982f44695db7432d6ff1c23529020a971faa68ab60baf3118"` but now we only use the final directory name (e.g., `0x8ce25415d078bdc83133758604578ba51707a55965eeca8982f44695db7432d6ff1c23529020a971faa68ab60baf3118`). --- account_manager/src/validator/list.rs | 30 +++++++++++++++++++++++++++ account_manager/src/validator/mod.rs | 3 +++ common/validator_dir/src/manager.rs | 12 ++++++----- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 account_manager/src/validator/list.rs diff --git a/account_manager/src/validator/list.rs b/account_manager/src/validator/list.rs new file mode 100644 index 000000000..b594a7295 --- /dev/null +++ b/account_manager/src/validator/list.rs @@ -0,0 +1,30 @@ +use crate::VALIDATOR_DIR_FLAG; +use clap::{App, ArgMatches}; +use std::path::PathBuf; +use validator_dir::Manager as ValidatorManager; + +pub const CMD: &str = "list"; + +pub fn cli_app<'a, 'b>() -> App<'a, 'b> { + App::new(CMD).about("Lists the names of all validators.") +} + +pub fn cli_run(matches: &ArgMatches<'_>) -> Result<(), String> { + let data_dir = clap_utils::parse_path_with_default_in_home_dir( + matches, + VALIDATOR_DIR_FLAG, + PathBuf::new().join(".lighthouse").join("validators"), + )?; + + let mgr = ValidatorManager::open(&data_dir) + .map_err(|e| format!("Unable to read --{}: {:?}", VALIDATOR_DIR_FLAG, e))?; + + for (name, _path) in mgr + .directory_names() + .map_err(|e| format!("Unable to list wallets: {:?}", e))? + { + println!("{}", name) + } + + Ok(()) +} diff --git a/account_manager/src/validator/mod.rs b/account_manager/src/validator/mod.rs index 25d0846f5..e4862df06 100644 --- a/account_manager/src/validator/mod.rs +++ b/account_manager/src/validator/mod.rs @@ -1,5 +1,6 @@ pub mod create; pub mod deposit; +pub mod list; use crate::common::base_wallet_dir; use clap::{App, Arg, ArgMatches}; @@ -20,6 +21,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { ) .subcommand(create::cli_app()) .subcommand(deposit::cli_app()) + .subcommand(list::cli_app()) } pub fn cli_run(matches: &ArgMatches, env: Environment) -> Result<(), String> { @@ -28,6 +30,7 @@ pub fn cli_run(matches: &ArgMatches, env: Environment) -> Result< match matches.subcommand() { (create::CMD, Some(matches)) => create::cli_run::(matches, env, base_wallet_dir), (deposit::CMD, Some(matches)) => deposit::cli_run::(matches, env), + (list::CMD, Some(matches)) => list::cli_run(matches), (unknown, _) => Err(format!( "{} does not have a {} command. See --help", CMD, unknown diff --git a/common/validator_dir/src/manager.rs b/common/validator_dir/src/manager.rs index b6f59fa28..77011570e 100644 --- a/common/validator_dir/src/manager.rs +++ b/common/validator_dir/src/manager.rs @@ -164,10 +164,12 @@ impl Manager { /// /// Returns an error if a directory is unable to be read. pub fn directory_names(&self) -> Result, Error> { - Ok(HashMap::from_iter( - self.iter_dir()? - .into_iter() - .map(|path| (format!("{:?}", path), path)), - )) + Ok(HashMap::from_iter(self.iter_dir()?.into_iter().filter_map( + |path| { + path.file_name() + .and_then(|os_string| os_string.to_str().map(|s| s.to_string())) + .map(|filename| (filename, path)) + }, + ))) } }