Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 1 | #include <cassert> |
| 2 | #include <fstream> |
| 3 | #include <iostream> |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 4 | #include "systemd_target_parser.hpp" |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 5 | |
| 6 | void validateErrorsToMonitor(std::vector<std::string>& errorsToMonitor) |
| 7 | { |
| 8 | assert(errorsToMonitor.size()); |
| 9 | |
| 10 | const std::vector<std::string> validErrorsToMonitor = { |
| 11 | "default", "timeout", "failed", "dependency"}; |
| 12 | for (const auto& errorToMonitor : errorsToMonitor) |
| 13 | { |
| 14 | if (std::find(validErrorsToMonitor.begin(), validErrorsToMonitor.end(), |
| 15 | errorToMonitor) == validErrorsToMonitor.end()) |
| 16 | { |
| 17 | throw std::out_of_range("Found invalid error to monitor"); |
| 18 | } |
| 19 | } |
Andrew Geissler | 75a2614 | 2019-08-09 15:23:51 -0500 | [diff] [blame] | 20 | // See if default was in the errors to monitor, if so replace with defaults |
| 21 | auto errorItr = |
| 22 | std::find(errorsToMonitor.begin(), errorsToMonitor.end(), "default"); |
| 23 | if (errorItr != errorsToMonitor.end()) |
| 24 | { |
| 25 | // Verify default is the only entry |
| 26 | if (errorsToMonitor.size() != 1) |
| 27 | { |
| 28 | throw std::invalid_argument( |
| 29 | "default must be only error to monitor"); |
| 30 | } |
| 31 | // delete "default" and insert defaults |
| 32 | errorsToMonitor.erase(errorItr); |
| 33 | errorsToMonitor.push_back("timeout"); |
| 34 | errorsToMonitor.push_back("failed"); |
| 35 | errorsToMonitor.push_back("dependency"); |
| 36 | } |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | TargetErrorData parseFiles(const std::vector<std::string>& filePaths) |
| 40 | { |
| 41 | TargetErrorData systemdTargetMap; |
| 42 | for (const auto& jsonFile : filePaths) |
| 43 | { |
| 44 | if (gVerbose) |
| 45 | { |
| 46 | std::cout << "Parsing input file " << jsonFile << std::endl; |
| 47 | } |
| 48 | std::ifstream fileStream(jsonFile); |
| 49 | auto j = json::parse(fileStream); |
| 50 | |
| 51 | for (auto it = j["targets"].begin(); it != j["targets"].end(); ++it) |
| 52 | { |
| 53 | targetEntry entry; |
| 54 | if (gVerbose) |
| 55 | { |
| 56 | std::cout << "target: " << it.key() << " | " << it.value() |
| 57 | << std::endl; |
| 58 | } |
| 59 | |
| 60 | // Be unforgiving on invalid json files. Just throw or allow |
| 61 | // nlohmann to throw an exception if something is off |
| 62 | auto errorsToMonitor = it.value().find("errorsToMonitor"); |
| 63 | entry.errorsToMonitor = |
| 64 | errorsToMonitor->get<std::vector<std::string>>(); |
| 65 | |
| 66 | validateErrorsToMonitor(entry.errorsToMonitor); |
| 67 | |
| 68 | auto errorToLog = it.value().find("errorToLog"); |
| 69 | entry.errorToLog = errorToLog->get<std::string>(); |
| 70 | |
| 71 | systemdTargetMap[it.key()] = entry; |
| 72 | } |
| 73 | } |
| 74 | return systemdTargetMap; |
| 75 | } |