Running validator via commands::run::execute

This commit is contained in:
Jedrzej Stuczynski
2020-02-05 16:02:44 +00:00
parent 95d1912e18
commit da8365ee14
2 changed files with 59 additions and 27 deletions
+55
View File
@@ -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()
}
+4 -27
View File
@@ -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()),
}
}