mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
CmdConfig: Abstract saving and removal of the configuration values into separate methods
This commit is contained in:
parent
49a7e46eaf
commit
a4d5ab07e9
2 changed files with 106 additions and 62 deletions
|
@ -47,49 +47,15 @@ CmdConfig::CmdConfig ()
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int CmdConfig::execute (std::string& output)
|
||||
bool CmdConfig::setConfigVariable (std::string name, std::string value, bool confirmation /* = false */)
|
||||
{
|
||||
int rc = 0;
|
||||
std::stringstream out;
|
||||
|
||||
// Get the non-attribute, non-fancy command line arguments.
|
||||
std::vector <std::string> words = context.cli.getWords ();
|
||||
|
||||
// Support:
|
||||
// task config name value # set name to value
|
||||
// task config name "" # set name to blank
|
||||
// task config name # remove name
|
||||
if (words.size ())
|
||||
{
|
||||
bool confirmation = context.config.getBoolean ("confirmation");
|
||||
|
||||
std::string name = words[0];
|
||||
std::string value = "";
|
||||
|
||||
if (words.size () > 1)
|
||||
{
|
||||
for (unsigned int i = 1; i < words.size (); ++i)
|
||||
{
|
||||
if (i > 1)
|
||||
value += " ";
|
||||
|
||||
value += words[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (name != "")
|
||||
{
|
||||
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 ""
|
||||
if (words.size () > 1)
|
||||
{
|
||||
bool found = false;
|
||||
bool change = false;
|
||||
|
||||
std::vector <std::string>::iterator line;
|
||||
for (line = contents.begin (); line != contents.end (); ++line)
|
||||
{
|
||||
|
@ -123,12 +89,23 @@ int CmdConfig::execute (std::string& output)
|
|||
contents.push_back (name + "=" + json::encode (value));
|
||||
change = true;
|
||||
}
|
||||
|
||||
if (change)
|
||||
File::write (context.config._original_file, contents);
|
||||
|
||||
return change;
|
||||
}
|
||||
|
||||
// task config name
|
||||
else
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
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)
|
||||
{
|
||||
|
@ -152,6 +129,72 @@ int CmdConfig::execute (std::string& output)
|
|||
}
|
||||
}
|
||||
|
||||
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 rc = 0;
|
||||
std::stringstream out;
|
||||
|
||||
// Get the non-attribute, non-fancy command line arguments.
|
||||
std::vector <std::string> words = context.cli.getWords ();
|
||||
|
||||
// Support:
|
||||
// task config name value # set name to value
|
||||
// task config name "" # set name to blank
|
||||
// task config name # remove name
|
||||
if (words.size ())
|
||||
{
|
||||
bool confirmation = context.config.getBoolean ("confirmation");
|
||||
|
||||
bool change = false;
|
||||
bool found = false;
|
||||
|
||||
std::string name = words[0];
|
||||
std::string value = "";
|
||||
|
||||
if (words.size () > 1)
|
||||
{
|
||||
for (unsigned int i = 1; i < words.size (); ++i)
|
||||
{
|
||||
if (i > 1)
|
||||
value += " ";
|
||||
|
||||
value += words[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (name != "")
|
||||
{
|
||||
bool change = false;
|
||||
|
||||
// task config name value
|
||||
// task config name ""
|
||||
if (words.size () > 1)
|
||||
change = setConfigVariable(name, value, confirmation);
|
||||
|
||||
// task config name
|
||||
else
|
||||
{
|
||||
rc = unsetConfigVariable(name, confirmation);
|
||||
if (rc == 0)
|
||||
{
|
||||
change = true;
|
||||
found = true;
|
||||
}
|
||||
else if (rc == 1)
|
||||
found = true;
|
||||
|
||||
if (!found)
|
||||
throw format (STRING_CMD_CONFIG_NO_ENTRY, name);
|
||||
}
|
||||
|
@ -159,7 +202,6 @@ int CmdConfig::execute (std::string& output)
|
|||
// Write .taskrc (or equivalent)
|
||||
if (change)
|
||||
{
|
||||
File::write (context.config._original_file, contents);
|
||||
out << format (STRING_CMD_CONFIG_FILE_MOD,
|
||||
context.config._original_file._data)
|
||||
<< "\n";
|
||||
|
|
|
@ -34,6 +34,8 @@ class CmdConfig : public Command
|
|||
{
|
||||
public:
|
||||
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&);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue