2020-06-16 14:19:49 +00:00
|
|
|
#include <cassert>
|
2017-04-13 13:55:38 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "types.hh"
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* The Config class provides Nix runtime configurations.
|
|
|
|
*
|
|
|
|
* What is a Configuration?
|
|
|
|
* A collection of uniquely named Settings.
|
|
|
|
*
|
|
|
|
* What is a Setting?
|
|
|
|
* Each property that you can set in a configuration corresponds to a
|
|
|
|
* `Setting`. A setting records value and description of a property
|
|
|
|
* with a default and optional aliases.
|
|
|
|
*
|
|
|
|
* A valid configuration consists of settings that are registered to a
|
|
|
|
* `Config` object instance:
|
|
|
|
*
|
|
|
|
* Config config;
|
|
|
|
* Setting<std::string> systemSetting{&config, "x86_64-linux", "system", "the current system"};
|
|
|
|
*
|
|
|
|
* The above creates a `Config` object and registers a setting called "system"
|
|
|
|
* via the variable `systemSetting` with it. The setting defaults to the string
|
|
|
|
* "x86_64-linux", it's description is "the current system". All of the
|
|
|
|
* registered settings can then be accessed as shown below:
|
|
|
|
*
|
|
|
|
* std::map<std::string, Config::SettingInfo> settings;
|
|
|
|
* config.getSettings(settings);
|
|
|
|
* config["system"].description == "the current system"
|
|
|
|
* config["system"].value == "x86_64-linux"
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The above retrieves all currently known settings from the `Config` object
|
|
|
|
* and adds them to the `settings` map.
|
|
|
|
*/
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
class Args;
|
|
|
|
class AbstractSetting;
|
2017-04-20 15:34:47 +00:00
|
|
|
class JSONPlaceholder;
|
|
|
|
class JSONObject;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
class AbstractConfig
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
StringMap unknownSettings;
|
|
|
|
|
|
|
|
AbstractConfig(const StringMap & initials = {})
|
|
|
|
: unknownSettings(initials)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Sets the value referenced by `name` to `value`. Returns true if the
|
|
|
|
* setting is known, false otherwise.
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
virtual bool set(const std::string & name, const std::string & value) = 0;
|
|
|
|
|
|
|
|
struct SettingInfo
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
std::string description;
|
|
|
|
};
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Adds the currently known settings to the given result map `res`.
|
|
|
|
* - res: map to store settings in
|
|
|
|
* - overridenOnly: when set to true only overridden settings will be added to `res`
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
virtual void getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly = false) = 0;
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Parses the configuration in `contents` and applies it
|
|
|
|
* - contents: configuration contents to be parsed and applied
|
|
|
|
* - path: location of the configuration file
|
|
|
|
*/
|
2020-05-27 12:39:02 +00:00
|
|
|
void applyConfig(const std::string & contents, const std::string & path = "<unknown>");
|
2020-05-27 14:37:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a nix configuration file
|
|
|
|
* - path: the location of the config file to apply
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
void applyConfigFile(const Path & path);
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Resets the `overridden` flag of all Settings
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
virtual void resetOverriden() = 0;
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Outputs all settings to JSON
|
|
|
|
* - out: JSONObject to write the configuration to
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
virtual void toJSON(JSONObject & out) = 0;
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Converts settings to `Args` to be used on the command line interface
|
|
|
|
* - args: args to write to
|
|
|
|
* - category: category of the settings
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
virtual void convertToArgs(Args & args, const std::string & category) = 0;
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Logs a warning for each unregistered setting
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
void warnUnknownSettings();
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Re-applies all previously attempted changes to unknown settings
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
void reapplyUnknownSettings();
|
|
|
|
};
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
/* A class to simplify providing configuration settings. The typical
|
|
|
|
use is to inherit Config and add Setting<T> members:
|
|
|
|
|
|
|
|
class MyClass : private Config
|
|
|
|
{
|
|
|
|
Setting<int> foo{this, 123, "foo", "the number of foos to use"};
|
|
|
|
Setting<std::string> bar{this, "blabla", "bar", "the name of the bar"};
|
|
|
|
|
|
|
|
MyClass() : Config(readConfigFile("/etc/my-app.conf"))
|
|
|
|
{
|
|
|
|
std::cout << foo << "\n"; // will print 123 unless overriden
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
class Config : public AbstractConfig
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
friend class AbstractSetting;
|
|
|
|
|
2017-06-07 14:49:54 +00:00
|
|
|
public:
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
struct SettingData
|
|
|
|
{
|
2017-05-03 14:08:48 +00:00
|
|
|
bool isAlias;
|
2017-04-13 13:55:38 +00:00
|
|
|
AbstractSetting * setting;
|
2017-05-03 14:08:48 +00:00
|
|
|
SettingData(bool isAlias, AbstractSetting * setting)
|
|
|
|
: isAlias(isAlias), setting(setting)
|
|
|
|
{ }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
2017-06-07 14:49:54 +00:00
|
|
|
typedef std::map<std::string, SettingData> Settings;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Settings _settings;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
Config(const StringMap & initials = {})
|
|
|
|
: AbstractConfig(initials)
|
2017-04-13 13:55:38 +00:00
|
|
|
{ }
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
bool set(const std::string & name, const std::string & value) override;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
void addSetting(AbstractSetting * setting);
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
void getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly = false) override;
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
void resetOverriden() override;
|
2017-04-20 12:58:16 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
void toJSON(JSONObject & out) override;
|
2017-04-20 15:34:47 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
void convertToArgs(Args & args, const std::string & category) override;
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AbstractSetting
|
|
|
|
{
|
|
|
|
friend class Config;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
const std::string name;
|
|
|
|
const std::string description;
|
|
|
|
const std::set<std::string> aliases;
|
|
|
|
|
|
|
|
int created = 123;
|
|
|
|
|
2017-04-20 12:58:16 +00:00
|
|
|
bool overriden = false;
|
|
|
|
|
2019-12-17 16:17:53 +00:00
|
|
|
void setDefault(const std::string & str);
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
AbstractSetting(
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases);
|
|
|
|
|
|
|
|
virtual ~AbstractSetting()
|
|
|
|
{
|
|
|
|
// Check against a gcc miscompilation causing our constructor
|
2017-04-20 12:58:16 +00:00
|
|
|
// not to run (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80431).
|
2017-04-13 13:55:38 +00:00
|
|
|
assert(created == 123);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void set(const std::string & value) = 0;
|
|
|
|
|
2019-12-17 16:17:53 +00:00
|
|
|
virtual std::string to_string() const = 0;
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2017-04-20 15:34:47 +00:00
|
|
|
virtual void toJSON(JSONPlaceholder & out);
|
|
|
|
|
2017-06-07 16:41:20 +00:00
|
|
|
virtual void convertToArg(Args & args, const std::string & category);
|
2017-06-07 14:17:17 +00:00
|
|
|
|
2019-12-17 16:17:53 +00:00
|
|
|
bool isOverriden() const { return overriden; }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A setting of type T. */
|
2017-04-20 14:52:53 +00:00
|
|
|
template<typename T>
|
|
|
|
class BaseSetting : public AbstractSetting
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
T value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-04-20 14:52:53 +00:00
|
|
|
BaseSetting(const T & def,
|
2017-04-13 13:55:38 +00:00
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases = {})
|
|
|
|
: AbstractSetting(name, description, aliases)
|
|
|
|
, value(def)
|
2017-04-20 14:52:53 +00:00
|
|
|
{ }
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
operator const T &() const { return value; }
|
2017-04-13 18:53:23 +00:00
|
|
|
operator T &() { return value; }
|
|
|
|
const T & get() const { return value; }
|
2017-04-13 13:55:38 +00:00
|
|
|
bool operator ==(const T & v2) const { return value == v2; }
|
|
|
|
bool operator !=(const T & v2) const { return value != v2; }
|
2017-04-20 14:52:53 +00:00
|
|
|
void operator =(const T & v) { assign(v); }
|
|
|
|
virtual void assign(const T & v) { value = v; }
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
void set(const std::string & str) override;
|
|
|
|
|
2017-11-21 17:50:56 +00:00
|
|
|
virtual void override(const T & v)
|
|
|
|
{
|
|
|
|
overriden = true;
|
|
|
|
value = v;
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:17:53 +00:00
|
|
|
std::string to_string() const override;
|
2017-04-20 15:34:47 +00:00
|
|
|
|
2017-06-07 16:41:20 +00:00
|
|
|
void convertToArg(Args & args, const std::string & category) override;
|
2017-06-07 14:17:17 +00:00
|
|
|
|
2017-04-20 15:34:47 +00:00
|
|
|
void toJSON(JSONPlaceholder & out) override;
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-04-20 14:52:53 +00:00
|
|
|
std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt)
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
str << (const T &) opt;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
template<typename T>
|
2017-04-20 14:52:53 +00:00
|
|
|
bool operator ==(const T & v1, const BaseSetting<T> & v2) { return v1 == (const T &) v2; }
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Setting : public BaseSetting<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Setting(Config * options,
|
|
|
|
const T & def,
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases = {})
|
|
|
|
: BaseSetting<T>(def, name, description, aliases)
|
|
|
|
{
|
|
|
|
options->addSetting(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator =(const T & v) { this->assign(v); }
|
|
|
|
};
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
/* A special setting for Paths. These are automatically canonicalised
|
|
|
|
(e.g. "/foo//bar/" becomes "/foo/bar"). */
|
2017-04-20 14:52:53 +00:00
|
|
|
class PathSetting : public BaseSetting<Path>
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
bool allowEmpty;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
PathSetting(Config * options,
|
|
|
|
bool allowEmpty,
|
|
|
|
const Path & def,
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases = {})
|
2017-04-20 14:52:53 +00:00
|
|
|
: BaseSetting<Path>(def, name, description, aliases)
|
2017-04-13 13:55:38 +00:00
|
|
|
, allowEmpty(allowEmpty)
|
|
|
|
{
|
2017-04-20 14:52:53 +00:00
|
|
|
options->addSetting(this);
|
2017-04-13 13:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set(const std::string & str) override;
|
|
|
|
|
|
|
|
Path operator +(const char * p) const { return value + p; }
|
2017-04-20 14:52:53 +00:00
|
|
|
|
|
|
|
void operator =(const Path & v) { this->assign(v); }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
struct GlobalConfig : public AbstractConfig
|
|
|
|
{
|
|
|
|
typedef std::vector<Config*> ConfigRegistrations;
|
|
|
|
static ConfigRegistrations * configRegistrations;
|
|
|
|
|
|
|
|
bool set(const std::string & name, const std::string & value) override;
|
|
|
|
|
|
|
|
void getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly = false) override;
|
|
|
|
|
|
|
|
void resetOverriden() override;
|
|
|
|
|
|
|
|
void toJSON(JSONObject & out) override;
|
|
|
|
|
|
|
|
void convertToArgs(Args & args, const std::string & category) override;
|
|
|
|
|
|
|
|
struct Register
|
|
|
|
{
|
|
|
|
Register(Config * config);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
extern GlobalConfig globalConfig;
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
}
|