Reconcile config file with command line switches (#113)

* allowing mix of command line and config file values
* rustfmt check
This commit is contained in:
Yeastplume 2017-09-05 02:34:24 +01:00 committed by Ignotus Peverell
parent 66255dfe83
commit 301e9a6e98

View file

@ -50,7 +50,16 @@ fn start_from_config_file(mut global_config: GlobalConfig) {
global_config.config_file_path.unwrap().to_str().unwrap() global_config.config_file_path.unwrap().to_str().unwrap()
); );
global::set_mining_mode(global_config.members.as_mut().unwrap().server.clone().mining_parameter_mode.unwrap()); global::set_mining_mode(
global_config
.members
.as_mut()
.unwrap()
.server
.clone()
.mining_parameter_mode
.unwrap(),
);
grin::Server::start(global_config.members.as_mut().unwrap().server.clone()).unwrap(); grin::Server::start(global_config.members.as_mut().unwrap().server.clone()).unwrap();
loop { loop {
@ -61,6 +70,42 @@ fn start_from_config_file(mut global_config: GlobalConfig) {
fn main() { fn main() {
env_logger::init().unwrap(); env_logger::init().unwrap();
// First, load a global config object,
// then modify that object with any switches
// found so that the switches override the
// global config file
// This will return a global config object,
// which will either contain defaults for all // of the config structures or a
// configuration
// read from a config file
let mut global_config = GlobalConfig::new(None).unwrap_or_else(|e| {
panic!("Error parsing config file: {}", e);
});
if global_config.using_config_file {
info!(
"Using configuration file at: {}",
global_config
.config_file_path
.clone()
.unwrap()
.to_str()
.unwrap()
);
global::set_mining_mode(
global_config
.members
.as_mut()
.unwrap()
.server
.clone()
.mining_parameter_mode
.unwrap(),
);
}
let args = App::new("Grin") let args = App::new("Grin")
.version("0.1") .version("0.1")
.author("The Grin Team") .author("The Grin Team")
@ -74,7 +119,7 @@ fn main() {
.long("port") .long("port")
.help("Port to start the P2P server on") .help("Port to start the P2P server on")
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("api_port") .arg(Arg::with_name("api_port")
.short("a") .short("a")
.long("api_port") .long("api_port")
.help("Port on which to start the api server (e.g. transaction pool api)") .help("Port on which to start the api server (e.g. transaction pool api)")
@ -89,11 +134,11 @@ fn main() {
.short("m") .short("m")
.long("mine") .long("mine")
.help("Starts the debugging mining loop")) .help("Starts the debugging mining loop"))
.arg(Arg::with_name("wallet_url") .arg(Arg::with_name("wallet_url")
.short("w") .short("w")
.long("wallet_url") .long("wallet_url")
.help("A listening wallet receiver to which mining rewards will be sent") .help("A listening wallet receiver to which mining rewards will be sent")
.takes_value(true)) .takes_value(true))
.subcommand(SubCommand::with_name("start") .subcommand(SubCommand::with_name("start")
.about("Start the Grin server as a daemon")) .about("Start the Grin server as a daemon"))
.subcommand(SubCommand::with_name("stop") .subcommand(SubCommand::with_name("stop")
@ -118,7 +163,8 @@ fn main() {
.arg(Arg::with_name("data_dir") .arg(Arg::with_name("data_dir")
.short("dd") .short("dd")
.long("data_dir") .long("data_dir")
.help("Directory in which to store wallet files (defaults to current directory)") .help("Directory in which to store wallet files (defaults to current \
directory)")
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("port") .arg(Arg::with_name("port")
.short("r") .short("r")
@ -128,17 +174,23 @@ fn main() {
.arg(Arg::with_name("api_server_address") .arg(Arg::with_name("api_server_address")
.short("a") .short("a")
.long("api_server_address") .long("api_server_address")
.help("The api address of a running node on which to check inputs and post transactions") .help("The api address of a running node on which to check inputs and \
.takes_value(true)) post transactions")
.takes_value(true))
.subcommand(SubCommand::with_name("receive") .subcommand(SubCommand::with_name("receive")
.about("Run the wallet in receiving mode. If an input file is provided, will process it, otherwise runs in server mode waiting for send requests.") .about("Run the wallet in receiving mode. If an input file is \
provided, will process it, otherwise runs in server mode waiting \
for send requests.")
.arg(Arg::with_name("input") .arg(Arg::with_name("input")
.help("Partial transaction to receive, expects as a JSON file.") .help("Partial transaction to receive, expects as a JSON file.")
.short("i") .short("i")
.long("input") .long("input")
.takes_value(true))) .takes_value(true)))
.subcommand(SubCommand::with_name("send") .subcommand(SubCommand::with_name("send")
.about("Builds a transaction to send someone some coins. By default, the transaction will just be printed to stdout. If a destination is provided, the command will attempt to contact the receiver at that address and send the transaction directly.") .about("Builds a transaction to send someone some coins. By default, \
the transaction will just be printed to stdout. If a destination is \
provided, the command will attempt to contact the receiver at that \
address and send the transaction directly.")
.arg(Arg::with_name("amount") .arg(Arg::with_name("amount")
.help("Amount to send in the smallest denomination") .help("Amount to send in the smallest denomination")
.index(1)) .index(1))
@ -152,7 +204,7 @@ fn main() {
match args.subcommand() { match args.subcommand() {
// server commands and options // server commands and options
("server", Some(server_args)) => { ("server", Some(server_args)) => {
server_command(server_args); server_command(server_args, global_config);
} }
// client commands and options // client commands and options
@ -170,33 +222,18 @@ fn main() {
wallet_command(wallet_args); wallet_command(wallet_args);
} }
// If nothing is specified, try to load up and use a config file instead // If nothing is specified, try to just use the config file instead
// this should possibly become the way to configure most things // this could possibly become the way to configure most things
// with most command line options being phased out // with most command line options being phased out
_ => { _ => {
// This will return a global config object, if global_config.using_config_file {
// which will either contain defaults for all start_from_config_file(global_config);
// of the config structures or a configuration } else {
// read from a config file // won't attempt to just start with defaults,
// and will reject
let global_config = GlobalConfig::new(None); println!("Unknown command, and no configuration file was found.");
match global_config { println!("Use 'grin help' for a list of all commands.");
Ok(gc) => {
if gc.using_config_file {
start_from_config_file(gc);
} else {
// won't attempt to just start with defaults,
// and will reject
println!("Unknown command, and no configuration file was found.");
println!("Use 'grin help' for a list of all commands.");
}
}
Err(e) => {
println!("{}", e);
}
} }
} }
} }
} }
@ -205,11 +242,11 @@ fn main() {
/// stopping the Grin blockchain server. Processes all the command line /// stopping the Grin blockchain server. Processes all the command line
/// arguments /// arguments
/// to build a proper configuration and runs Grin with that configuration. /// to build a proper configuration and runs Grin with that configuration.
fn server_command(server_args: &ArgMatches) { fn server_command(server_args: &ArgMatches, global_config: GlobalConfig) {
info!("Starting the Grin server..."); info!("Starting the Grin server...");
// just get defaults from the global config // just get defaults from the global config
let mut server_config = GlobalConfig::default().members.unwrap().server; let mut server_config = global_config.members.unwrap().server;
if let Some(port) = server_args.value_of("port") { if let Some(port) = server_args.value_of("port") {
server_config.p2p_config.as_mut().unwrap().port = port.parse().unwrap(); server_config.p2p_config.as_mut().unwrap().port = port.parse().unwrap();
@ -237,10 +274,6 @@ fn server_command(server_args: &ArgMatches) {
server_config.seeds = Some(seeds.map(|s| s.to_string()).collect()); server_config.seeds = Some(seeds.map(|s| s.to_string()).collect());
} }
/*let mut sc = GlobalConfig::default();
sc.members.as_mut().unwrap().server = server_config.clone();
println!("{}", sc.ser_config().unwrap());*/
// start the server in the different run modes (interactive or daemon) // start the server in the different run modes (interactive or daemon)
match server_args.subcommand() { match server_args.subcommand() {
("run", _) => { ("run", _) => {