Update source/src/config.cpp
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
#include "utils/directories.hpp"
|
#include "utils/directories.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <set>
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "utils/utils.hpp"
|
#include "utils/utils.hpp"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include "utils/execute.hpp"
|
#include "utils/execute.hpp"
|
||||||
#include "output.hpp"
|
#include "utils/output.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
@@ -41,6 +42,72 @@ bool config::load_config() { // load json config file.
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate the config format - check for required and supported fields
|
||||||
|
std::set<std::string> allowed_fields = {
|
||||||
|
"server_definition_paths",
|
||||||
|
"template_local_paths",
|
||||||
|
"template_registries",
|
||||||
|
"backups_path"
|
||||||
|
};
|
||||||
|
|
||||||
|
std::set<std::string> deprecated_fields = {
|
||||||
|
"template_registry_URLs",
|
||||||
|
"template_upload_token"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check for deprecated fields
|
||||||
|
for (const auto& field : deprecated_fields) {
|
||||||
|
if (mConfig.contains(field)) {
|
||||||
|
error << "Config file contains deprecated field '" << field << "'" << std::endl;
|
||||||
|
error << "Please update your config file to the new format." << std::endl;
|
||||||
|
if (field == "template_registry_URLs") {
|
||||||
|
error << "Replace 'template_registry_URLs' with 'template_registries' using the format:" << std::endl;
|
||||||
|
error << " \"template_registries\": [" << std::endl;
|
||||||
|
error << " {" << std::endl;
|
||||||
|
error << " \"name\": \"main\"," << std::endl;
|
||||||
|
error << " \"url\": \"https://templates.dropshell.app\"," << std::endl;
|
||||||
|
error << " \"token\": \"\"" << std::endl;
|
||||||
|
error << " }" << std::endl;
|
||||||
|
error << " ]" << std::endl;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for unknown fields
|
||||||
|
for (auto& [key, value] : mConfig.items()) {
|
||||||
|
if (allowed_fields.find(key) == allowed_fields.end()) {
|
||||||
|
error << "Config file contains unknown field '" << key << "'" << std::endl;
|
||||||
|
error << "Allowed fields are: ";
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& field : allowed_fields) {
|
||||||
|
if (!first) error << ", ";
|
||||||
|
error << field;
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
error << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate template_registries format if present
|
||||||
|
if (mConfig.contains("template_registries")) {
|
||||||
|
if (!mConfig["template_registries"].is_array()) {
|
||||||
|
error << "'template_registries' must be an array" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (const auto& registry : mConfig["template_registries"]) {
|
||||||
|
if (!registry.is_object()) {
|
||||||
|
error << "Each registry in 'template_registries' must be an object" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!registry.contains("name") || !registry.contains("url")) {
|
||||||
|
error << "Each registry must have 'name' and 'url' fields" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mIsConfigSet = true;
|
mIsConfigSet = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -77,11 +144,13 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
mConfig["template_local_paths"] = {
|
mConfig["template_local_paths"] = {
|
||||||
dropshell_base + "/local_templates"
|
dropshell_base + "/local_templates"
|
||||||
};
|
};
|
||||||
mConfig["template_registry_URLs"] = {
|
mConfig["template_registries"] = nlohmann::json::array({
|
||||||
"https://templates.dropshell.app"
|
nlohmann::json::object({
|
||||||
};
|
{"name", "main"},
|
||||||
mConfig["template_upload_token"] = "SECRETTOKEN";
|
{"url", "https://templates.dropshell.app"},
|
||||||
|
{"token", ""}
|
||||||
|
})
|
||||||
|
});
|
||||||
mConfig["backups_path"] = {
|
mConfig["backups_path"] = {
|
||||||
dropshell_base + "/backups"
|
dropshell_base + "/backups"
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user