new option to verify syntax and parameters of config file

cdoc for logging class
This commit is contained in:
2020-05-01 15:33:20 +02:00
parent cf49bf7bc5
commit 2505e2cbf4
7 changed files with 106 additions and 11 deletions

View File

@ -3,11 +3,12 @@
//
#include <Config.h>
#include <Logger.h>
#include <iostream>
#include <cstring>
#include <fstream>
#include "libconfig.h++"
#include <libconfig.h++>
std::string Config::dynuapikey;
std::string Config::domainid; //id of the dynu domain
@ -40,14 +41,13 @@ domainname = ""
std::ofstream myfile;
myfile.open("/etc/iprefresher.cfg");
if(myfile.is_open()){
if (myfile.is_open()) {
myfile << defaultconf;
myfile.close();
} else {
std::cout << "error creating file" << std::endl;
}
return false;
}
catch (const libconfig::ParseException &pex) {
@ -56,7 +56,6 @@ domainname = ""
return false;
}
// Get the store name.
try {
// needed parameters
dynuapikey = (std::string) cfg.lookup("dynuapikey");
@ -75,3 +74,48 @@ domainname = ""
// check if needed values aren't empty
return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty());
}
bool Config::validateConfig() {
libconfig::Config cfg;
try {
Logger::message("reading config file");
cfg.readFile("/etc/iprefresher.cfg");
}
catch (const libconfig::FileIOException &fioex) {
Logger::warning("config file doesn't exist or permission denied!");
return false;
}
catch (const libconfig::ParseException &pex) {
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << std::endl;
return false;
}
Logger::message("Syntax and Permission is OK");
try {
// needed parameters
if (((std::string) cfg.lookup("dynuapikey")).empty()) {
Logger::warning("required parameter dynuapikey seems to be empty.");
return false;
}
if (((std::string) cfg.lookup("domainid")).empty()) {
Logger::warning("required parameter domainid seems to be empty.");
return false;
}
if (((std::string) cfg.lookup("domainname")).empty()) {
Logger::warning("required parameter domainname seems to be empty.");
return false;
}
// optional parameters
cfg.lookup("telegramApiKey");
cfg.lookup("chatId");
}
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 false;
}
}
return true;
}

View File

@ -39,6 +39,9 @@ void Logger::log(const std::string &message, int level) {
case Error:
out << "ERROR";
break;
default:
out << "UNDEFINED";
break;
}
out << "] ";
out << message;

View File

@ -3,10 +3,7 @@
//
#include "api/API.h"
#include "api/Hashmap.h"
#include <string>
#include <iostream>
#include <sstream>
#include <curl/curl.h>

View File

@ -2,12 +2,12 @@
// Created by lukas on 18.06.19.
//
#include <iostream>
#include <Version.h>
#include <IPRefresher.h>
#include <Logger.h>
#include <Config.h>
#include <iostream>
/**
* application entry point
*/
@ -19,6 +19,7 @@ int main(int argc, char *argv[]) {
<< "[-v] [--version] print the software version" << std::endl
<< "[-f] [--force] force refresh of ip" << std::endl
<< "[-l] [--loop] infinite loop to refresh ip every five minutes" << std::endl
<< "[-c] [--checkconfig] validate configuration" << std::endl
<< "[no argument] normal ip check and refresh" << std::endl;
} else if (firstarg == "-v" || firstarg == "--version") {
std::cout << "Version " << Version::VERSION << std::endl;
@ -32,6 +33,13 @@ int main(int argc, char *argv[]) {
} else if (firstarg == "-l" || firstarg == "--loop") {
IPRefresher(true);
} else if (firstarg == "-c" || firstarg == "--checkconfig") {
if (Config::validateConfig()) {
Logger::message("Config file is OK");
} else {
Logger::warning("There are errors in config file!");
return -1;
}
} else {
Logger::message("wrong arguments! -h for help");
}