20 #include "softwarecontainer-common.h" 22 #include "configtypes.h" 23 #include "configerror.h" 30 MandatoryConfigs mandatory,
31 ConfigDependencies dependencies):
32 m_sources(std::move(sources)),
33 m_mandatory(mandatory),
34 m_dependencies(dependencies),
38 m_allConfigs(std::vector<UniqueKey>())
46 readConfigsFromSources();
52 assertAllMandatoryPresent();
55 void Config::assertDependencies()
57 for (
const auto &dep : m_dependencies) {
58 UniqueKey config = dep.first;
59 std::string group = std::get<0>(config);
60 std::string key = std::get<1>(config);
63 bool found = std::find(m_allConfigs.begin(), m_allConfigs.end(), config) != m_allConfigs.end();
66 if (found && !allDepsSatisfied(dep.second)) {
68 std::string message =
"One or more dependencies were not met for config " + group +
"::" + key;
69 log_error() << message;
75 bool Config::allDepsSatisfied(
const std::vector<UniqueKey> &dependencies)
const 77 for (
const UniqueKey &dependency : dependencies) {
78 bool found = std::find(m_allConfigs.begin(), m_allConfigs.end(), dependency) != m_allConfigs.end();
88 void Config::assertAllMandatoryPresent()
90 for (
const UniqueKey &mandatory : m_mandatory) {
92 bool found = std::find(m_allConfigs.begin(), m_allConfigs.end(), mandatory) != m_allConfigs.end();
95 std::string message =
"Mandatory config " + mandatory.first +
"::" + mandatory.second +
" not found";
96 log_error() << message;
102 void Config::readConfigsFromSources()
104 for (
auto &source : m_sources) {
105 auto stringConfigs = source->stringConfigs();
106 m_stringConfigs.insert(m_stringConfigs.end(), stringConfigs.begin(), stringConfigs.end());
108 auto intConfigs = source->intConfigs();
109 m_intConfigs.insert(m_intConfigs.end(), intConfigs.begin(), intConfigs.end());
111 auto boolConfigs = source->boolConfigs();
112 m_boolConfigs.insert(m_boolConfigs.end(), boolConfigs.begin(), boolConfigs.end());
116 for (
auto &config : m_stringConfigs) {
117 m_allConfigs.push_back(UniqueKey(config.group(), config.key()));
120 for (
auto &config : m_intConfigs) {
121 m_allConfigs.push_back(UniqueKey(config.group(), config.key()));
124 for (
auto &config : m_boolConfigs) {
125 m_allConfigs.push_back(UniqueKey(config.group(), config.key()));
131 return getConfig(group, key, m_stringConfigs).value();
136 return getConfig(group, key, m_intConfigs).value();
141 return getConfig(group, key, m_boolConfigs).value();
145 T Config::getConfig(
const std::string &group,
const std::string &key,
const std::vector<T> &configs)
const 147 std::vector<T> foundConfigs;
149 for (
auto config : configs) {
150 if (config.group() == group && config.key() == key) {
151 log_debug() <<
"Found config " 152 << group <<
"::" << key
154 << ConfigTypes::configSourceToString(config.source());
155 foundConfigs.push_back(config);
159 if (foundConfigs.empty()) {
160 std::string message =
"No config found for " + group +
"::" + key;
161 log_error() << message;
165 T config = prioritizedConfig(foundConfigs);
167 log_debug() <<
"Using config " 168 << group <<
"::" << key
170 << config.value() <<
"\"" 171 <<
", from " << ConfigTypes::configSourceToString(config.source());
177 T Config::prioritizedConfig(
const std::vector<T> &configs)
const 179 for (
auto config : configs) {
180 if (config.source() == ConfigSourceType::Commandline) {
185 for (
auto config : configs) {
186 if (config.source() == ConfigSourceType::Main) {
191 for (
auto config : configs) {
192 if (config.source() == ConfigSourceType::Default) {
197 log_error() <<
"Config has an incorrect source";
bool getBoolValue(const std::string &group, const std::string &key) const
getBoolValue Get a config value of type bool
Represents a config item of type bool.
Represents a config item of type string.
std::string getStringValue(const std::string &group, const std::string &key) const
getStringValue Get a config value of type string
A requested config item could not be found in any of the config sources.
int getIntValue(const std::string &group, const std::string &key) const
getIntValue Get a config value of type int
Developers guide to adding a config item:
Represents a config item of type int.
Config(std::vector< std::unique_ptr< ConfigSource >> sources, MandatoryConfigs mandatory, ConfigDependencies dependencies)
Constructor - retrieves all configs from the available sources.
An error occured which is an internal error in the config code.