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