From b2cd771a4203b07c3c19a3c914ed1016bab3edb6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 17:04:55 +1100 Subject: [PATCH] Shift argument passing into config module --- validator_client/src/config.rs | 37 ++++++++++++++++++++++++++++++++++ validator_client/src/main.rs | 37 ++-------------------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 49e0a506f..60edc564a 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -1,3 +1,5 @@ +use clap::ArgMatches; +use slog::{error, info}; use std::fs; use std::path::PathBuf; use types::ChainSpec; @@ -29,4 +31,39 @@ impl ClientConfig { spec, } } + + pub fn parse_args(matches: ArgMatches, log: &slog::Logger) -> Result { + let mut config = ClientConfig::default(); + // Custom datadir + if let Some(dir) = matches.value_of("datadir") { + config.data_dir = PathBuf::from(dir.to_string()); + } + + // Custom server port + if let Some(server_str) = matches.value_of("server") { + if let Ok(addr) = server_str.parse::() { + config.server = addr.to_string(); + } else { + error!(log, "Invalid address"; "server" => server_str); + return Err("Invalid address"); + } + } + + // TODO: Permit loading a custom spec from file. + // Custom spec + if let Some(spec_str) = matches.value_of("spec") { + match spec_str { + "foundation" => config.spec = ChainSpec::foundation(), + "few_validators" => config.spec = ChainSpec::few_validators(), + // Should be impossible due to clap's `possible_values(..)` function. + _ => unreachable!(), + }; + } + + // Log configuration + info!(log, ""; + "data_dir" => &config.data_dir.to_str(), + "server" => &config.server); + Ok(config) + } } diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index b4da737c8..c6cf586f3 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -13,12 +13,10 @@ use protos::services_grpc::{ AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, ValidatorServiceClient, }; -use slog::{error, info, o, Drain}; +use slog::{info, o, Drain}; use slot_clock::SystemTimeSlotClock; -use std::path::PathBuf; use std::sync::Arc; use std::thread; -use types::ChainSpec; mod attester_service; mod block_producer_service; @@ -63,38 +61,7 @@ fn main() { ) .get_matches(); - let mut config = ClientConfig::default(); - - // Custom datadir - if let Some(dir) = matches.value_of("datadir") { - config.data_dir = PathBuf::from(dir.to_string()); - } - - // Custom server port - if let Some(server_str) = matches.value_of("server") { - if let Ok(addr) = server_str.parse::() { - config.server = addr.to_string(); - } else { - error!(log, "Invalid address"; "server" => server_str); - return; - } - } - - // TODO: Permit loading a custom spec from file. - // Custom spec - if let Some(spec_str) = matches.value_of("spec") { - match spec_str { - "foundation" => config.spec = ChainSpec::foundation(), - "few_validators" => config.spec = ChainSpec::few_validators(), - // Should be impossible due to clap's `possible_values(..)` function. - _ => unreachable!(), - }; - } - - // Log configuration - info!(log, ""; - "data_dir" => &config.data_dir.to_str(), - "server" => &config.server); + let config = ClientConfig::parse_args(matches, &log).unwrap(); // Beacon node gRPC beacon node endpoints. let beacon_node_grpc_client = {