mirror of
https://github.com/NixOS/nix
synced 2025-07-05 20:41:47 +02:00
Merge pull request #10951 from obsidiansystems/load-just-one-config
Small global config refactors
This commit is contained in:
commit
fda4c78921
23 changed files with 130 additions and 98 deletions
|
@ -6,7 +6,7 @@
|
|||
#include <strings.h> // for strcasecmp
|
||||
|
||||
#include "archive.hh"
|
||||
#include "config.hh"
|
||||
#include "config-global.hh"
|
||||
#include "posix-source-accessor.hh"
|
||||
#include "source-path.hh"
|
||||
#include "file-system.hh"
|
||||
|
|
67
src/libutil/config-global.cc
Normal file
67
src/libutil/config-global.cc
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include "config-global.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
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 overriddenOnly)
|
||||
{
|
||||
for (auto & config : *configRegistrations)
|
||||
config->getSettings(res, overriddenOnly);
|
||||
}
|
||||
|
||||
void GlobalConfig::resetOverridden()
|
||||
{
|
||||
for (auto & config : *configRegistrations)
|
||||
config->resetOverridden();
|
||||
}
|
||||
|
||||
nlohmann::json GlobalConfig::toJSON()
|
||||
{
|
||||
auto res = nlohmann::json::object();
|
||||
for (const auto & config : *configRegistrations)
|
||||
res.update(config->toJSON());
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string GlobalConfig::toKeyValue()
|
||||
{
|
||||
std::string res;
|
||||
std::map<std::string, Config::SettingInfo> settings;
|
||||
globalConfig.getSettings(settings);
|
||||
for (const auto & s : settings)
|
||||
res += fmt("%s = %s\n", s.first, s.second.value);
|
||||
return res;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
ExperimentalFeatureSettings experimentalFeatureSettings;
|
||||
|
||||
static GlobalConfig::Register rSettings(&experimentalFeatureSettings);
|
||||
|
||||
}
|
33
src/libutil/config-global.hh
Normal file
33
src/libutil/config-global.hh
Normal file
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include "config.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
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 overriddenOnly = false) override;
|
||||
|
||||
void resetOverridden() override;
|
||||
|
||||
nlohmann::json toJSON() override;
|
||||
|
||||
std::string toKeyValue() override;
|
||||
|
||||
void convertToArgs(Args & args, const std::string & category) override;
|
||||
|
||||
struct Register
|
||||
{
|
||||
Register(Config * config);
|
||||
};
|
||||
};
|
||||
|
||||
extern GlobalConfig globalConfig;
|
||||
|
||||
}
|
|
@ -443,67 +443,6 @@ void OptionalPathSetting::operator =(const std::optional<Path> & v)
|
|||
this->assign(v);
|
||||
}
|
||||
|
||||
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 overriddenOnly)
|
||||
{
|
||||
for (auto & config : *configRegistrations)
|
||||
config->getSettings(res, overriddenOnly);
|
||||
}
|
||||
|
||||
void GlobalConfig::resetOverridden()
|
||||
{
|
||||
for (auto & config : *configRegistrations)
|
||||
config->resetOverridden();
|
||||
}
|
||||
|
||||
nlohmann::json GlobalConfig::toJSON()
|
||||
{
|
||||
auto res = nlohmann::json::object();
|
||||
for (const auto & config : *configRegistrations)
|
||||
res.update(config->toJSON());
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string GlobalConfig::toKeyValue()
|
||||
{
|
||||
std::string res;
|
||||
std::map<std::string, Config::SettingInfo> settings;
|
||||
globalConfig.getSettings(settings);
|
||||
for (const auto & s : settings)
|
||||
res += fmt("%s = %s\n", s.first, s.second.value);
|
||||
return res;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
ExperimentalFeatureSettings experimentalFeatureSettings;
|
||||
|
||||
static GlobalConfig::Register rSettings(&experimentalFeatureSettings);
|
||||
|
||||
bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const
|
||||
{
|
||||
auto & f = experimentalFeatures.get();
|
||||
|
|
|
@ -375,31 +375,6 @@ public:
|
|||
void operator =(const std::optional<Path> & v);
|
||||
};
|
||||
|
||||
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 overriddenOnly = false) override;
|
||||
|
||||
void resetOverridden() override;
|
||||
|
||||
nlohmann::json toJSON() override;
|
||||
|
||||
std::string toKeyValue() override;
|
||||
|
||||
void convertToArgs(Args & args, const std::string & category) override;
|
||||
|
||||
struct Register
|
||||
{
|
||||
Register(Config * config);
|
||||
};
|
||||
};
|
||||
|
||||
extern GlobalConfig globalConfig;
|
||||
|
||||
|
||||
struct ExperimentalFeatureSettings : Config {
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include <fcntl.h>
|
||||
|
||||
#include "error.hh"
|
||||
#include "config.hh"
|
||||
#include "config-global.hh"
|
||||
#include "fs-sink.hh"
|
||||
|
||||
#if _WIN32
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "environment-variables.hh"
|
||||
#include "terminal.hh"
|
||||
#include "util.hh"
|
||||
#include "config.hh"
|
||||
#include "config-global.hh"
|
||||
#include "source-path.hh"
|
||||
#include "position.hh"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue