Added commandline flags to genenerate and upgrade config files.

This commit is contained in:
Revertron
2021-04-03 14:57:56 +02:00
parent 6ae6852670
commit 7edc425bc6
4 changed files with 33 additions and 24 deletions
+24 -3
View File
@@ -43,7 +43,9 @@ fn main() {
opts.optflag("v", "verbose", "Show more debug messages"); opts.optflag("v", "verbose", "Show more debug messages");
opts.optflag("d", "debug", "Show trace messages, more than debug"); opts.optflag("d", "debug", "Show trace messages, more than debug");
opts.optflag("l", "list", "List blocks from DB and exit"); 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..]) { let opt_matches = match opts.parse(&args[1..]) {
Ok(m) => m, Ok(m) => m,
@@ -52,10 +54,29 @@ fn main() {
if opt_matches.opt_present("h") { if opt_matches.opt_present("h") {
let brief = format!("Usage: {} [options]", program); let brief = format!("Usage: {} [options]", program);
print!("{}", opts.usage(&brief)); println!("{}", opts.usage(&brief));
return; 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")] #[cfg(feature = "webgui")]
let no_gui = opt_matches.opt_present("n"); let no_gui = opt_matches.opt_present("n");
#[cfg(not(feature = "webgui"))] #[cfg(not(feature = "webgui"))]
@@ -79,7 +100,7 @@ fn main() {
.unwrap(); .unwrap();
info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION")); 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); info!(target: LOG_TARGET_MAIN, "Loaded settings: {:?}", &settings);
let keystore = Keystore::from_file(&settings.key_file, ""); let keystore = Keystore::from_file(&settings.key_file, "");
let chain: Chain = Chain::new(&settings); let chain: Chain = Chain::new(&settings);
+1 -1
View File
@@ -244,7 +244,7 @@ fn handle_connection_event(context: Arc<Mutex<Context>>, peers: &mut Peers, regi
Some(peer) => { Some(peer) => {
match peer.get_state().clone() { match peer.get_state().clone() {
State::Connecting => { State::Connecting => {
debug!("Sending hello to {}", &peer.get_addr()); debug!("Connected to peer {}, sending hello...", &peer.get_addr());
let data: String = { let data: String = {
let c = context.lock().unwrap(); let c = context.lock().unwrap();
let message = Message::hand(&c.app_version, &c.settings.origin, CHAIN_VERSION, c.settings.net.public, peer.get_rand()); let message = Message::hand(&c.app_version, &c.settings.origin, CHAIN_VERSION, c.settings.net.public, peer.get_rand());
+3 -3
View File
@@ -240,7 +240,7 @@ impl Peers {
if peer.get_state().need_reconnect() { if peer.get_state().need_reconnect() {
let addr = peer.get_addr(); let addr = peer.get_addr();
if let Ok(mut stream) = TcpStream::connect(addr.clone()) { 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(); registry.register(&mut stream, token.clone(), Interest::WRITABLE).unwrap();
peer.set_state(State::Connecting); peer.set_state(State::Connecting);
peer.inc_reconnects(); peer.inc_reconnects();
@@ -281,12 +281,12 @@ impl Peers {
return; return;
} }
if yggdrasil_only && !is_yggdrasil(&addr.ip()) { if yggdrasil_only && !is_yggdrasil(&addr.ip()) {
info!("Ignoring not Yggdrasil address '{}'", &addr.ip()); debug!("Ignoring not Yggdrasil address '{}'", &addr.ip());
return; return;
} }
if let Ok(mut stream) = TcpStream::connect(addr.clone()) { if let Ok(mut stream) = TcpStream::connect(addr.clone()) {
let token = next(unique_token); 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(); registry.register(&mut stream, token, Interest::WRITABLE).unwrap();
let mut peer = Peer::new(addr.clone(), stream, State::Connecting, false); let mut peer = Peer::new(addr.clone(), stream, State::Connecting, false);
peer.set_public(true); peer.set_public(true);
+5 -17
View File
@@ -1,5 +1,5 @@
use std::fs::File; use std::fs::File;
use std::io::{Read, Write}; use std::io::{Read,};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[allow(unused_imports)] #[allow(unused_imports)]
@@ -22,30 +22,18 @@ pub struct Settings {
} }
impl Settings { impl Settings {
pub fn new<S: Into<String>>(settings: S) -> serde_json::Result<Settings> { pub fn load(filename: &str) -> Option<Settings> {
serde_json::from_str(&settings.into())
}
pub fn load(filename: &str) -> Settings {
match File::open(filename) { match File::open(filename) {
Ok(mut file) => { Ok(mut file) => {
let mut text = String::new(); let mut text = String::new();
file.read_to_string(&mut text).unwrap(); file.read_to_string(&mut text).unwrap();
if let Ok(settings) = toml::from_str(&text) { if let Ok(settings) = toml::from_str(&text) {
return settings; return Some(settings);
} }
Settings::default() None
} }
Err(..) => { Err(..) => {
let settings = Settings::default(); None
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
} }
} }
} }