From da8365ee14a9b1aea5271c15bc07fc3f2c518bc6 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Wed, 5 Feb 2020 16:02:44 +0000 Subject: [PATCH] Running validator via commands::run::execute --- validator/src/commands/run.rs | 55 +++++++++++++++++++++++++++++++++++ validator/src/main.rs | 31 +++----------------- 2 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 validator/src/commands/run.rs diff --git a/validator/src/commands/run.rs b/validator/src/commands/run.rs new file mode 100644 index 0000000000..bca11b7eef --- /dev/null +++ b/validator/src/commands/run.rs @@ -0,0 +1,55 @@ +use crate::commands::override_config; +use crate::config::Config; +use crate::validator; +use crate::validator::Validator; +use clap::{App, Arg, ArgMatches}; +use config::NymConfig; + +pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { + App::new("run") + .about("Starts the validator") + .arg( + Arg::with_name("id") + .long("id") + .help("Id of the nym-validator we want to run") + .takes_value(true) + .required(true), + ) + // the rest of arguments are optional, they are used to override settings in config file + .arg( + Arg::with_name("config") + .long("config") + .help("Custom path to the nym-validator configuration file") + .takes_value(true), + ) + .arg( + Arg::with_name("directory") + .long("directory") + .help("Address of the directory server the validator is sending presence to") + .takes_value(true), + ) +} + +fn parse_old_config(matches: &ArgMatches) -> validator::Config { + let config_file_path = matches.value_of("config").unwrap(); + // since this is happening at the very startup, it's fine to panic if file doesn't exist + let config_content = std::fs::read_to_string(config_file_path).unwrap(); + toml::from_str(&config_content).unwrap() +} + +pub fn execute(matches: &ArgMatches) { + let id = matches.value_of("id").unwrap(); + + println!("Starting sfw-provider {}...", id); + + let mut config = + Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id)) + .expect("Failed to load config file"); + + config = override_config(config, matches); + + let old_config = parse_old_config(matches); + + let validator = Validator::new(old_config); + validator.start() +} diff --git a/validator/src/main.rs b/validator/src/main.rs index c7b71a3d7b..4a12f05e5e 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -15,46 +15,23 @@ fn main() { dotenv::dotenv().ok(); pretty_env_logger::init(); + println!("{}", banner()); + let arg_matches = App::new("Nym Validator") .version(built_info::PKG_VERSION) .author("Nymtech") .about("Implementation of Nym Validator") .subcommand(commands::init::command_args()) - .subcommand( - SubCommand::with_name("run") - .about("Starts the validator") - .arg( - Arg::with_name("config") - .long("config") - .help("Location of the validator configuration file") - .takes_value(true) - .required(true), - ), - ) + .subcommand(commands::run::command_args()) .get_matches(); execute(arg_matches); } -fn run(matches: &ArgMatches) { - let config = parse_config(matches); - trace!("read config: {:?}", config); - - let validator = Validator::new(config); - validator.start() -} - -fn parse_config(matches: &ArgMatches) -> Config { - let config_file_path = matches.value_of("config").unwrap(); - // since this is happening at the very startup, it's fine to panic if file doesn't exist - let config_content = std::fs::read_to_string(config_file_path).unwrap(); - toml::from_str(&config_content).unwrap() -} - fn execute(matches: ArgMatches) { match matches.subcommand() { ("init", Some(m)) => commands::init::execute(m), - ("run", Some(m)) => run(m), + ("run", Some(m)) => commands::run::execute(m), _ => println!("{}", usage()), } }