store sampleconfig also in header for regenerating it if missing during runtime

This commit is contained in:
Lukas Heiligenbrunner 2020-05-01 16:56:37 +02:00
parent ae9aa1d423
commit 98f181fd6d
2 changed files with 14 additions and 27 deletions

View File

@ -54,13 +54,18 @@ include_directories(${LIBCONFIG_INCLUDE_DIR})
message("") message("")
#add version header
FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h
"\#pragma once\n#include <string>\nnamespace Version {\n const std::string VERSION = \"${PROJECT_VERSION}\";\n}"
)
#read sample config #read sample config
FILE(READ ${CMAKE_SOURCE_DIR}/config/iprefresher.cfg SAMPLECONFIG) FILE(READ ${CMAKE_SOURCE_DIR}/config/iprefresher.cfg SAMPLECONFIG)
#add version header
FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h
"\#pragma once
#include <string>
namespace Version {
const std::string VERSION = \"${PROJECT_VERSION}\";
const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\";
}"
)
# generate post script for checking if configuration already exists # generate post script for checking if configuration already exists
FILE(WRITE ${CMAKE_SOURCE_DIR}/postinstall.sh FILE(WRITE ${CMAKE_SOURCE_DIR}/postinstall.sh
"#!/bin/bash "#!/bin/bash
@ -72,18 +77,13 @@ fi"
add_library(api ${LIB_METHOD} add_library(api ${LIB_METHOD}
src/api/API.cpp src/api/API.cpp
src/api/TelegramAPI.cpp src/api/TelegramAPI.cpp
src/api/DynuAPI.cpp src/api/DynuAPI.cpp
src/api/IPAPI.cpp)
src/api/IPAPI.cpp
)
add_library(logger ${LIB_METHOD} add_library(logger ${LIB_METHOD}
src/FileLogger.cpp src/FileLogger.cpp
src/Logger.cpp src/Logger.cpp)
)
SET(SOURCE SET(SOURCE

View File

@ -9,6 +9,7 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <libconfig.h++> #include <libconfig.h++>
#include <Version.h>
std::string Config::dynuapikey; std::string Config::dynuapikey;
std::string Config::domainid; //id of the dynu domain std::string Config::domainid; //id of the dynu domain
@ -25,24 +26,10 @@ bool Config::readCredentials() {
catch (const libconfig::FileIOException &fioex) { catch (const libconfig::FileIOException &fioex) {
std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl; std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl;
std::string defaultconf = R"(# Dynu IP refresher config
# 2020
# Lukas Heiligenbrunner
## DYNU API Config
dynuapikey = ""
domainid = ""
domainname = ""
## Telegram API Config (optional)
#telegramApiKey = ""
#chatId = ""
)";
std::ofstream myfile; std::ofstream myfile;
myfile.open("/etc/iprefresher.cfg"); myfile.open("/etc/iprefresher.cfg");
if (myfile.is_open()) { if (myfile.is_open()) {
myfile << defaultconf; myfile << Version::SAMPLECONFIG;
myfile.close(); myfile.close();
} else { } else {
std::cout << "error creating file" << std::endl; std::cout << "error creating file" << std::endl;