Allow testnet command to overwrite files (#1045)

This commit is contained in:
Age Manning 2020-04-23 19:01:16 +10:00 committed by GitHub
parent 91648cc230
commit 6784a8b42a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 9 deletions

View File

@ -64,9 +64,11 @@ impl<E: EthSpec> Eth2TestnetConfig<E> {
}) })
} }
// Write the files to the directory, only if the directory doesn't already exist. // Write the files to the directory.
pub fn write_to_file(&self, base_dir: PathBuf) -> Result<(), String> { //
if base_dir.exists() { // Overwrites files if specified to do so.
pub fn write_to_file(&self, base_dir: PathBuf, overwrite: bool) -> Result<(), String> {
if base_dir.exists() && !overwrite {
return Err("Testnet directory already exists".to_string()); return Err("Testnet directory already exists".to_string());
} }
@ -252,7 +254,7 @@ mod tests {
}; };
testnet testnet
.write_to_file(base_dir.clone()) .write_to_file(base_dir.clone(), false)
.expect("should write to file"); .expect("should write to file");
let decoded = Eth2TestnetConfig::load(base_dir).expect("should load struct"); let decoded = Eth2TestnetConfig::load(base_dir).expect("should load struct");

View File

@ -254,6 +254,13 @@ fn main() {
"Produce a new testnet directory. If any of the optional flags are not "Produce a new testnet directory. If any of the optional flags are not
supplied the values will remain the default for the --spec flag", supplied the values will remain the default for the --spec flag",
) )
.arg(
Arg::with_name("force")
.long("force")
.short("f")
.takes_value(false)
.help("Overwrites any previous testnet configurations"),
)
.arg( .arg(
Arg::with_name("min-genesis-time") Arg::with_name("min-genesis-time")
.long("min-genesis-time") .long("min-genesis-time")

View File

@ -15,11 +15,15 @@ pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let deposit_contract_address: Address = parse_required(matches, "deposit-contract-address")?; let deposit_contract_address: Address = parse_required(matches, "deposit-contract-address")?;
let deposit_contract_deploy_block = parse_required(matches, "deposit-contract-deploy-block")?; let deposit_contract_deploy_block = parse_required(matches, "deposit-contract-deploy-block")?;
let overwrite_files = matches.is_present("force");
if testnet_dir_path.exists() { if testnet_dir_path.exists() {
return Err(format!( if !overwrite_files {
"{:?} already exists, will not overwrite", return Err(format!(
testnet_dir_path "{:?} already exists, will not overwrite. Use --force to overwrite",
)); testnet_dir_path
));
}
} }
let mut spec = T::default_spec(); let mut spec = T::default_spec();
@ -57,5 +61,5 @@ pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
yaml_config: Some(YamlConfig::from_spec::<T>(&spec)), yaml_config: Some(YamlConfig::from_spec::<T>(&spec)),
}; };
testnet.write_to_file(testnet_dir_path) testnet.write_to_file(testnet_dir_path, overwrite_files)
} }