Skip to content

Commit

Permalink
add ENV variable to disable config generation and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lionkor committed Dec 29, 2023
1 parent d07c4aa commit 1479bda
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Env {
enum class Key {
// provider settings
PROVIDER_UPDATE_MESSAGE,
PROVIDER_DISABLE_CONFIG,
};

std::optional<std::string> Get(Key key);
Expand Down
1 change: 1 addition & 0 deletions include/TConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ class TConfig {
std::string TagsAsPrettyArray() const;
bool IsDefault();
bool mFailed { false };
bool mDisableConfig { false };
std::string mConfigFileName;
};
3 changes: 3 additions & 0 deletions src/Env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ std::string_view Env::ToString(Env::Key key) {
case Key::PROVIDER_UPDATE_MESSAGE:
return "BEAMMP_PROVIDER_UPDATE_MESSAGE";
break;
case Key::PROVIDER_DISABLE_CONFIG:
return "BEAMMP_PROVIDER_DISABLE_CONFIG";
break;
}
return "";
}
38 changes: 32 additions & 6 deletions src/TConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Common.h"

#include "Env.h"
#include "TConfig.h"
#include <cstdlib>
#include <fstream>
Expand Down Expand Up @@ -69,7 +70,9 @@ TEST_CASE("TConfig::TConfig") {
TConfig::TConfig(const std::string& ConfigFileName)
: mConfigFileName(ConfigFileName) {
Application::SetSubsystemStatus("Config", Application::Status::Starting);
if (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName)) {
auto DisableConfig = Env::Get(Env::Key::PROVIDER_DISABLE_CONFIG).value_or("false");
mDisableConfig = DisableConfig == "true" || DisableConfig == "1";
if (!mDisableConfig && (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName))) {
beammp_info("No config file found! Generating one...");
CreateConfigFile();
}
Expand Down Expand Up @@ -143,7 +146,9 @@ void TConfig::FlushToFile() {

void TConfig::CreateConfigFile() {
// build from old config Server.cfg

if (mDisableConfig) {
return;
}
try {
if (fs::exists("Server.cfg")) {
// parse it (this is weird and bad and should be removed in some future version)
Expand All @@ -163,6 +168,9 @@ void TConfig::TryReadValue(toml::value& Table, const std::string& Category, cons
return;
}
}
if (mDisableConfig) {
return;
}
if (Table[Category.c_str()][Key.data()].is_string()) {
OutValue = Table[Category.c_str()][Key.data()].as_string();
}
Expand All @@ -176,6 +184,9 @@ void TConfig::TryReadValue(toml::value& Table, const std::string& Category, cons
return;
}
}
if (mDisableConfig) {
return;
}
if (Table[Category.c_str()][Key.data()].is_boolean()) {
OutValue = Table[Category.c_str()][Key.data()].as_boolean();
}
Expand All @@ -188,14 +199,20 @@ void TConfig::TryReadValue(toml::value& Table, const std::string& Category, cons
return;
}
}
if (mDisableConfig) {
return;
}
if (Table[Category.c_str()][Key.data()].is_integer()) {
OutValue = int(Table[Category.c_str()][Key.data()].as_integer());
}
}

void TConfig::ParseFromFile(std::string_view name) {
try {
toml::value data = toml::parse<toml::preserve_comments>(name.data());
toml::value data {};
if (!mDisableConfig) {
data = toml::parse<toml::preserve_comments>(name.data());
}
// GENERAL
TryReadValue(data, "General", StrDebug, EnvStrDebug, Application::Settings.DebugModeEnabled);
TryReadValue(data, "General", StrPrivate, EnvStrPrivate, Application::Settings.Private);
Expand Down Expand Up @@ -223,10 +240,16 @@ void TConfig::ParseFromFile(std::string_view name) {
PrintDebug();

// Update in any case
FlushToFile();
if (!mDisableConfig) {
FlushToFile();
}
// all good so far, let's check if there's a key
if (Application::Settings.Key.empty()) {
beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server.");
if (mDisableConfig) {
beammp_error("No AuthKey specified in the environment.");
} else {
beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server.");
}
Application::SetSubsystemStatus("Config", Application::Status::Bad);
mFailed = true;
return;
Expand All @@ -238,6 +261,9 @@ void TConfig::ParseFromFile(std::string_view name) {
}

void TConfig::PrintDebug() {
if (mDisableConfig) {
beammp_debug("Provider turned off the generation and parsing of the ServerConfig.toml");
}
beammp_debug(std::string(StrDebug) + ": " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false"));
beammp_debug(std::string(StrPrivate) + ": " + std::string(Application::Settings.Private ? "true" : "false"));
beammp_debug(std::string(StrPort) + ": " + std::to_string(Application::Settings.Port));
Expand Down Expand Up @@ -313,6 +339,6 @@ std::string TConfig::TagsAsPrettyArray() const {
for (size_t i = 0; i < TagsArray.size() - 1; ++i) {
Pretty += '\"' + TagsArray[i] + "\", ";
}
Pretty += '\"' + TagsArray.at(TagsArray.size()-1) + "\"";
Pretty += '\"' + TagsArray.at(TagsArray.size() - 1) + "\"";
return Pretty;
}

0 comments on commit 1479bda

Please sign in to comment.