Added commandline flags to genenerate and upgrade config files.
This commit is contained in:
+24
-3
@@ -43,7 +43,9 @@ fn main() {
|
||||
opts.optflag("v", "verbose", "Show more debug messages");
|
||||
opts.optflag("d", "debug", "Show trace messages, more than debug");
|
||||
opts.optflag("l", "list", "List blocks from DB and exit");
|
||||
opts.optopt("c", "config", "Path to config file", "");
|
||||
opts.optflag("g", "generate", "Generate new config file. Generated config will be printed to console.");
|
||||
opts.optopt("c", "config", "Path to config file", "FILE");
|
||||
opts.optopt("u", "upgrade", "Path to config file that you want to upgrade. Upgraded config will be printed to console.", "FILE");
|
||||
|
||||
let opt_matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => m,
|
||||
@@ -52,10 +54,29 @@ fn main() {
|
||||
|
||||
if opt_matches.opt_present("h") {
|
||||
let brief = format!("Usage: {} [options]", program);
|
||||
print!("{}", opts.usage(&brief));
|
||||
println!("{}", opts.usage(&brief));
|
||||
return;
|
||||
}
|
||||
|
||||
if opt_matches.opt_present("g") {
|
||||
println!("{}", include_str!("../alfis.toml"));
|
||||
return;
|
||||
}
|
||||
|
||||
match opt_matches.opt_str("u") {
|
||||
None => {}
|
||||
Some(path) => {
|
||||
if let Some(settings) = Settings::load(&path) {
|
||||
let string = toml::to_string(&settings).unwrap();
|
||||
println!("{}", &string);
|
||||
return;
|
||||
} else {
|
||||
println!("Error loading config for upgrade!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(feature = "webgui")]
|
||||
let no_gui = opt_matches.opt_present("n");
|
||||
#[cfg(not(feature = "webgui"))]
|
||||
@@ -79,7 +100,7 @@ fn main() {
|
||||
.unwrap();
|
||||
info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let settings = Settings::load(&config_name);
|
||||
let settings = Settings::load(&config_name).expect(&format!("Cannot load settings from {}!", &config_name));
|
||||
info!(target: LOG_TARGET_MAIN, "Loaded settings: {:?}", &settings);
|
||||
let keystore = Keystore::from_file(&settings.key_file, "");
|
||||
let chain: Chain = Chain::new(&settings);
|
||||
|
||||
+1
-1
@@ -244,7 +244,7 @@ fn handle_connection_event(context: Arc<Mutex<Context>>, peers: &mut Peers, regi
|
||||
Some(peer) => {
|
||||
match peer.get_state().clone() {
|
||||
State::Connecting => {
|
||||
debug!("Sending hello to {}", &peer.get_addr());
|
||||
debug!("Connected to peer {}, sending hello...", &peer.get_addr());
|
||||
let data: String = {
|
||||
let c = context.lock().unwrap();
|
||||
let message = Message::hand(&c.app_version, &c.settings.origin, CHAIN_VERSION, c.settings.net.public, peer.get_rand());
|
||||
|
||||
+3
-3
@@ -240,7 +240,7 @@ impl Peers {
|
||||
if peer.get_state().need_reconnect() {
|
||||
let addr = peer.get_addr();
|
||||
if let Ok(mut stream) = TcpStream::connect(addr.clone()) {
|
||||
info!("Created connection to peer {}", &addr);
|
||||
debug!("Trying to connect to peer {}", &addr);
|
||||
registry.register(&mut stream, token.clone(), Interest::WRITABLE).unwrap();
|
||||
peer.set_state(State::Connecting);
|
||||
peer.inc_reconnects();
|
||||
@@ -281,12 +281,12 @@ impl Peers {
|
||||
return;
|
||||
}
|
||||
if yggdrasil_only && !is_yggdrasil(&addr.ip()) {
|
||||
info!("Ignoring not Yggdrasil address '{}'", &addr.ip());
|
||||
debug!("Ignoring not Yggdrasil address '{}'", &addr.ip());
|
||||
return;
|
||||
}
|
||||
if let Ok(mut stream) = TcpStream::connect(addr.clone()) {
|
||||
let token = next(unique_token);
|
||||
info!("Created connection {} to peer {}", &token.0, &addr);
|
||||
debug!("Trying to reconnect connection {}, to peer {}", &token.0, &addr);
|
||||
registry.register(&mut stream, token, Interest::WRITABLE).unwrap();
|
||||
let mut peer = Peer::new(addr.clone(), stream, State::Connecting, false);
|
||||
peer.set_public(true);
|
||||
|
||||
+5
-17
@@ -1,5 +1,5 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::io::{Read,};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[allow(unused_imports)]
|
||||
@@ -22,30 +22,18 @@ pub struct Settings {
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new<S: Into<String>>(settings: S) -> serde_json::Result<Settings> {
|
||||
serde_json::from_str(&settings.into())
|
||||
}
|
||||
|
||||
pub fn load(filename: &str) -> Settings {
|
||||
pub fn load(filename: &str) -> Option<Settings> {
|
||||
match File::open(filename) {
|
||||
Ok(mut file) => {
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text).unwrap();
|
||||
if let Ok(settings) = toml::from_str(&text) {
|
||||
return settings;
|
||||
return Some(settings);
|
||||
}
|
||||
Settings::default()
|
||||
None
|
||||
}
|
||||
Err(..) => {
|
||||
let settings = Settings::default();
|
||||
let string = toml::to_string(&settings).unwrap();
|
||||
match File::create(filename) {
|
||||
Ok(mut f) => {
|
||||
f.write_all(string.as_bytes()).expect("Error saving settings!");
|
||||
}
|
||||
Err(_) => { error!("Error saving settings file!"); }
|
||||
}
|
||||
settings
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user