Update source/src/config.cpp
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 41s
Build-Test-Publish / build (linux/arm64) (push) Successful in 1m13s

This commit is contained in:
Your Name
2025-08-25 20:28:54 +12:00
parent 44e6d88c85
commit 5d15b92dbf

View File

@@ -1,11 +1,12 @@
#include "utils/directories.hpp"
#include <iostream>
#include <fstream>
#include <set>
#include "config.hpp"
#include "utils/utils.hpp"
#include <filesystem>
#include "utils/execute.hpp"
#include "output.hpp"
#include "utils/output.hpp"
namespace dropshell {
@@ -41,6 +42,72 @@ bool config::load_config() { // load json config file.
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;
return true;
}
@@ -77,11 +144,13 @@ bool config::save_config(bool create_aux_directories)
mConfig["template_local_paths"] = {
dropshell_base + "/local_templates"
};
mConfig["template_registry_URLs"] = {
"https://templates.dropshell.app"
};
mConfig["template_upload_token"] = "SECRETTOKEN";
mConfig["template_registries"] = nlohmann::json::array({
nlohmann::json::object({
{"name", "main"},
{"url", "https://templates.dropshell.app"},
{"token", ""}
})
});
mConfig["backups_path"] = {
dropshell_base + "/backups"
};