From 8ea9b14e9470554bcd2e119d32cbe048509b0879 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Wed, 5 Feb 2020 15:52:35 +0000 Subject: [PATCH] Init command for the validator --- validator/src/commands/init.rs | 38 ++++++++++++++++++++++++++++++++++ validator/src/main.rs | 17 +++++++-------- 2 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 validator/src/commands/init.rs diff --git a/validator/src/commands/init.rs b/validator/src/commands/init.rs new file mode 100644 index 0000000000..b46f24e4bd --- /dev/null +++ b/validator/src/commands/init.rs @@ -0,0 +1,38 @@ +use crate::commands::override_config; +use clap::{App, Arg, ArgMatches}; +use config::NymConfig; + +pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { + App::new("init") + .about("Initialise the validator") + .arg( + Arg::with_name("id") + .long("id") + .help("Id of the nym-mixnode we want to create config for.") + .takes_value(true) + .required(true), + ) + .arg( + Arg::with_name("directory") + .long("directory") + .help("Address of the directory server the validator is sending presence to") + .takes_value(true), + ) +} + +pub fn execute(matches: &ArgMatches) { + let id = matches.value_of("id").unwrap(); + println!("Initialising validator {}...", id); + + let mut config = crate::config::Config::new(id); + + config = override_config(config, matches); + + let config_save_location = config.get_config_file_save_location(); + config + .save_to_file(None) + .expect("Failed to save the config file"); + println!("Saved configuration file to {:?}", config_save_location); + + println!("Validator configuration completed.\n\n\n") +} diff --git a/validator/src/main.rs b/validator/src/main.rs index eba3636636..c7b71a3d7b 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -2,10 +2,11 @@ use crate::validator::Config; use crate::validator::Validator; use clap::{App, Arg, ArgMatches, SubCommand}; use log::*; -use std::process; use toml; pub mod built_info; +mod commands; +mod config; mod network; mod services; mod validator; @@ -18,6 +19,7 @@ fn main() { .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") @@ -31,10 +33,7 @@ fn main() { ) .get_matches(); - if let Err(e) = execute(arg_matches) { - error!("{:?}", e); - process::exit(1); - } + execute(arg_matches); } fn run(matches: &ArgMatches) { @@ -52,11 +51,11 @@ fn parse_config(matches: &ArgMatches) -> Config { toml::from_str(&config_content).unwrap() } - -fn execute(matches: ArgMatches) -> Result<(), String> { +fn execute(matches: ArgMatches) { match matches.subcommand() { - ("run", Some(m)) => Ok(run(m)), - _ => Err(usage()), + ("init", Some(m)) => commands::init::execute(m), + ("run", Some(m)) => run(m), + _ => println!("{}", usage()), } }