CmdConfig: Abstract saving and removal of the configuration values into separate methods
This commit is contained in:
committed by
Paul Beckingham
parent
49a7e46eaf
commit
a4d5ab07e9
@@ -46,6 +46,100 @@ CmdConfig::CmdConfig ()
|
|||||||
_displays_id = false;
|
_displays_id = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
bool CmdConfig::setConfigVariable (std::string name, std::string value, bool confirmation /* = false */)
|
||||||
|
{
|
||||||
|
// Read .taskrc (or equivalent)
|
||||||
|
std::vector <std::string> contents;
|
||||||
|
File::read (context.config._original_file, contents);
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
bool change = false;
|
||||||
|
|
||||||
|
std::vector <std::string>::iterator line;
|
||||||
|
for (line = contents.begin (); line != contents.end (); ++line)
|
||||||
|
{
|
||||||
|
// If there is a comment on the line, it must follow the pattern.
|
||||||
|
std::string::size_type comment = line->find ("#");
|
||||||
|
std::string::size_type pos = line->find (name + "=");
|
||||||
|
|
||||||
|
if (pos != std::string::npos &&
|
||||||
|
(comment == std::string::npos ||
|
||||||
|
comment > pos))
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
if (!confirmation ||
|
||||||
|
confirm (format (STRING_CMD_CONFIG_CONFIRM, name, context.config.get (name), value)))
|
||||||
|
{
|
||||||
|
if (comment != std::string::npos)
|
||||||
|
*line = name + "=" + json::encode (value) + " " + line->substr (comment);
|
||||||
|
else
|
||||||
|
*line = name + "=" + json::encode (value);
|
||||||
|
|
||||||
|
change = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not found, so append instead.
|
||||||
|
if (!found &&
|
||||||
|
(!confirmation ||
|
||||||
|
confirm (format (STRING_CMD_CONFIG_CONFIRM2, name, value))))
|
||||||
|
{
|
||||||
|
contents.push_back (name + "=" + json::encode (value));
|
||||||
|
change = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (change)
|
||||||
|
File::write (context.config._original_file, contents);
|
||||||
|
|
||||||
|
return change;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
int CmdConfig::unsetConfigVariable (std::string name, bool confirmation /* = false */)
|
||||||
|
{
|
||||||
|
// Read .taskrc (or equivalent)
|
||||||
|
std::vector <std::string> contents;
|
||||||
|
File::read (context.config._original_file, contents);
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
bool change = false;
|
||||||
|
|
||||||
|
std::vector <std::string>::iterator line;
|
||||||
|
for (line = contents.begin (); line != contents.end (); ++line)
|
||||||
|
{
|
||||||
|
// If there is a comment on the line, it must follow the pattern.
|
||||||
|
std::string::size_type comment = line->find ("#");
|
||||||
|
std::string::size_type pos = line->find (name + "=");
|
||||||
|
|
||||||
|
if (pos != std::string::npos &&
|
||||||
|
(comment == std::string::npos ||
|
||||||
|
comment > pos))
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
|
||||||
|
// Remove name
|
||||||
|
if (!confirmation ||
|
||||||
|
confirm (format (STRING_CMD_CONFIG_CONFIRM3, name)))
|
||||||
|
{
|
||||||
|
*line = "";
|
||||||
|
change = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (change)
|
||||||
|
File::write (context.config._original_file, contents);
|
||||||
|
|
||||||
|
if ( change && found )
|
||||||
|
return 0;
|
||||||
|
else if ( found )
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int CmdConfig::execute (std::string& output)
|
int CmdConfig::execute (std::string& output)
|
||||||
{
|
{
|
||||||
@@ -63,6 +157,9 @@ int CmdConfig::execute (std::string& output)
|
|||||||
{
|
{
|
||||||
bool confirmation = context.config.getBoolean ("confirmation");
|
bool confirmation = context.config.getBoolean ("confirmation");
|
||||||
|
|
||||||
|
bool change = false;
|
||||||
|
bool found = false;
|
||||||
|
|
||||||
std::string name = words[0];
|
std::string name = words[0];
|
||||||
std::string value = "";
|
std::string value = "";
|
||||||
|
|
||||||
@@ -81,76 +178,22 @@ int CmdConfig::execute (std::string& output)
|
|||||||
{
|
{
|
||||||
bool change = false;
|
bool change = false;
|
||||||
|
|
||||||
// Read .taskrc (or equivalent)
|
|
||||||
std::vector <std::string> contents;
|
|
||||||
File::read (context.config._original_file, contents);
|
|
||||||
|
|
||||||
// task config name value
|
// task config name value
|
||||||
// task config name ""
|
// task config name ""
|
||||||
if (words.size () > 1)
|
if (words.size () > 1)
|
||||||
{
|
change = setConfigVariable(name, value, confirmation);
|
||||||
bool found = false;
|
|
||||||
std::vector <std::string>::iterator line;
|
|
||||||
for (line = contents.begin (); line != contents.end (); ++line)
|
|
||||||
{
|
|
||||||
// If there is a comment on the line, it must follow the pattern.
|
|
||||||
std::string::size_type comment = line->find ("#");
|
|
||||||
std::string::size_type pos = line->find (name + "=");
|
|
||||||
|
|
||||||
if (pos != std::string::npos &&
|
|
||||||
(comment == std::string::npos ||
|
|
||||||
comment > pos))
|
|
||||||
{
|
|
||||||
found = true;
|
|
||||||
if (!confirmation ||
|
|
||||||
confirm (format (STRING_CMD_CONFIG_CONFIRM, name, context.config.get (name), value)))
|
|
||||||
{
|
|
||||||
if (comment != std::string::npos)
|
|
||||||
*line = name + "=" + json::encode (value) + " " + line->substr (comment);
|
|
||||||
else
|
|
||||||
*line = name + "=" + json::encode (value);
|
|
||||||
|
|
||||||
change = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not found, so append instead.
|
|
||||||
if (!found &&
|
|
||||||
(!confirmation ||
|
|
||||||
confirm (format (STRING_CMD_CONFIG_CONFIRM2, name, value))))
|
|
||||||
{
|
|
||||||
contents.push_back (name + "=" + json::encode (value));
|
|
||||||
change = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// task config name
|
// task config name
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bool found = false;
|
rc = unsetConfigVariable(name, confirmation);
|
||||||
std::vector <std::string>::iterator line;
|
if (rc == 0)
|
||||||
for (line = contents.begin (); line != contents.end (); ++line)
|
|
||||||
{
|
{
|
||||||
// If there is a comment on the line, it must follow the pattern.
|
change = true;
|
||||||
std::string::size_type comment = line->find ("#");
|
found = true;
|
||||||
std::string::size_type pos = line->find (name + "=");
|
|
||||||
|
|
||||||
if (pos != std::string::npos &&
|
|
||||||
(comment == std::string::npos ||
|
|
||||||
comment > pos))
|
|
||||||
{
|
|
||||||
found = true;
|
|
||||||
|
|
||||||
// Remove name
|
|
||||||
if (!confirmation ||
|
|
||||||
confirm (format (STRING_CMD_CONFIG_CONFIRM3, name)))
|
|
||||||
{
|
|
||||||
*line = "";
|
|
||||||
change = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else if (rc == 1)
|
||||||
|
found = true;
|
||||||
|
|
||||||
if (!found)
|
if (!found)
|
||||||
throw format (STRING_CMD_CONFIG_NO_ENTRY, name);
|
throw format (STRING_CMD_CONFIG_NO_ENTRY, name);
|
||||||
@@ -159,7 +202,6 @@ int CmdConfig::execute (std::string& output)
|
|||||||
// Write .taskrc (or equivalent)
|
// Write .taskrc (or equivalent)
|
||||||
if (change)
|
if (change)
|
||||||
{
|
{
|
||||||
File::write (context.config._original_file, contents);
|
|
||||||
out << format (STRING_CMD_CONFIG_FILE_MOD,
|
out << format (STRING_CMD_CONFIG_FILE_MOD,
|
||||||
context.config._original_file._data)
|
context.config._original_file._data)
|
||||||
<< "\n";
|
<< "\n";
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ class CmdConfig : public Command
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CmdConfig ();
|
CmdConfig ();
|
||||||
|
static bool setConfigVariable (std::string name, std::string value, bool confirmation = false);
|
||||||
|
static int unsetConfigVariable (std::string name, bool confirmation = false);
|
||||||
int execute (std::string&);
|
int execute (std::string&);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user