CmdConfig: Be more strict when matching confiuration variables

Allow only leading spaces in front of configuration variables, as
opposed to arbitrary strings.

This prevents matching variables like "report.list.context=" when one is
seeking to modify "context=".
This commit is contained in:
Tomas Babej 2021-08-21 00:45:12 -04:00
parent 9768fb8bca
commit e2e184b8d4

View file

@ -64,23 +64,27 @@ bool CmdConfig::setConfigVariable (
for (auto& line : contents)
{
// Get l-trimmed version of the line
auto trimmed_line = trim (line, " ");
// If there is a comment on the line, it must follow the pattern.
auto comment = line.find ('#');
auto pos = line.find (name + '=');
auto pos = trimmed_line.find (name + '=');
if (pos != std::string::npos &&
(comment == std::string::npos ||
comment > pos))
// TODO: Use std::regex here
if (pos == 0)
{
found = true;
if (!confirmation ||
confirm (format ("Are you sure you want to change the value of '{1}' from '{2}' to '{3}'?", name, Context::getContext ().config.get (name), value)))
{
line = name + '=' + json::encode (value);
auto new_line = line.substr (0, pos + name.length () + 1) + json::encode (value);
if (comment != std::string::npos)
line += ' ' + line.substr (comment);
// Rewrite the line
line = new_line;
change = true;
}
}
@ -115,13 +119,13 @@ int CmdConfig::unsetConfigVariable (const std::string& name, bool confirmation /
{
auto lineDeleted = false;
// If there is a comment on the line, it must follow the pattern.
auto comment = line->find ('#');
auto pos = line->find (name + '=');
// Get l-trimmed version of the line
if (pos != std::string::npos &&
(comment == std::string::npos ||
comment > pos))
// If there is a comment on the line, it must follow the pattern.
auto pos = trim (*line, " ").find (name + '=');
// TODO: Use std::regex here
if (pos == 0)
{
found = true;