dep: rm confy as a dep (#10290)

Co-authored-by: Alexey Shekhirin <a.shekhirin@gmail.com>
This commit is contained in:
Thomas Coratger
2024-08-19 23:37:35 -07:00
committed by GitHub
parent 56e1448dbd
commit cd05a96fee
13 changed files with 187 additions and 49 deletions

View File

@@ -76,7 +76,6 @@ secp256k1 = { workspace = true, features = [
aquamarine.workspace = true
eyre.workspace = true
fdlimit.workspace = true
confy.workspace = true
rayon.workspace = true
# tracing

View File

@@ -117,7 +117,7 @@ impl LaunchContext {
pub fn load_toml_config(&self, config: &NodeConfig) -> eyre::Result<reth_config::Config> {
let config_path = config.config.clone().unwrap_or_else(|| self.data_dir.config());
let mut toml_config = confy::load_path::<reth_config::Config>(&config_path)
let mut toml_config = reth_config::Config::from_path(&config_path)
.wrap_err_with(|| format!("Could not load config file {config_path:?}"))?;
Self::save_pruning_config_if_full_node(&mut toml_config, config, &config_path)?;
@@ -970,12 +970,8 @@ mod tests {
)
.unwrap();
assert_eq!(
reth_config.prune.as_ref().map(|p| p.block_interval),
node_config.prune_config().map(|p| p.block_interval)
);
let loaded_config = Config::from_path(config_path).unwrap();
let loaded_config: Config = confy::load_path(config_path).unwrap();
assert_eq!(reth_config, loaded_config);
})
}

View File

@@ -51,6 +51,8 @@ humantime.workspace = true
const_format.workspace = true
rand.workspace = true
derive_more.workspace = true
toml.workspace = true
serde.workspace = true
# io
dirs-next = "2.0.0"
@@ -77,6 +79,7 @@ futures.workspace = true
# test vectors generation
proptest.workspace = true
tokio.workspace = true
tempfile.workspace = true
[features]
optimism = [

View File

@@ -8,10 +8,13 @@ use crate::{
dirs::{ChainPath, DataDirPath},
utils::get_single_header,
};
use eyre::eyre;
use reth_chainspec::{ChainSpec, MAINNET};
use reth_config::config::PruneConfig;
use reth_db_api::database::Database;
use reth_network_p2p::headers::client::HeadersClient;
use serde::{de::DeserializeOwned, Serialize};
use std::{fs, path::Path};
use reth_primitives::{
revm_primitives::EnvKzgSettings, BlockHashOrNumber, BlockNumber, Head, SealedHeader, B256,
@@ -365,6 +368,33 @@ impl NodeConfig {
pub fn datadir(&self) -> ChainPath<DataDirPath> {
self.datadir.clone().resolve_datadir(self.chain.chain)
}
/// Load an application configuration from a specified path.
///
/// A new configuration file is created with default values if none
/// exists.
pub fn load_path<T: Serialize + DeserializeOwned + Default>(
path: impl AsRef<Path>,
) -> eyre::Result<T> {
let path = path.as_ref();
match fs::read_to_string(path) {
Ok(cfg_string) => {
toml::from_str(&cfg_string).map_err(|e| eyre!("Failed to parse TOML: {e}"))
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.map_err(|e| eyre!("Failed to create directory: {e}"))?;
}
let cfg = T::default();
let s = toml::to_string_pretty(&cfg)
.map_err(|e| eyre!("Failed to serialize to TOML: {e}"))?;
fs::write(path, s).map_err(|e| eyre!("Failed to write configuration file: {e}"))?;
Ok(cfg)
}
Err(e) => Err(eyre!("Failed to load configuration: {e}")),
}
}
}
impl Default for NodeConfig {