2017-04-13 15:55:38 +02:00
|
|
|
|
#include "config.hh"
|
|
|
|
|
#include "args.hh"
|
2017-04-20 17:34:47 +02:00
|
|
|
|
#include "json.hh"
|
2017-04-13 15:55:38 +02:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
void Config::set(const std::string & name, const std::string & value)
|
|
|
|
|
{
|
|
|
|
|
auto i = _settings.find(name);
|
|
|
|
|
if (i == _settings.end())
|
|
|
|
|
throw UsageError("unknown setting '%s'", name);
|
|
|
|
|
i->second.setting->set(value);
|
2017-04-20 14:58:16 +02:00
|
|
|
|
i->second.setting->overriden = 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
|
|
|
|
{
|
|
|
|
|
_settings.emplace(setting->name, Config::SettingData{false, setting});
|
|
|
|
|
for (auto & alias : setting->aliases)
|
|
|
|
|
_settings.emplace(alias, Config::SettingData{true, setting});
|
|
|
|
|
|
|
|
|
|
bool set = false;
|
|
|
|
|
|
|
|
|
|
auto i = initials.find(setting->name);
|
|
|
|
|
if (i != initials.end()) {
|
|
|
|
|
setting->set(i->second);
|
2017-04-20 14:58:16 +02:00
|
|
|
|
setting->overriden = true;
|
2017-04-13 15:55:38 +02:00
|
|
|
|
initials.erase(i);
|
|
|
|
|
set = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto & alias : setting->aliases) {
|
|
|
|
|
auto i = initials.find(alias);
|
|
|
|
|
if (i != initials.end()) {
|
|
|
|
|
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 14:58:16 +02:00
|
|
|
|
setting->overriden = true;
|
2017-04-13 15:55:38 +02:00
|
|
|
|
initials.erase(i);
|
|
|
|
|
set = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 20:53:23 +02:00
|
|
|
|
void Config::warnUnknownSettings()
|
2017-04-13 15:55:38 +02:00
|
|
|
|
{
|
|
|
|
|
for (auto & i : initials)
|
|
|
|
|
warn("unknown setting '%s'", i.first);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 14:58:16 +02:00
|
|
|
|
StringMap Config::getSettings(bool overridenOnly)
|
2017-04-13 15:55:38 +02:00
|
|
|
|
{
|
2017-04-13 20:53:23 +02:00
|
|
|
|
StringMap res;
|
2017-04-13 15:55:38 +02:00
|
|
|
|
for (auto & opt : _settings)
|
2017-04-20 14:58:16 +02:00
|
|
|
|
if (!opt.second.isAlias && (!overridenOnly || opt.second.setting->overriden))
|
2017-04-13 20:53:23 +02:00
|
|
|
|
res.emplace(opt.first, opt.second.setting->to_string());
|
2017-04-13 15:55:38 +02:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 20:53:23 +02:00
|
|
|
|
void Config::applyConfigFile(const Path & path, bool fatal)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (tokens.size() < 2 || tokens[1] != "=")
|
|
|
|
|
throw UsageError("illegal configuration line ‘%1%’ in ‘%2%’", line, path);
|
|
|
|
|
|
|
|
|
|
string name = tokens[0];
|
|
|
|
|
|
|
|
|
|
vector<string>::iterator i = tokens.begin();
|
|
|
|
|
advance(i, 2);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
set(name, concatStringsSep(" ", Strings(i, tokens.end()))); // FIXME: slow
|
|
|
|
|
} catch (UsageError & e) {
|
|
|
|
|
if (fatal) throw;
|
|
|
|
|
warn("in configuration file '%s': %s", path, e.what());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} catch (SysError &) { }
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 14:58:16 +02:00
|
|
|
|
void Config::resetOverriden()
|
|
|
|
|
{
|
|
|
|
|
for (auto & s : _settings)
|
|
|
|
|
s.second.setting->overriden = false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 17:34:47 +02: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-04-13 15:55:38 +02:00
|
|
|
|
AbstractSetting::AbstractSetting(
|
|
|
|
|
const std::string & name,
|
|
|
|
|
const std::string & description,
|
|
|
|
|
const std::set<std::string> & aliases)
|
|
|
|
|
: name(name), description(description), aliases(aliases)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 17:34:47 +02:00
|
|
|
|
void AbstractSetting::toJSON(JSONPlaceholder & out)
|
|
|
|
|
{
|
|
|
|
|
out.write(to_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void BaseSetting<T>::toJSON(JSONPlaceholder & out)
|
|
|
|
|
{
|
|
|
|
|
out.write(value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<> void BaseSetting<std::string>::set(const std::string & str)
|
2017-04-13 15:55:38 +02:00
|
|
|
|
{
|
|
|
|
|
value = str;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<> std::string BaseSetting<std::string>::to_string()
|
2017-04-13 15:55:38 +02:00
|
|
|
|
{
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<typename T>
|
|
|
|
|
void BaseSetting<T>::set(const std::string & str)
|
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 20:53:23 +02:00
|
|
|
|
if (!string2Int(str, value))
|
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>
|
|
|
|
|
std::string BaseSetting<T>::to_string()
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<> void BaseSetting<bool>::set(const std::string & str)
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<> std::string BaseSetting<bool>::to_string()
|
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-04-20 16:52:53 +02:00
|
|
|
|
template<> void BaseSetting<Strings>::set(const std::string & str)
|
2017-04-13 20:53:23 +02:00
|
|
|
|
{
|
|
|
|
|
value = tokenizeString<Strings>(str);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<> std::string BaseSetting<Strings>::to_string()
|
2017-04-13 20:53:23 +02:00
|
|
|
|
{
|
|
|
|
|
return concatStringsSep(" ", value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 17:34:47 +02:00
|
|
|
|
template<> void BaseSetting<Strings>::toJSON(JSONPlaceholder & out)
|
|
|
|
|
{
|
|
|
|
|
JSONList list(out.list());
|
|
|
|
|
for (auto & s : value)
|
|
|
|
|
list.elem(s);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<> void BaseSetting<StringSet>::set(const std::string & str)
|
2017-04-13 20:53:23 +02:00
|
|
|
|
{
|
|
|
|
|
value = tokenizeString<StringSet>(str);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 16:52:53 +02:00
|
|
|
|
template<> std::string BaseSetting<StringSet>::to_string()
|
2017-04-13 20:53:23 +02:00
|
|
|
|
{
|
|
|
|
|
return concatStringsSep(" ", value);
|
2017-04-13 15:55:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 17:34:47 +02:00
|
|
|
|
template<> void BaseSetting<StringSet>::toJSON(JSONPlaceholder & out)
|
|
|
|
|
{
|
|
|
|
|
JSONList list(out.list());
|
|
|
|
|
for (auto & s : value)
|
|
|
|
|
list.elem(s);
|
|
|
|
|
}
|
|
|
|
|
|
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-13 17:54:05 +02:00
|
|
|
|
|
2017-04-13 15:55:38 +02: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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|