Skip to content

Commit a174d67

Browse files
committed
remove ensure_dir_exists (2 deps saved)
1 parent b1a19a8 commit a174d67

File tree

9 files changed

+19
-32
lines changed

9 files changed

+19
-32
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

account_manager/src/validator/create.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ use account_utils::{
66
};
77
use clap::{Arg, ArgAction, ArgMatches, Command};
88
use clap_utils::FLAG_HEADER;
9-
use directory::{
10-
ensure_dir_exists, parse_path_or_default_with_flag, DEFAULT_SECRET_DIR, DEFAULT_WALLET_DIR,
11-
};
9+
use directory::{parse_path_or_default_with_flag, DEFAULT_SECRET_DIR, DEFAULT_WALLET_DIR};
1210
use environment::Environment;
1311
use eth2_wallet_manager::WalletManager;
1412
use slashing_protection::{SlashingDatabase, SLASHING_PROTECTION_FILENAME};
1513
use std::ffi::OsStr;
1614
use std::fs;
15+
use std::fs::create_dir_all;
1716
use std::path::{Path, PathBuf};
1817
use types::EthSpec;
1918
use validator_dir::Builder as ValidatorDirBuilder;
@@ -156,8 +155,10 @@ pub fn cli_run<E: EthSpec>(
156155
));
157156
}
158157

159-
ensure_dir_exists(&validator_dir)?;
160-
ensure_dir_exists(&secrets_dir)?;
158+
create_dir_all(&validator_dir)
159+
.map_err(|e| format!("Could not create validator dir at {validator_dir:?}: {e:?}"))?;
160+
create_dir_all(&secrets_dir)
161+
.map_err(|e| format!("Could not create secrets dir at {secrets_dir:?}: {e:?}"))?;
161162

162163
eprintln!("secrets-dir path {:?}", secrets_dir);
163164
eprintln!("wallets-dir path {:?}", wallet_base_dir);

account_manager/src/validator/recover.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use account_utils::eth2_keystore::{keypair_from_secret, Keystore, KeystoreBuilde
55
use account_utils::{random_password, read_mnemonic_from_cli, STDIN_INPUTS_FLAG};
66
use clap::{Arg, ArgAction, ArgMatches, Command};
77
use clap_utils::FLAG_HEADER;
8-
use directory::ensure_dir_exists;
98
use directory::{parse_path_or_default_with_flag, DEFAULT_SECRET_DIR};
109
use eth2_wallet::bip39::Seed;
1110
use eth2_wallet::{recover_validator_secret_from_mnemonic, KeyType, ValidatorKeystores};
11+
use std::fs::create_dir_all;
1212
use std::path::PathBuf;
1313
use validator_dir::Builder as ValidatorDirBuilder;
1414
pub const CMD: &str = "recover";
@@ -91,8 +91,10 @@ pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), Strin
9191

9292
eprintln!("secrets-dir path: {:?}", secrets_dir);
9393

94-
ensure_dir_exists(&validator_dir)?;
95-
ensure_dir_exists(&secrets_dir)?;
94+
create_dir_all(&validator_dir)
95+
.map_err(|e| format!("Could not create validator dir at {validator_dir:?}: {e:?}"))?;
96+
create_dir_all(&secrets_dir)
97+
.map_err(|e| format!("Could not create secrets dir at {secrets_dir:?}: {e:?}"))?;
9698

9799
eprintln!();
98100
eprintln!("WARNING: KEY RECOVERY CAN LEAD TO DUPLICATING VALIDATORS KEYS, WHICH CAN LEAD TO SLASHING.");

account_manager/src/wallet/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pub mod recover;
55
use crate::WALLETS_DIR_FLAG;
66
use clap::{Arg, ArgAction, ArgMatches, Command};
77
use clap_utils::FLAG_HEADER;
8-
use directory::{ensure_dir_exists, parse_path_or_default_with_flag, DEFAULT_WALLET_DIR};
8+
use directory::{parse_path_or_default_with_flag, DEFAULT_WALLET_DIR};
9+
use std::fs::create_dir_all;
910
use std::path::PathBuf;
1011

1112
pub const CMD: &str = "wallet";
@@ -44,7 +45,7 @@ pub fn cli_run(matches: &ArgMatches) -> Result<(), String> {
4445
} else {
4546
parse_path_or_default_with_flag(matches, WALLETS_DIR_FLAG, DEFAULT_WALLET_DIR)?
4647
};
47-
ensure_dir_exists(&wallet_base_dir)?;
48+
create_dir_all(&wallet_base_dir).map_err(|_| "Could not create wallet base dir")?;
4849

