From 5d15b92dbfb3a458e8dda1d55a197c8e367c77c0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 25 Aug 2025 20:28:54 +1200 Subject: [PATCH] Update source/src/config.cpp --- source/src/config.cpp | 81 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/source/src/config.cpp b/source/src/config.cpp index 8d955fb..5d31bea 100644 --- a/source/src/config.cpp +++ b/source/src/config.cpp @@ -1,11 +1,12 @@ #include "utils/directories.hpp" #include #include +#include #include "config.hpp" #include "utils/utils.hpp" #include #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 allowed_fields = { + "server_definition_paths", + "template_local_paths", + "template_registries", + "backups_path" + }; + + std::set 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" };