Skip to content

Commit 7ad508c

Browse files
committed
refactor: move YAML load function to policy parser
1 parent 7391c86 commit 7ad508c

File tree

3 files changed

+7
-13
lines changed

3 files changed

+7
-13
lines changed

src/modules/sca/src/sca_policy_loader.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <logger.hpp>
66
#include <sca_policy_parser.hpp>
77

8-
#include <yaml-cpp/yaml.h>
9-
108
#include <algorithm>
119

1210
SCAPolicyLoader::SCAPolicyLoader(std::shared_ptr<IFileSystemWrapper> fileSystemWrapper,
@@ -65,14 +63,9 @@ std::vector<SCAPolicy> SCAPolicyLoader::GetPolicies(const CreateEventsFunc& crea
6563
{
6664
LogDebug("Loading policy from {}", path.string());
6765

68-
auto loadFunc = [](const std::filesystem::path& filename) -> YAML::Node
69-
{
70-
return YAML::LoadFile(filename.string());
71-
};
66+
const PolicyParser parser(path);
7267

73-
const PolicyParser parser(path, loadFunc);
74-
auto policy = parser.ParsePolicy(policiesAndChecks);
75-
if (policy)
68+
if (auto policy = parser.ParsePolicy(policiesAndChecks); policy)
7669
{
7770
policies.emplace_back(std::move(policy.value()));
7871
}

src/modules/sca/src/sca_policy_parser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ namespace
7171
}
7272
} // namespace
7373

74-
PolicyParser::PolicyParser(const std::filesystem::path& filename, const LoadFileFunc& loadFile)
74+
// NOLINTNEXTLINE(performance-unnecessary-value-param)
75+
PolicyParser::PolicyParser(const std::filesystem::path& filename, LoadFileFunc loadFileFunc)
7576
{
7677
try
7778
{
7879
if (!isValidYamlFile(filename))
7980
{
8081
throw std::runtime_error("The file does not contain a valid YAML structure.");
8182
}
82-
m_node = loadFile(filename);
83+
m_node = loadFileFunc ? loadFileFunc(filename) : YAML::LoadFile(filename.string());
8384
auto variables = m_node["variables"];
8485
if (variables)
8586
{

src/modules/sca/src/sca_policy_parser.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class PolicyParser
3737
public:
3838
/// @brief Constructs a PolicyParser and loads the YAML file.
3939
/// @param filename Path to the YAML policy file.
40-
/// @param loadFile Function to load the YAML file.
41-
explicit PolicyParser(const std::filesystem::path& filename, const LoadFileFunc& loadFile);
40+
/// @param loadFileFunc Function to load the YAML file.
41+
explicit PolicyParser(const std::filesystem::path& filename, LoadFileFunc loadFileFunc = {});
4242

4343
/// @brief Checks if the specified YAML file is valid.
4444
///

0 commit comments

Comments
 (0)