From 98ea9dccdf073bbdda459829c8461dc1fa1a18a8 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Mon, 3 Feb 2020 09:40:53 +0000 Subject: [PATCH] Removed duplicate code for overwriting config values --- nym-client/src/commands/init.rs | 22 ++-------------------- nym-client/src/commands/mod.rs | 27 +++++++++++++++++++++++++++ nym-client/src/commands/run.rs | 27 +++++---------------------- 3 files changed, 34 insertions(+), 42 deletions(-) diff --git a/nym-client/src/commands/init.rs b/nym-client/src/commands/init.rs index 6ced36ff39..82c8e3038e 100644 --- a/nym-client/src/commands/init.rs +++ b/nym-client/src/commands/init.rs @@ -1,5 +1,5 @@ +use crate::commands::overwrite_config; use crate::config::persistance::pathfinder::ClientPathfinder; -use crate::config::SocketType; use clap::ArgMatches; use config::NymConfig; use crypto::identity::MixIdentityKeyPair; @@ -11,25 +11,7 @@ pub fn execute(matches: &ArgMatches) { let id = matches.value_of("id").unwrap(); // required for now let mut config = crate::config::Config::new(id); - if let Some(directory) = matches.value_of("directory") { - config = config.with_custom_directory(directory); - } - - if let Some(provider_id) = matches.value_of("provider") { - config = config.with_provider_id(provider_id); - } - - if let Some(socket_type) = matches.value_of("socket-type") { - config = config.with_socket(SocketType::from_string(socket_type)); - } - - if let Some(port) = matches.value_of("port").map(|port| port.parse::()) { - if let Err(err) = port { - // if port was overridden, it must be parsable - panic!("Invalid port value provided - {:?}", err); - } - config = config.with_port(port.unwrap()); - } + config = overwrite_config(config, matches); let mix_identity_keys = MixIdentityKeyPair::new(); let pathfinder = ClientPathfinder::new_from_config(&config); diff --git a/nym-client/src/commands/mod.rs b/nym-client/src/commands/mod.rs index 0d06305e61..92e7c3dfe6 100644 --- a/nym-client/src/commands/mod.rs +++ b/nym-client/src/commands/mod.rs @@ -1,2 +1,29 @@ +use crate::config::{Config, SocketType}; +use clap::ArgMatches; + pub mod init; pub mod run; + +pub(crate) fn overwrite_config(mut config: Config, matches: &ArgMatches) -> Config { + if let Some(directory) = matches.value_of("directory") { + config = config.with_custom_directory(directory); + } + + if let Some(provider_id) = matches.value_of("provider") { + config = config.with_provider_id(provider_id); + } + + if let Some(socket_type) = matches.value_of("socket-type") { + config = config.with_socket(SocketType::from_string(socket_type)); + } + + if let Some(port) = matches.value_of("port").map(|port| port.parse::()) { + if let Err(err) = port { + // if port was overridden, it must be parsable + panic!("Invalid port value provided - {:?}", err); + } + config = config.with_port(port.unwrap()); + } + + config +} diff --git a/nym-client/src/commands/run.rs b/nym-client/src/commands/run.rs index 5a982b6daa..4b564b0918 100644 --- a/nym-client/src/commands/run.rs +++ b/nym-client/src/commands/run.rs @@ -1,4 +1,5 @@ use crate::client::NymClient; +use crate::commands::overwrite_config; use crate::config::persistance::pathfinder::ClientPathfinder; use crate::config::{Config, SocketType}; use clap::ArgMatches; @@ -8,31 +9,13 @@ use pemstore::pemstore::PemStore; pub fn execute(matches: &ArgMatches) { let id = matches.value_of("id").unwrap(); - let mut config_file = + let mut config = Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id)) .expect("Failed to load config file"); - if let Some(directory) = matches.value_of("directory") { - config_file = config_file.with_custom_directory(directory); - } + config = overwrite_config(config, matches); - if let Some(provider_id) = matches.value_of("provider") { - config_file = config_file.with_provider_id(provider_id); - } - - if let Some(socket_type) = matches.value_of("socket-type") { - config_file = config_file.with_socket(SocketType::from_string(socket_type)); - } - - if let Some(port) = matches.value_of("port").map(|port| port.parse::()) { - if let Err(err) = port { - // if port was overridden, it must be parsable - panic!("Invalid port value provided - {:?}", err); - } - config_file = config_file.with_port(port.unwrap()); - } - - let identity_keypair = PemStore::new(ClientPathfinder::new_from_config(&config_file)) + let identity_keypair = PemStore::new(ClientPathfinder::new_from_config(&config)) .read_identity() .expect("Failed to read stored identity key files"); @@ -41,7 +24,7 @@ pub fn execute(matches: &ArgMatches) { identity_keypair.public_key.to_base58_string() ); - let client = NymClient::new(config_file); + let client = NymClient::new(config); // // client.start().unwrap(); }