CmdContext: Remove old-style context during re-definition

This makes it easier to migrate from old-style context.<name>=value
context definition to new context.<name>.read=value and
context.<name>.write=value variables.
This commit is contained in:
Tomas Babej 2021-09-28 21:07:46 -04:00
parent c28aa2f7e2
commit c69cbe8bfc

View file

@ -172,7 +172,8 @@ std::vector <std::string> CmdContext::getContexts ()
//
void CmdContext::defineContext (const std::vector <std::string>& words, std::stringstream& out)
{
auto confirmation = Context::getContext ().config.getBoolean ("confirmation");
auto config = Context::getContext ().config;
bool confirmation = config.getBoolean ("confirmation");
if (words.size () > 2)
{
@ -230,6 +231,12 @@ void CmdContext::defineContext (const std::vector <std::string>& words, std::str
if (valid_write_context)
write_success = CmdConfig::setConfigVariable (name + ".write", value, confirmation);
// Remove old-school context name, if it exists, assuming the read context was defined
if (read_success)
if (config.has (name)) {
CmdConfig::unsetConfigVariable (name, false);
}
if (!read_success and !write_success)
throw format ("Context '{1}' not defined.", words[1]);
else if (!read_success)