blob: d2cfa8611062ce2c146d9de31f946bac85afd068 [file] [log] [blame]
Andrew Geissler693eaed2019-08-02 14:12:28 -05001#include <cassert>
2#include <fstream>
3#include <iostream>
4#include <systemd_target_parser.hpp>
5
6void 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 }
20}
21
22TargetErrorData parseFiles(const std::vector<std::string>& filePaths)
23{
24 TargetErrorData systemdTargetMap;
25 for (const auto& jsonFile : filePaths)
26 {
27 if (gVerbose)
28 {
29 std::cout << "Parsing input file " << jsonFile << std::endl;
30 }
31 std::ifstream fileStream(jsonFile);
32 auto j = json::parse(fileStream);
33
34 for (auto it = j["targets"].begin(); it != j["targets"].end(); ++it)
35 {
36 targetEntry entry;
37 if (gVerbose)
38 {
39 std::cout << "target: " << it.key() << " | " << it.value()
40 << std::endl;
41 }
42
43 // Be unforgiving on invalid json files. Just throw or allow
44 // nlohmann to throw an exception if something is off
45 auto errorsToMonitor = it.value().find("errorsToMonitor");
46 entry.errorsToMonitor =
47 errorsToMonitor->get<std::vector<std::string>>();
48
49 validateErrorsToMonitor(entry.errorsToMonitor);
50
51 auto errorToLog = it.value().find("errorToLog");
52 entry.errorToLog = errorToLog->get<std::string>();
53
54 systemdTargetMap[it.key()] = entry;
55 }
56 }
57 return systemdTargetMap;
58}