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