Created Function to Reduce Duplicated Code

This commit is contained in:
Mark Mackey
2022-02-28 13:59:43 -06:00
parent f01e56d0fa
commit dbedc93707
5 changed files with 47 additions and 55 deletions

View File

@@ -13,3 +13,7 @@ dirs = "3.0.1"
eth2_network_config = { path = "../eth2_network_config" }
eth2_ssz = "0.4.1"
ethereum-types = "0.12.1"
serde = "1.0.116"
serde_json = "1.0.59"
serde_yaml = "0.8.13"
types = { path = "../../consensus/types"}

View File

@@ -6,6 +6,7 @@ use ethereum_types::U256 as Uint256;
use ssz::Decode;
use std::path::PathBuf;
use std::str::FromStr;
use types::{ChainSpec, Config, EthSpec};
pub mod flags;
@@ -163,3 +164,29 @@ pub fn parse_ssz_optional<T: Decode>(
})
.transpose()
}
/// Writes configs to file if `dump-config` or `dump-chain-config` flags are set
pub fn check_dump_configs<S, E>(
matches: &ArgMatches,
config: S,
spec: &ChainSpec,
) -> Result<(), String>
where
S: serde::Serialize,
E: EthSpec,
{
if let Some(dump_path) = parse_optional::<PathBuf>(matches, "dump-config")? {
let mut file = std::fs::File::create(dump_path)
.map_err(|e| format!("Failed to open file for writing config: {:?}", e))?;
serde_json::to_writer(&mut file, &config)
.map_err(|e| format!("Error serializing config: {:?}", e))?;
}
if let Some(dump_path) = parse_optional::<PathBuf>(matches, "dump-chain-config")? {
let chain_config = Config::from_chain_spec::<E>(spec);
let mut file = std::fs::File::create(dump_path)
.map_err(|e| format!("Failed to open file for writing chain config: {:?}", e))?;
serde_yaml::to_writer(&mut file, &chain_config)
.map_err(|e| format!("Error serializing config: {:?}", e))?;
}
Ok(())
}