Removed duplicate code for overwriting config values

This commit is contained in:
Jedrzej Stuczynski
2020-02-03 09:40:53 +00:00
parent 8747dc9d72
commit 98ea9dccdf
3 changed files with 34 additions and 42 deletions
+2 -20
View File
@@ -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::<u16>()) {
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);
+27
View File
@@ -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::<u16>()) {
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
}
+5 -22
View File
@@ -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::<u16>()) {
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();
}