2017-04-13 15:55:38 +02:00
|
|
|
#include "config.hh"
|
|
|
|
#include "args.hh"
|
2020-09-21 18:49:43 +02:00
|
|
|
#include "abstract-setting-to-json.hh"
|
2021-10-25 15:53:01 +02:00
|
|
|
#include "experimental-features.hh"
|
2020-08-20 11:02:16 +02:00
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
2017-04-13 15:55:38 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2018-03-27 18:41:31 +02:00
|
|
|
bool Config::set(const std::string & name, const std::string & value)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
2020-10-29 18:17:39 +01:00
|
|
|
bool append = false;
|
2017-04-13 15:55:38 +02:00
|
|
|
auto i = _settings.find(name);
|
2020-10-29 18:17:39 +01:00
|
|
|
if (i == _settings.end()) {
|
|
|
|
if (hasPrefix(name, "extra-")) {
|
|
|
|
i = _settings.find(std::string(name, 6));
|
|
|
|
if (i == _settings.end() || !i->second.setting->isAppendable())
|
|
|
|
return false;
|
|
|
|
append = true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i->second.setting->set(value, append);
|
2021-03-26 16:14:38 +01:00
|
|
|
i->second.setting->overridden = true;
|
2018-03-27 18:41:31 +02:00
|
|
|
return true;
|
2017-04-13 15:55:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:53:23 +02:00
|
|
|
void Config::addSetting(AbstractSetting * setting)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
2017-05-03 16:08:48 +02:00
|
|
|
_settings.emplace(setting->name, Config::SettingData(false, setting));
|
2017-04-13 15:55:38 +02:00
|
|
|
for (auto & alias : setting->aliases)
|
2017-05-03 16:08:48 +02:00
|
|
|
_settings.emplace(alias, Config::SettingData(true, setting));
|
2017-04-13 15:55:38 +02:00
|
|
|
|
|
|
|
bool set = false;
|
|
|
|
|
2018-03-27 18:41:31 +02:00
|
|
|
auto i = unknownSettings.find(setting->name);
|
|
|
|
if (i != unknownSettings.end()) {
|
2017-04-13 15:55:38 +02:00
|
|
|
setting->set(i->second);
|
2021-03-26 16:14:38 +01:00
|
|
|
setting->overridden = true;
|
2018-03-27 18:41:31 +02:00
|
|
|
unknownSettings.erase(i);
|
2017-04-13 15:55:38 +02:00
|
|
|
set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & alias : setting->aliases) {
|
2018-03-27 18:41:31 +02:00
|
|
|
auto i = unknownSettings.find(alias);
|
|
|
|
if (i != unknownSettings.end()) {
|
2017-04-13 15:55:38 +02:00
|
|
|
if (set)
|
|
|
|
warn("setting '%s' is set, but it's an alias of '%s' which is also set",
|
|
|
|
alias, setting->name);
|
|
|
|
else {
|
|
|
|
setting->set(i->second);
|
2021-03-26 16:14:38 +01:00
|
|
|
setting->overridden = true;
|
2018-03-27 18:41:31 +02:00
|
|
|
unknownSettings.erase(i);
|
2017-04-13 15:55:38 +02:00
|
|
|
set = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 18:41:31 +02:00
|
|
|
void AbstractConfig::warnUnknownSettings()
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
2018-03-27 18:41:31 +02:00
|
|
|
for (auto & s : unknownSettings)
|
2018-02-19 14:00:34 +01:00
|
|
|
warn("unknown setting '%s'", s.first);
|
2017-04-13 15:55:38 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 18:41:31 +02:00
|
|
|
void AbstractConfig::reapplyUnknownSettings()
|
|
|
|
{
|
|
|
|
auto unknownSettings2 = std::move(unknownSettings);
|
|
|
|
for (auto & s : unknownSettings2)
|
|
|
|
set(s.first, s.second);
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:14:38 +01:00
|
|
|
void Config::getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
|
|
|
for (auto & opt : _settings)
|
2021-03-26 16:14:38 +01:00
|
|
|
if (!opt.second.isAlias && (!overriddenOnly || opt.second.setting->overridden))
|
2018-03-27 18:41:31 +02:00
|
|
|
res.emplace(opt.first, SettingInfo{opt.second.setting->to_string(), opt.second.setting->description});
|
2017-04-13 15:55:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 14:39:02 +02:00
|
|
|
void AbstractConfig::applyConfig(const std::string & contents, const std::string & path) {
|
|
|
|
unsigned int pos = 0;
|
|
|
|
|
|
|
|
while (pos < contents.size()) {
|
2022-02-25 16:00:00 +01:00
|
|
|
std::string line;
|
2020-05-27 14:39:02 +02:00
|
|
|
while (pos < contents.size() && contents[pos] != '\n')
|
|
|
|
line += contents[pos++];
|
|
|
|
pos++;
|
|
|
|
|
2022-02-25 16:00:00 +01:00
|
|
|
auto hash = line.find('#');
|
|
|
|
if (hash != std::string::npos)
|
|
|
|
line = std::string(line, 0, hash);
|
2020-05-27 14:39:02 +02:00
|
|
|
|
2022-02-25 16:00:00 +01:00
|
|
|
auto tokens = tokenizeString<std::vector<std::string>>(line);
|
2020-05-27 14:39:02 +02:00
|
|
|
if (tokens.empty()) continue;
|
|
|
|
|
|
|
|
if (tokens.size() < 2)
|
|
|
|
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
|
|
|
|
|
|
|
|
auto include = false;
|
|
|
|
auto ignoreMissing = false;
|
|
|
|
if (tokens[0] == "include")
|
|
|
|
include = true;
|
|
|
|
else if (tokens[0] == "!include") {
|
|
|
|
include = true;
|
|
|
|
ignoreMissing = true;
|
|
|
|
}
|
2017-04-13 20:53:23 +02:00
|
|
|
|
2020-05-27 14:39:02 +02:00
|
|
|
if (include) {
|
|
|
|
if (tokens.size() != 2)
|
2018-02-13 14:16:32 +01:00
|
|
|
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
|
2020-05-27 14:39:02 +02:00
|
|
|
auto p = absPath(tokens[1], dirOf(path));
|
|
|
|
if (pathExists(p)) {
|
|
|
|
applyConfigFile(p);
|
|
|
|
} else if (!ignoreMissing) {
|
|
|
|
throw Error("file '%1%' included from '%2%' not found", p, path);
|
2018-02-13 14:16:32 +01:00
|
|
|
}
|
2020-05-27 14:39:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-02-13 14:16:32 +01:00
|
|
|
|
2020-05-27 14:39:02 +02:00
|
|
|
if (tokens[1] != "=")
|
|
|
|
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
|
2018-02-13 14:16:32 +01:00
|
|
|
|
2022-02-25 16:00:00 +01:00
|
|
|
std::string name = tokens[0];
|
2017-04-13 20:53:23 +02:00
|
|
|
|
2022-02-21 16:32:34 +01:00
|
|
|
auto i = tokens.begin();
|
2020-05-27 14:39:02 +02:00
|
|
|
advance(i, 2);
|
2017-04-13 20:53:23 +02:00
|
|
|
|
2020-05-27 14:39:02 +02:00
|
|
|
set(name, concatStringsSep(" ", Strings(i, tokens.end()))); // FIXME: slow
|
|
|
|
};
|
|
|
|
}
|
2017-04-13 20:53:23 +02:00
|
|
|
|
2020-05-27 14:39:02 +02:00
|
|
|
void AbstractConfig::applyConfigFile(const Path & path)
|
|
|
|
{
|
|
|
|
try {
|
2022-02-25 16:00:00 +01:00
|
|
|
std::string contents = readFile(path);
|
2020-05-27 14:39:02 +02:00
|
|
|
applyConfig(contents, path);
|
2017-04-13 20:53:23 +02:00
|
|
|
} catch (SysError &) { }
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:14:38 +01:00
|
|
|
void Config::resetOverridden()
|
2017-04-20 14:58:16 +02:00
|
|
|
{
|
|
|
|
for (auto & s : _settings)
|
2021-03-26 16:14:38 +01:00
|
|
|
s.second.setting->overridden = false;
|
2017-04-20 14:58:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:02:16 +02:00
|
|
|
nlohmann::json Config::toJSON()
|
2017-04-20 17:34:47 +02:00
|
|
|
{
|
2020-08-20 11:02:16 +02:00
|
|
|
auto res = nlohmann::json::object();
|
2017-04-20 17:34:47 +02:00
|
|
|
for (auto & s : _settings)
|
|
|
|
if (!s.second.isAlias) {
|
2020-09-09 11:35:33 +02:00
|
|
|
res.emplace(s.first, s.second.setting->toJSON());
|
2017-04-20 17:34:47 +02:00
|
|
|
}
|
2020-08-20 11:02:16 +02:00
|
|
|
return res;
|
2017-04-20 17:34:47 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 18:17:18 +02:00
|
|
|
std::string Config::toKeyValue()
|
|
|
|
{
|
|
|
|
auto res = std::string();
|
|
|
|
for (auto & s : _settings)
|
|
|
|
if (!s.second.isAlias) {
|
|
|
|
res += fmt("%s = %s\n", s.first, s.second.setting->to_string());
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:41:20 +02:00
|
|
|
void Config::convertToArgs(Args & args, const std::string & category)
|
2017-06-07 16:17:17 +02:00
|
|
|
{
|
|
|
|
for (auto & s : _settings)
|
|
|
|
if (!s.second.isAlias)
|
2017-06-07 18:41:20 +02:00
|
|
|
s.second.setting->convertToArg(args, category);
|
2017-06-07 16:17:17 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 15:55:38 +02:00
|
|
|
AbstractSetting::AbstractSetting(
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases)
|
2020-08-19 18:28:04 +02:00
|
|
|
: name(name), description(stripIndentation(description)), aliases(aliases)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-20 11:02:16 +02:00
|
|
|
nlohmann::json AbstractSetting::toJSON()
|
2017-04-20 17:34:47 +02:00
|
|
|
{
|
2020-09-09 11:35:33 +02:00
|
|
|
return nlohmann::json(toJSONObject());
|
2017-04-20 17:34:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 11:35:33 +02:00
|
|
|
std::map<std::string, nlohmann::json> AbstractSetting::toJSONObject()
|
2017-06-07 16:17:17 +02:00
|
|
|
{
|
2020-09-09 11:35:33 +02:00
|
|
|
std::map<std::string, nlohmann::json> obj;
|
|
|
|
obj.emplace("description", description);
|
|
|
|
obj.emplace("aliases", aliases);
|
|
|
|
return obj;
|
2017-06-07 16:17:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 11:35:33 +02:00
|
|
|
void AbstractSetting::convertToArg(Args & args, const std::string & category)
|
2017-04-20 17:34:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
template<typename T>
|
|
|
|
bool BaseSetting<T>::isAppendable()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:17:17 +02:00
|
|
|
template<typename T>
|
2017-06-07 18:41:20 +02:00
|
|
|
void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
|
2017-06-07 16:17:17 +02:00
|
|
|
{
|
2020-05-04 22:40:19 +02:00
|
|
|
args.addFlag({
|
|
|
|
.longName = name,
|
2020-10-29 18:26:35 +01:00
|
|
|
.description = fmt("Set the `%s` setting.", name),
|
2020-05-04 22:40:19 +02:00
|
|
|
.category = category,
|
|
|
|
.labels = {"value"},
|
2021-03-26 16:14:38 +01:00
|
|
|
.handler = {[=](std::string s) { overridden = true; set(s); }},
|
2020-05-04 22:40:19 +02:00
|
|
|
});
|
2020-10-29 18:17:39 +01:00
|
|
|
|
|
|
|
if (isAppendable())
|
|
|
|
args.addFlag({
|
|
|
|
.longName = "extra-" + name,
|
2020-10-29 18:26:35 +01:00
|
|
|
.description = fmt("Append to the `%s` setting.", name),
|
2020-10-29 18:17:39 +01:00
|
|
|
.category = category,
|
|
|
|
.labels = {"value"},
|
2021-03-26 16:14:38 +01:00
|
|
|
.handler = {[=](std::string s) { overridden = true; set(s, true); }},
|
2020-10-29 18:17:39 +01:00
|
|
|
});
|
2017-06-07 16:17:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
template<> void BaseSetting<std::string>::set(const std::string & str, bool append)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
|
|
|
value = str;
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:17:53 +01:00
|
|
|
template<> std::string BaseSetting<std::string>::to_string() const
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
template<typename T>
|
2020-10-29 18:17:39 +01:00
|
|
|
void BaseSetting<T>::set(const std::string & str, bool append)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
2017-04-13 17:54:05 +02:00
|
|
|
static_assert(std::is_integral<T>::value, "Integer required.");
|
2021-01-08 12:22:21 +01:00
|
|
|
if (auto n = string2Int<T>(str))
|
|
|
|
value = *n;
|
|
|
|
else
|
2017-04-13 15:55:38 +02:00
|
|
|
throw UsageError("setting '%s' has invalid value '%s'", name, str);
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
template<typename T>
|
2019-12-17 17:17:53 +01:00
|
|
|
std::string BaseSetting<T>::to_string() const
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
2017-04-13 17:54:05 +02:00
|
|
|
static_assert(std::is_integral<T>::value, "Integer required.");
|
2017-04-13 15:55:38 +02:00
|
|
|
return std::to_string(value);
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
template<> void BaseSetting<bool>::set(const std::string & str, bool append)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
2017-04-13 16:31:28 +02:00
|
|
|
if (str == "true" || str == "yes" || str == "1")
|
2017-04-20 16:52:53 +02:00
|
|
|
value = true;
|
2017-04-13 16:31:28 +02:00
|
|
|
else if (str == "false" || str == "no" || str == "0")
|
2017-04-20 16:52:53 +02:00
|
|
|
value = false;
|
2017-04-13 16:31:28 +02:00
|
|
|
else
|
|
|
|
throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str);
|
2017-04-13 15:55:38 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:17:53 +01:00
|
|
|
template<> std::string BaseSetting<bool>::to_string() const
|
2017-04-13 20:53:23 +02:00
|
|
|
{
|
2017-04-20 16:52:53 +02:00
|
|
|
return value ? "true" : "false";
|
2017-04-13 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:41:20 +02:00
|
|
|
template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & category)
|
2017-06-07 16:17:17 +02:00
|
|
|
{
|
2020-05-04 22:40:19 +02:00
|
|
|
args.addFlag({
|
|
|
|
.longName = name,
|
2020-10-29 18:26:35 +01:00
|
|
|
.description = fmt("Enable the `%s` setting.", name),
|
2020-05-04 22:40:19 +02:00
|
|
|
.category = category,
|
|
|
|
.handler = {[=]() { override(true); }}
|
|
|
|
});
|
|
|
|
args.addFlag({
|
|
|
|
.longName = "no-" + name,
|
2020-10-29 18:26:35 +01:00
|
|
|
.description = fmt("Disable the `%s` setting.", name),
|
2020-05-04 22:40:19 +02:00
|
|
|
.category = category,
|
|
|
|
.handler = {[=]() { override(false); }}
|
|
|
|
});
|
2017-06-07 16:17:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
template<> void BaseSetting<Strings>::set(const std::string & str, bool append)
|
|
|
|
{
|
|
|
|
auto ss = tokenizeString<Strings>(str);
|
|
|
|
if (!append) value.clear();
|
|
|
|
for (auto & s : ss) value.push_back(std::move(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool BaseSetting<Strings>::isAppendable()
|
2017-04-13 20:53:23 +02:00
|
|
|
{
|
2020-10-29 18:17:39 +01:00
|
|
|
return true;
|
2017-04-13 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:17:53 +01:00
|
|
|
template<> std::string BaseSetting<Strings>::to_string() const
|
2017-04-13 20:53:23 +02:00
|
|
|
{
|
|
|
|
return concatStringsSep(" ", value);
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
template<> void BaseSetting<StringSet>::set(const std::string & str, bool append)
|
2017-04-13 20:53:23 +02:00
|
|
|
{
|
2020-11-09 15:04:34 +01:00
|
|
|
if (!append) value.clear();
|
|
|
|
for (auto & s : tokenizeString<StringSet>(str))
|
|
|
|
value.insert(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool BaseSetting<StringSet>::isAppendable()
|
|
|
|
{
|
|
|
|
return true;
|
2017-04-13 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:17:53 +01:00
|
|
|
template<> std::string BaseSetting<StringSet>::to_string() const
|
2017-04-13 20:53:23 +02:00
|
|
|
{
|
|
|
|
return concatStringsSep(" ", value);
|
2017-04-13 15:55:38 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 15:53:01 +02:00
|
|
|
template<> void BaseSetting<std::set<ExperimentalFeature>>::set(const std::string & str, bool append)
|
|
|
|
{
|
|
|
|
if (!append) value.clear();
|
|
|
|
for (auto & s : tokenizeString<StringSet>(str)) {
|
|
|
|
auto thisXpFeature = parseExperimentalFeature(s);
|
|
|
|
if (thisXpFeature)
|
|
|
|
value.insert(thisXpFeature.value());
|
|
|
|
else
|
2021-10-27 13:01:56 +02:00
|
|
|
warn("unknown experimental feature '%s'", s);
|
2021-10-25 15:53:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool BaseSetting<std::set<ExperimentalFeature>>::isAppendable()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> std::string BaseSetting<std::set<ExperimentalFeature>>::to_string() const
|
|
|
|
{
|
|
|
|
StringSet stringifiedXpFeatures;
|
|
|
|
for (auto & feature : value)
|
|
|
|
stringifiedXpFeatures.insert(std::string(showExperimentalFeature(feature)));
|
|
|
|
return concatStringsSep(" ", stringifiedXpFeatures);
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
template<> void BaseSetting<StringMap>::set(const std::string & str, bool append)
|
2020-09-25 07:42:59 +02:00
|
|
|
{
|
2020-10-29 18:17:39 +01:00
|
|
|
if (!append) value.clear();
|
2020-11-09 15:04:34 +01:00
|
|
|
for (auto & s : tokenizeString<Strings>(str)) {
|
2020-09-25 07:42:59 +02:00
|
|
|
auto eq = s.find_first_of('=');
|
|
|
|
if (std::string::npos != eq)
|
|
|
|
value.emplace(std::string(s, 0, eq), std::string(s, eq + 1));
|
|
|
|
// else ignored
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
template<> bool BaseSetting<StringMap>::isAppendable()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-25 07:42:59 +02:00
|
|
|
template<> std::string BaseSetting<StringMap>::to_string() const
|
|
|
|
{
|
|
|
|
Strings kvstrs;
|
|
|
|
std::transform(value.begin(), value.end(), back_inserter(kvstrs),
|
|
|
|
[&](auto kvpair){ return kvpair.first + "=" + kvpair.second; });
|
|
|
|
return concatStringsSep(" ", kvstrs);
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
template class BaseSetting<int>;
|
|
|
|
template class BaseSetting<unsigned int>;
|
|
|
|
template class BaseSetting<long>;
|
|
|
|
template class BaseSetting<unsigned long>;
|
|
|
|
template class BaseSetting<long long>;
|
|
|
|
template class BaseSetting<unsigned long long>;
|
2017-04-20 17:34:47 +02:00
|
|
|
template class BaseSetting<bool>;
|
2017-04-28 17:13:55 +02:00
|
|
|
template class BaseSetting<std::string>;
|
2017-06-07 16:17:17 +02:00
|
|
|
template class BaseSetting<Strings>;
|
|
|
|
template class BaseSetting<StringSet>;
|
2020-09-25 07:42:59 +02:00
|
|
|
template class BaseSetting<StringMap>;
|
2021-10-25 15:53:01 +02:00
|
|
|
template class BaseSetting<std::set<ExperimentalFeature>>;
|
2017-04-13 17:54:05 +02:00
|
|
|
|
2020-10-29 18:17:39 +01:00
|
|
|
void PathSetting::set(const std::string & str, bool append)
|
2017-04-13 15:55:38 +02:00
|
|
|
{
|
|
|
|
if (str == "") {
|
|
|
|
if (allowEmpty)
|
|
|
|
value = "";
|
|
|
|
else
|
|
|
|
throw UsageError("setting '%s' cannot be empty", name);
|
|
|
|
} else
|
|
|
|
value = canonPath(str);
|
|
|
|
}
|
|
|
|
|
2018-03-27 18:41:31 +02:00
|
|
|
bool GlobalConfig::set(const std::string & name, const std::string & value)
|
|
|
|
{
|
|
|
|
for (auto & config : *configRegistrations)
|
|
|
|
if (config->set(name, value)) return true;
|
|
|
|
|
|
|
|
unknownSettings.emplace(name, value);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:14:38 +01:00
|
|
|
void GlobalConfig::getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly)
|
2018-03-27 18:41:31 +02:00
|
|
|
{
|
|
|
|
for (auto & config : *configRegistrations)
|
2021-03-26 16:14:38 +01:00
|
|
|
config->getSettings(res, overriddenOnly);
|
2018-03-27 18:41:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 16:14:38 +01:00
|
|
|
void GlobalConfig::resetOverridden()
|
2018-03-27 18:41:31 +02:00
|
|
|
{
|
|
|
|
for (auto & config : *configRegistrations)
|
2021-03-26 16:14:38 +01:00
|
|
|
config->resetOverridden();
|
2018-03-27 18:41:31 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:02:16 +02:00
|
|
|
nlohmann::json GlobalConfig::toJSON()
|
2018-03-27 18:41:31 +02:00
|
|
|
{
|
2020-08-20 11:02:16 +02:00
|
|
|
auto res = nlohmann::json::object();
|
2018-03-27 18:41:31 +02:00
|
|
|
for (auto & config : *configRegistrations)
|
2020-08-20 11:02:16 +02:00
|
|
|
res.update(config->toJSON());
|
|
|
|
return res;
|
2018-03-27 18:41:31 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 18:17:18 +02:00
|
|
|
std::string GlobalConfig::toKeyValue()
|
|
|
|
{
|
|
|
|
std::string res;
|
|
|
|
std::map<std::string, Config::SettingInfo> settings;
|
|
|
|
globalConfig.getSettings(settings);
|
|
|
|
for (auto & s : settings)
|
|
|
|
res += fmt("%s = %s\n", s.first, s.second.value);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-27 18:41:31 +02:00
|
|
|
void GlobalConfig::convertToArgs(Args & args, const std::string & category)
|
|
|
|
{
|
|
|
|
for (auto & config : *configRegistrations)
|
|
|
|
config->convertToArgs(args, category);
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalConfig globalConfig;
|
|
|
|
|
|
|
|
GlobalConfig::ConfigRegistrations * GlobalConfig::configRegistrations;
|
|
|
|
|
|
|
|
GlobalConfig::Register::Register(Config * config)
|
|
|
|
{
|
|
|
|
if (!configRegistrations)
|
|
|
|
configRegistrations = new ConfigRegistrations;
|
|
|
|
configRegistrations->emplace_back(config);
|
|
|
|
}
|
|
|
|
|
2017-04-13 15:55:38 +02:00
|
|
|
}
|