updated config management

This commit is contained in:
Paul Lecuq 2021-12-06 17:17:24 +01:00
parent 43cac4a821
commit b7a9def20d

View File

@ -85,6 +85,7 @@ pub struct Config {
pub server: String, pub server: String,
pub username: String, pub username: String,
pub password: String, pub password: String,
#[serde(skip_serializing)]
pub authtoken: Option<String>, pub authtoken: Option<String>,
pub sloweffect: Option<bool>, pub sloweffect: Option<bool>,
pub refresh: Option<u64>, pub refresh: Option<u64>,
@ -107,6 +108,7 @@ impl<'a> Config {
pub fn load(&'a mut self, configfile: &str) { pub fn load(&'a mut self, configfile: &str) {
let fileopen: Result<File, std::io::Error>; let fileopen: Result<File, std::io::Error>;
let filemeta = std::fs::metadata(configfile); let filemeta = std::fs::metadata(configfile);
let mut errorreading = false;
let fileexists = match filemeta { let fileexists = match filemeta {
Ok(_) => true, Ok(_) => true,
Err(_) => false, Err(_) => false,
@ -126,25 +128,29 @@ impl<'a> Config {
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
let parse: Result<JsonValue, JsonError> = serde_json::from_str(contents.as_str()); let parse: Result<JsonValue, JsonError> = serde_json::from_str(contents.as_str());
*self = match parse { let newcfg = match parse {
Ok(cfg) => { Ok(ncfg) => {
let tmpcfg = Config::new(); let tmpcfg = Config::new();
Config { Config {
server: cfg["server"].as_str().unwrap_or(tmpcfg.server.as_str()).to_string(), server: ncfg["server"].as_str().unwrap_or(tmpcfg.server.as_str()).to_string(),
username: cfg["username"].as_str().unwrap_or(tmpcfg.username.as_str()).to_string(), username: ncfg["username"].as_str().unwrap_or(tmpcfg.username.as_str()).to_string(),
password: cfg["password"].as_str().unwrap_or(tmpcfg.password.as_str()).to_string(), password: ncfg["password"].as_str().unwrap_or(tmpcfg.password.as_str()).to_string(),
authtoken: Some(cfg["authtoken"].as_str().unwrap_or(tmpcfg.authtoken.unwrap().as_str()).to_string()), authtoken: Some(ncfg["authtoken"].as_str().unwrap_or(self.authtoken.clone().unwrap().as_str()).to_string()),
sloweffect: Some(cfg["sloweffect"].as_bool().unwrap_or(tmpcfg.sloweffect.unwrap())), sloweffect: Some(ncfg["sloweffect"].as_bool().unwrap_or(tmpcfg.sloweffect.unwrap())),
refresh: Some(cfg["refresh"].as_u64().unwrap_or(tmpcfg.refresh.unwrap())), refresh: Some(ncfg["refresh"].as_u64().unwrap_or(tmpcfg.refresh.unwrap())),
limit: Some(cfg["limit"].as_u64().unwrap_or(tmpcfg.limit.unwrap())), limit: Some(ncfg["limit"].as_u64().unwrap_or(tmpcfg.limit.unwrap())),
} }
}, },
Err(err) => { Err(err) => {
errorreading = true;
println!("error occured: '{}'", err); println!("error occured: '{}'", err);
Config::new() Config::new()
} }
}; };
self.save(&configfile); *self = newcfg;
if !fileexists || errorreading {
self.save(&configfile);
}
} }
pub fn save(&self, configfile: &str) { pub fn save(&self, configfile: &str) {