Christopher Meis | bdaa6b2 | 2025-04-02 10:49:02 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame^] | 5 | #include <unordered_set> |
| 6 | #include <vector> |
Christopher Meis | bdaa6b2 | 2025-04-02 10:49:02 +0200 | [diff] [blame] | 7 | |
Christopher Meis | bdaa6b2 | 2025-04-02 10:49:02 +0200 | [diff] [blame] | 8 | constexpr const char* globalSchema = "global.json"; |
| 9 | constexpr const char* hostConfigurationDirectory = SYSCONF_DIR "configurations"; |
| 10 | constexpr const char* configurationDirectory = PACKAGE_DIR "configurations"; |
| 11 | constexpr const char* currentConfiguration = "/var/configuration/system.json"; |
Alexander Hansen | ddc8eb6 | 2025-04-25 18:08:51 +0200 | [diff] [blame] | 12 | constexpr const char* schemaDirectory = PACKAGE_DIR "schemas"; |
Christopher Meis | bdaa6b2 | 2025-04-02 10:49:02 +0200 | [diff] [blame] | 13 | |
Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame^] | 14 | class Configuration |
| 15 | { |
| 16 | public: |
| 17 | explicit Configuration(); |
| 18 | std::unordered_set<std::string> probeInterfaces; |
| 19 | std::vector<nlohmann::json> configurations; |
Christopher Meis | bdaa6b2 | 2025-04-02 10:49:02 +0200 | [diff] [blame] | 20 | |
Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame^] | 21 | private: |
| 22 | void loadConfigurations(); |
| 23 | void filterProbeInterfaces(); |
| 24 | }; |
| 25 | |
| 26 | bool writeJsonFiles(const nlohmann::json& systemConfiguration); |
Christopher Meis | bdaa6b2 | 2025-04-02 10:49:02 +0200 | [diff] [blame] | 27 | |
| 28 | template <typename JsonType> |
| 29 | bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value, |
| 30 | nlohmann::json& systemConfiguration) |
| 31 | { |
| 32 | try |
| 33 | { |
| 34 | nlohmann::json::json_pointer ptr(ptrStr); |
| 35 | nlohmann::json& ref = systemConfiguration[ptr]; |
| 36 | ref = value; |
| 37 | return true; |
| 38 | } |
| 39 | catch (const std::out_of_range&) |
| 40 | { |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void deriveNewConfiguration(const nlohmann::json& oldConfiguration, |
| 46 | nlohmann::json& newConfiguration); |
| 47 | |
| 48 | bool validateJson(const nlohmann::json& schemaFile, |
| 49 | const nlohmann::json& input); |