4950
eprintln!("wallet-dir path: {:?}", wallet_base_dir);
5051

common/account_utils/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ edition = { workspace = true }
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
directory = { workspace = true }
109
eth2_keystore = { workspace = true }
1110
eth2_wallet = { workspace = true }
1211
filesystem = { workspace = true }

common/account_utils/src/validator_definitions.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
//! attempt) to load into the `crate::intialized_validators::InitializedValidators` struct.
55
66
use crate::{default_keystore_password_path, read_password_string, write_file_via_temporary};
7-
use directory::ensure_dir_exists;
87
use eth2_keystore::Keystore;
98
use regex::Regex;
109
use serde::{Deserialize, Serialize};
1110
use slog::{error, Logger};
1211
use std::collections::HashSet;
13-
use std::fs::{self, File};
12+
use std::fs::{self, create_dir_all, File};
1413
use std::io;
1514
use std::path::{Path, PathBuf};
1615
use types::{graffiti::GraffitiString, Address, PublicKey};
@@ -229,7 +228,7 @@ impl From<Vec<ValidatorDefinition>> for ValidatorDefinitions {
229228
impl ValidatorDefinitions {
230229
/// Open an existing file or create a new, empty one if it does not exist.
231230
pub fn open_or_create<P: AsRef<Path>>(validators_dir: P) -> Result<Self, Error> {
232-
ensure_dir_exists(validators_dir.as_ref()).map_err(|_| {
231+
create_dir_all(validators_dir.as_ref()).map_err(|_| {
233232
Error::UnableToCreateValidatorDir(PathBuf::from(validators_dir.as_ref()))
234233
})?;
235234
let config_path = validators_dir.as_ref().join(CONFIG_FILENAME);

common/directory/src/lib.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clap::ArgMatches;
22
pub use eth2_network_config::DEFAULT_HARDCODED_NETWORK;
3-
use std::fs::{self, create_dir_all};
3+
use std::fs;
44
use std::path::{Path, PathBuf};
55

66
/// Names for the default directories.
@@ -30,17 +30,6 @@ pub fn get_network_dir(matches: &ArgMatches) -> String {
3030
}
3131
}
3232

33-
/// Checks if a directory exists in the given path and creates a directory if it does not exist.
34-
pub fn ensure_dir_exists<P: AsRef<Path>>(path: P) -> Result<(), String> {
35-
let path = path.as_ref();
36-
37-
if !path.exists() {
38-
create_dir_all(path).map_err(|e| format!("Unable to create {:?}: {:?}", path, e))?;
39-
}
40-
41-
Ok(())
42-
}
43-
4433
/// If `arg` is in `matches`, parses the value as a path.
4534
///
4635
/// Otherwise, attempts to find the default directory for the `testnet` from the `matches`.

common/validator_dir/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ insecure_keys = []
1212
bls = { workspace = true }
1313
deposit_contract = { workspace = true }
1414
derivative = { workspace = true }
15-
directory = { workspace = true }
1615
eth2_keystore = { workspace = true }
1716
filesystem = { workspace = true }
1817
hex = { workspace = true }

common/validator_dir/src/builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{Error as DirError, ValidatorDir};
22
use bls::get_withdrawal_credentials;
33
use deposit_contract::{encode_eth1_tx_data, Error as DepositError};
4-
use directory::ensure_dir_exists;
54
use eth2_keystore::{Error as KeystoreError, Keystore, KeystoreBuilder, PlainText};
65
use filesystem::create_with_600_perms;
76
use rand::{distributions::Alphanumeric, Rng};
@@ -42,7 +41,7 @@ pub enum Error {
4241
#[cfg(feature = "insecure_keys")]
4342
InsecureKeysError(String),
4443
MissingPasswordDir,
45-
UnableToCreatePasswordDir(String),
44+
UnableToCreatePasswordDir(io::Error),
4645
}
4746

4847
impl From<KeystoreError> for Error {
@@ -163,7 +162,7 @@ impl<'a> Builder<'a> {
163162
}
164163

165164
if let Some(password_dir) = &self.password_dir {
166-
ensure_dir_exists(password_dir).map_err(Error::UnableToCreatePasswordDir)?;
165+
create_dir_all(password_dir).map_err(Error::UnableToCreatePasswordDir)?;
167166
}
168167

169168
// The withdrawal keystore must be initialized in order to store it or create an eth1

0 commit comments

Comments
 (0)