Attempt to move the configuration setup logic into Config, so it's centralised.

This commit is contained in:
dg
2023-05-13 12:42:31 +00:00
parent 5568ac382f
commit dd8cc7bfd4
4 changed files with 85 additions and 72 deletions

View File

@@ -517,3 +517,71 @@ std::shared_ptr<Decoder>& Config::getDecoder()
}
return _decoder;
}
void Config::initialise(std::set<std::string> options,
std::vector<std::pair<std::string, std::string>> overrides)
{
/* Start fresh. */
/* TODO: can't do this yet without refactoring loads of stuff. */
// clear();
/* First apply any value overrides (in order). We need to set the up front
* because the options may depend on them. */
auto applyOverrides = [&]()
{
for (auto [k, v] : overrides)
globalConfig().set(k, v);
};
applyOverrides();
/* First apply any standalone options. After each one, reapply the overrides
* in case the option changed them. */
for (auto& option : globalConfig()->option())
{
if (options.find(option.name()) != options.end())
{
globalConfig().applyOption(option);
applyOverrides();
options.erase(option.name());
}
}
/* Add any config contributed by the flux and image readers, plus overrides.
*/
if (globalConfig().hasFluxSource())
globalConfig()->MergeFrom(
globalConfig().getFluxSource()->getExtraConfig());
if (globalConfig().hasImageReader())
globalConfig()->MergeFrom(
globalConfig().getImageReader()->getExtraConfig());
applyOverrides();
/* Then apply any default options in groups, likewise applying the
* overrides. */
for (auto& group : globalConfig()->option_group())
{
const OptionProto* defaultOption = &*group.option().begin();
bool isSet = false;
for (auto& option : group.option())
{
if (options.find(option.name()) != options.end())
{
defaultOption = &option;
options.erase(option.name());
}
}
globalConfig().applyOption(*defaultOption);
applyOverrides();
}
if (!options.empty())
error("--{} is not a known flag or format option; try --help",
*options.begin());
}

View File

@@ -63,6 +63,11 @@ public:
void clear();
/* Set up the entire configuration in one go. */
void initialise(std::set<std::string> options,
std::vector<std::pair<std::string, std::string>> overrides);
/* Merge in one config file. */
void readConfigFile(std::string filename);

View File

@@ -171,65 +171,7 @@ std::vector<std::string> FlagGroup::parseFlagsWithFilenames(int argc,
index++;
}
/* First apply any value overrides (in order). We need to set the up front
* because the options may depend on them. */
auto applyOverrides = [&]()
{
for (auto [k, v] : overrides)
globalConfig().set(k, v);
};
applyOverrides();
/* First apply any standalone options. After each one, reapply the overrides
* in case the option changed them. */
for (auto& option : globalConfig()->option())
{
if (options.find(option.name()) != options.end())
{
globalConfig().applyOption(option);
applyOverrides();
options.erase(option.name());
}
}
/* Add any config contributed by the flux and image readers, plus overrides.
*/
if (globalConfig().hasFluxSource())
globalConfig()->MergeFrom(
globalConfig().getFluxSource()->getExtraConfig());
if (globalConfig().hasImageReader())
globalConfig()->MergeFrom(
globalConfig().getImageReader()->getExtraConfig());
applyOverrides();
/* Then apply any default options in groups, likewise applying the
* overrides. */
for (auto& group : globalConfig()->option_group())
{
const OptionProto* defaultOption = &*group.option().begin();
bool isSet = false;
for (auto& option : group.option())
{
if (options.find(option.name()) != options.end())
{
defaultOption = &option;
options.erase(option.name());
}
}
globalConfig().applyOption(*defaultOption);
applyOverrides();
}
if (!options.empty())
error("--{} is not a known flag or format option; try --help",
*options.begin());
globalConfig().initialise(options, overrides);
return filenames;
}

View File

@@ -186,6 +186,7 @@ public:
/* Merge in any custom config. */
std::vector<std::pair<std::string, std::string>> overrides;
for (auto setting : split(_extraConfiguration, '\n'))
{
setting = trimWhitespace(setting);
@@ -199,7 +200,7 @@ public:
{
auto key = setting.substr(0, equals);
auto value = setting.substr(equals + 1);
setProtoByString(globalConfig(), key, value);
overrides.push_back(std::make_pair(key, value));
}
else
globalConfig().readConfigFile(setting);
@@ -207,28 +208,21 @@ public:
/* Apply any format options. */
std::set<std::string> options;
for (const auto& e : _formatOptions)
{
if (e.first == formatName)
{
try
{
globalConfig().applyOption(
globalConfig().findOption(e.second));
}
catch (const OptionNotFoundException e)
{
}
}
options.insert(e.second);
}
/* Locate the device, if any. */
auto serial = _selectedDevice;
if (!serial.empty() && (serial[0] == '/'))
setProtoByString(globalConfig(), "usb.greaseweazle.port", serial);
overrides.push_back(
std::make_pair("usb.greaseweazle.port", serial));
else
setProtoByString(globalConfig(), "usb.serial", serial);
overrides.push_back(std::make_pair("usb.serial", serial));
ClearLog();
@@ -264,6 +258,10 @@ public:
break;
}
}
/* Resolve the rest of the stuff. */
globalConfig().initialise(options, overrides);
}
const wxBitmap GetBitmap() override