2017-04-13 16:55:38 +03:00
|
|
|
#include "config.hh"
|
|
|
|
#include "args.hh"
|
2017-04-20 18:34:47 +03:00
|
|
|
#include "json.hh"
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
bool Config::set(const std::string & name, const std::string & value)
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
|
|
|
auto i = _settings.find(name);
|
2018-03-27 19:41:31 +03:00
|
|
|
if (i == _settings.end()) return false;
|
|
|
|
i->second.setting->set(value);
|
|
|
|
i->second.setting->overriden = true;
|
|
|
|
return true;
|
2017-04-13 16:55:38 +03:00
|
|
|
}
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
void Config::addSetting(AbstractSetting * setting)
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
2017-05-03 17:08:48 +03:00
|
|
|
_settings.emplace(setting->name, Config::SettingData(false, setting));
|
2017-04-13 16:55:38 +03:00
|
|
|
for (auto & alias : setting->aliases)
|
2017-05-03 17:08:48 +03:00
|
|
|
_settings.emplace(alias, Config::SettingData(true, setting));
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
bool set = false;
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
auto i = unknownSettings.find(setting->name);
|
|
|
|
if (i != unknownSettings.end()) {
|
2017-04-13 16:55:38 +03:00
|
|
|
setting->set(i->second);
|
2017-04-20 15:58:16 +03:00
|
|
|
setting->overriden = true;
|
2018-03-27 19:41:31 +03:00
|
|
|
unknownSettings.erase(i);
|
2017-04-13 16:55:38 +03:00
|
|
|
set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & alias : setting->aliases) {
|
2018-03-27 19:41:31 +03:00
|
|
|
auto i = unknownSettings.find(alias);
|
|
|
|
if (i != unknownSettings.end()) {
|
2017-04-13 16:55:38 +03: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);
|
2017-04-20 15:58:16 +03:00
|
|
|
setting->overriden = true;
|
2018-03-27 19:41:31 +03:00
|
|
|
unknownSettings.erase(i);
|
2017-04-13 16:55:38 +03:00
|
|
|
set = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
void AbstractConfig::warnUnknownSettings()
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
2018-03-27 19:41:31 +03:00
|
|
|
for (auto & s : unknownSettings)
|
2018-02-19 15:00:34 +02:00
|
|
|
warn("unknown setting '%s'", s.first);
|
2017-04-13 16:55:38 +03:00
|
|
|
}
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
void AbstractConfig::reapplyUnknownSettings()
|
|
|
|
{
|
|
|
|
auto unknownSettings2 = std::move(unknownSettings);
|
|
|
|
for (auto & s : unknownSettings2)
|
|
|
|
set(s.first, s.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly)
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
|
|
|
for (auto & opt : _settings)
|
2017-04-20 15:58:16 +03:00
|
|
|
if (!opt.second.isAlias && (!overridenOnly || opt.second.setting->overriden))
|
2018-03-27 19:41:31 +03:00
|
|
|
res.emplace(opt.first, SettingInfo{opt.second.setting->to_string(), opt.second.setting->description});
|
2017-04-13 16:55:38 +03:00
|
|
|
}
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
void AbstractConfig::applyConfigFile(const Path & path)
|
2017-04-13 21:53:23 +03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
string contents = readFile(path);
|
|
|
|
|
|
|
|
unsigned int pos = 0;
|
|
|
|
|
|
|
|
while (pos < contents.size()) {
|
|
|
|
string line;
|
|
|
|
while (pos < contents.size() && contents[pos] != '\n')
|
|
|
|
line += contents[pos++];
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
string::size_type hash = line.find('#');
|
|
|
|
if (hash != string::npos)
|
|
|
|
line = string(line, 0, hash);
|
|
|
|
|
|
|
|
vector<string> tokens = tokenizeString<vector<string> >(line);
|
|
|
|
if (tokens.empty()) continue;
|
|
|
|
|
2018-02-13 15:16:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (include) {
|
|
|
|
if (tokens.size() != 2)
|
|
|
|
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
|
|
|
|
auto p = absPath(tokens[1], dirOf(path));
|
|
|
|
if (pathExists(p)) {
|
2018-02-13 21:43:32 +02:00
|
|
|
applyConfigFile(p);
|
2018-02-13 15:16:32 +02:00
|
|
|
} else if (!ignoreMissing) {
|
|
|
|
throw Error("file '%1%' included from '%2%' not found", p, path);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens[1] != "=")
|
2017-07-30 14:27:57 +03:00
|
|
|
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
|
2017-04-13 21:53:23 +03:00
|
|
|
|
|
|
|
string name = tokens[0];
|
|
|
|
|
|
|
|
vector<string>::iterator i = tokens.begin();
|
|
|
|
advance(i, 2);
|
|
|
|
|
2018-02-13 21:43:32 +02:00
|
|
|
set(name, concatStringsSep(" ", Strings(i, tokens.end()))); // FIXME: slow
|
2017-04-13 21:53:23 +03:00
|
|
|
};
|
|
|
|
} catch (SysError &) { }
|
|
|
|
}
|
|
|
|
|
2017-04-20 15:58:16 +03:00
|
|
|
void Config::resetOverriden()
|
|
|
|
{
|
|
|
|
for (auto & s : _settings)
|
|
|
|
s.second.setting->overriden = false;
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:34:47 +03:00
|
|
|
void Config::toJSON(JSONObject & out)
|
|
|
|
{
|
|
|
|
for (auto & s : _settings)
|
|
|
|
if (!s.second.isAlias) {
|
|
|
|
JSONObject out2(out.object(s.first));
|
|
|
|
out2.attr("description", s.second.setting->description);
|
|
|
|
JSONPlaceholder out3(out2.placeholder("value"));
|
|
|
|
s.second.setting->toJSON(out3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:41:20 +03:00
|
|
|
void Config::convertToArgs(Args & args, const std::string & category)
|
2017-06-07 17:17:17 +03:00
|
|
|
{
|
|
|
|
for (auto & s : _settings)
|
|
|
|
if (!s.second.isAlias)
|
2017-06-07 19:41:20 +03:00
|
|
|
s.second.setting->convertToArg(args, category);
|
2017-06-07 17:17:17 +03:00
|
|
|
}
|
|
|
|
|
2017-04-13 16:55:38 +03:00
|
|
|
AbstractSetting::AbstractSetting(
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases)
|
|
|
|
: name(name), description(description), aliases(aliases)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:17:53 +02:00
|
|
|
void AbstractSetting::setDefault(const std::string & str)
|
|
|
|
{
|
|
|
|
if (!overriden) set(str);
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:34:47 +03:00
|
|
|
void AbstractSetting::toJSON(JSONPlaceholder & out)
|
|
|
|
{
|
|
|
|
out.write(to_string());
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:41:20 +03:00
|
|
|
void AbstractSetting::convertToArg(Args & args, const std::string & category)
|
2017-06-07 17:17:17 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:34:47 +03:00
|
|
|
template<typename T>
|
|
|
|
void BaseSetting<T>::toJSON(JSONPlaceholder & out)
|
|
|
|
{
|
|
|
|
out.write(value);
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:17:17 +03:00
|
|
|
template<typename T>
|
2017-06-07 19:41:20 +03:00
|
|
|
void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
|
2017-06-07 17:17:17 +03:00
|
|
|
{
|
2017-06-07 19:41:20 +03:00
|
|
|
args.mkFlag()
|
|
|
|
.longName(name)
|
|
|
|
.description(description)
|
|
|
|
.arity(1)
|
2017-11-21 19:50:56 +02:00
|
|
|
.handler([=](std::vector<std::string> ss) { overriden = true; set(ss[0]); })
|
2017-06-07 19:41:20 +03:00
|
|
|
.category(category);
|
2017-06-07 17:17:17 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 17:52:53 +03:00
|
|
|
template<> void BaseSetting<std::string>::set(const std::string & str)
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
|
|
|
value = str;
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:17:53 +02:00
|
|
|
template<> std::string BaseSetting<std::string>::to_string() const
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:52:53 +03:00
|
|
|
template<typename T>
|
|
|
|
void BaseSetting<T>::set(const std::string & str)
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
2017-04-13 18:54:05 +03:00
|
|
|
static_assert(std::is_integral<T>::value, "Integer required.");
|
2017-04-13 21:53:23 +03:00
|
|
|
if (!string2Int(str, value))
|
2017-04-13 16:55:38 +03:00
|
|
|
throw UsageError("setting '%s' has invalid value '%s'", name, str);
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:52:53 +03:00
|
|
|
template<typename T>
|
2019-12-17 18:17:53 +02:00
|
|
|
std::string BaseSetting<T>::to_string() const
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
2017-04-13 18:54:05 +03:00
|
|
|
static_assert(std::is_integral<T>::value, "Integer required.");
|
2017-04-13 16:55:38 +03:00
|
|
|
return std::to_string(value);
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:52:53 +03:00
|
|
|
template<> void BaseSetting<bool>::set(const std::string & str)
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
2017-04-13 17:31:28 +03:00
|
|
|
if (str == "true" || str == "yes" || str == "1")
|
2017-04-20 17:52:53 +03:00
|
|
|
value = true;
|
2017-04-13 17:31:28 +03:00
|
|
|
else if (str == "false" || str == "no" || str == "0")
|
2017-04-20 17:52:53 +03:00
|
|
|
value = false;
|
2017-04-13 17:31:28 +03:00
|
|
|
else
|
|
|
|
throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str);
|
2017-04-13 16:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 18:17:53 +02:00
|
|
|
template<> std::string BaseSetting<bool>::to_string() const
|
2017-04-13 21:53:23 +03:00
|
|
|
{
|
2017-04-20 17:52:53 +03:00
|
|
|
return value ? "true" : "false";
|
2017-04-13 21:53:23 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 19:41:20 +03:00
|
|
|
template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & category)
|
2017-06-07 17:17:17 +03:00
|
|
|
{
|
2017-06-07 19:41:20 +03:00
|
|
|
args.mkFlag()
|
|
|
|
.longName(name)
|
|
|
|
.description(description)
|
2017-11-21 19:50:56 +02:00
|
|
|
.handler([=](std::vector<std::string> ss) { override(true); })
|
2017-06-07 19:41:20 +03:00
|
|
|
.category(category);
|
|
|
|
args.mkFlag()
|
|
|
|
.longName("no-" + name)
|
|
|
|
.description(description)
|
2017-11-21 19:50:56 +02:00
|
|
|
.handler([=](std::vector<std::string> ss) { override(false); })
|
2017-06-07 19:41:20 +03:00
|
|
|
.category(category);
|
2017-06-07 17:17:17 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 17:52:53 +03:00
|
|
|
template<> void BaseSetting<Strings>::set(const std::string & str)
|
2017-04-13 21:53:23 +03:00
|
|
|
{
|
|
|
|
value = tokenizeString<Strings>(str);
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:17:53 +02:00
|
|
|
template<> std::string BaseSetting<Strings>::to_string() const
|
2017-04-13 21:53:23 +03:00
|
|
|
{
|
|
|
|
return concatStringsSep(" ", value);
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:34:47 +03:00
|
|
|
template<> void BaseSetting<Strings>::toJSON(JSONPlaceholder & out)
|
|
|
|
{
|
|
|
|
JSONList list(out.list());
|
|
|
|
for (auto & s : value)
|
|
|
|
list.elem(s);
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:52:53 +03:00
|
|
|
template<> void BaseSetting<StringSet>::set(const std::string & str)
|
2017-04-13 21:53:23 +03:00
|
|
|
{
|
|
|
|
value = tokenizeString<StringSet>(str);
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:17:53 +02:00
|
|
|
template<> std::string BaseSetting<StringSet>::to_string() const
|
2017-04-13 21:53:23 +03:00
|
|
|
{
|
|
|
|
return concatStringsSep(" ", value);
|
2017-04-13 16:55:38 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 18:34:47 +03:00
|
|
|
template<> void BaseSetting<StringSet>::toJSON(JSONPlaceholder & out)
|
|
|
|
{
|
|
|
|
JSONList list(out.list());
|
|
|
|
for (auto & s : value)
|
|
|
|
list.elem(s);
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:52:53 +03: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 18:34:47 +03:00
|
|
|
template class BaseSetting<bool>;
|
2017-04-28 18:13:55 +03:00
|
|
|
template class BaseSetting<std::string>;
|
2017-06-07 17:17:17 +03:00
|
|
|
template class BaseSetting<Strings>;
|
|
|
|
template class BaseSetting<StringSet>;
|
2017-04-13 18:54:05 +03:00
|
|
|
|
2017-04-13 16:55:38 +03:00
|
|
|
void PathSetting::set(const std::string & str)
|
|
|
|
{
|
|
|
|
if (str == "") {
|
|
|
|
if (allowEmpty)
|
|
|
|
value = "";
|
|
|
|
else
|
|
|
|
throw UsageError("setting '%s' cannot be empty", name);
|
|
|
|
} else
|
|
|
|
value = canonPath(str);
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:41:31 +03: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalConfig::getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly)
|
|
|
|
{
|
|
|
|
for (auto & config : *configRegistrations)
|
|
|
|
config->getSettings(res, overridenOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalConfig::resetOverriden()
|
|
|
|
{
|
|
|
|
for (auto & config : *configRegistrations)
|
|
|
|
config->resetOverriden();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalConfig::toJSON(JSONObject & out)
|
|
|
|
{
|
|
|
|
for (auto & config : *configRegistrations)
|
|
|
|
config->toJSON(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:55:38 +03:00
|
|
|
}
|