refactor(engine): support string config loading and add ad-hoc methods

This is a change of direction from the current design, that imposes loading
the configuration from file only, and in the object constructor. Instead,
yaml_configuration objects can now be reused ad can load the YAML config
from either file or string. This also makes it easier to unit test this class.

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
This commit is contained in:
Jason Dellaluce
2021-11-17 13:53:52 +00:00
committed by poiana
parent 205a8fd23b
commit 7781385769
2 changed files with 31 additions and 20 deletions

View File

@@ -54,7 +54,16 @@ falco_configuration::~falco_configuration()
void falco_configuration::init(string conf_filename, list<string> &cmdline_options)
{
string m_config_file = conf_filename;
m_config = new yaml_configuration(m_config_file);
m_config = new yaml_configuration();
try
{
m_config->load_from_file(m_config_file);
}
catch(const std::exception& e)
{
std::cerr << "Cannot read config file (" + m_config_file + "): " + e.what() + "\n";
throw e;
}
init_cmdline_options(cmdline_options);

View File

@@ -35,26 +35,28 @@ limitations under the License.
class yaml_configuration
{
public:
std::string m_path;
yaml_configuration(const std::string& path)
/**
* Load the YAML document represented by the input string.
*/
void load_from_string(const std::string& input)
{
m_path = path;
YAML::Node config;
std::vector<falco::outputs::config> outputs;
try
{
m_root = YAML::LoadFile(path);
}
catch(const YAML::BadFile& ex)
{
std::cerr << "Error reading config file (" + path + "): " + ex.what() + "\n";
throw;
}
catch(const YAML::ParserException& ex)
{
std::cerr << "Cannot read config file (" + path + "): " + ex.what() + "\n";
throw;
}
m_root = YAML::Load(input);
}
/**
* Load the YAML document from the given file path.
*/
void load_from_file(const std::string& path)
{
m_root = YAML::LoadFile(path);
}
/**
* Clears the internal loaded document.
*/
void clear()
{
m_root = YAML::Node();
}
/**