use libconfig for a /etc/iprefresher.cfg config file

there can all the api credentials be specified
This commit is contained in:
2020-04-29 20:06:27 +02:00
parent 5499fadac8
commit 722d25b325
7 changed files with 91 additions and 12 deletions

View File

@ -3,14 +3,50 @@
//
#include <Credentials.h>
#include <iostream>
#include <cstring>
std::string Credentials::dynuapikey = "";
std::string Credentials::domainid = ""; //id of the dynu domain
std::string Credentials::domainname = "";
#include "libconfig.h++"
std::string Credentials::telegramApiKey = "";
std::string Credentials::chatId = "";
std::string Credentials::dynuapikey;
std::string Credentials::domainid; //id of the dynu domain
std::string Credentials::domainname;
bool Credentials::checkCredentialValidity() {
std::string Credentials::telegramApiKey;
std::string Credentials::chatId;
bool Credentials::readCredentials() {
libconfig::Config cfg;
try {
cfg.readFile("/etc/iprefresher.cfg");
}
catch (const libconfig::FileIOException &fioex) {
std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl;
cfg.writeFile("/etc/iprefresher.cfg");
return false;
}
catch (const libconfig::ParseException &pex) {
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << std::endl;
return false;
}
// Get the store name.
try {
// needed parameters
dynuapikey = (std::string) cfg.lookup("dynuapikey");
domainid = (std::string) cfg.lookup("domainid");
domainname = (std::string) cfg.lookup("domainname");
// optional parameters
telegramApiKey = (std::string) cfg.lookup("telegramApiKey");
chatId = (std::string) cfg.lookup("chatId");
std::cout << "Store name: " << dynuapikey << std::endl;
}
catch (const libconfig::SettingNotFoundException &nfex) {
// triggered if setting is missing in config
if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) {
std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl;
}
}
return !(Credentials::dynuapikey.empty() || Credentials::domainid.empty() || Credentials::domainname.empty());
}

View File

@ -5,7 +5,7 @@
#include <Credentials.h>
int main(int argc, char *argv[]) {
if (!Credentials::checkCredentialValidity()) {
if (!Credentials::readCredentials()) {
std::cout << "incorrect credentials!" << std::endl;
return -1;
}