mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-27 00:57:19 +02:00
CmdConfig: Code cleanup
This commit is contained in:
parent
bd1661c2b0
commit
8e6fd5d979
2 changed files with 26 additions and 23 deletions
|
@ -53,14 +53,17 @@ CmdConfig::CmdConfig ()
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
bool CmdConfig::setConfigVariable (std::string name, std::string value, bool confirmation /* = false */)
|
bool CmdConfig::setConfigVariable (
|
||||||
|
const std::string& name,
|
||||||
|
const std::string& value,
|
||||||
|
bool confirmation /* = false */)
|
||||||
{
|
{
|
||||||
// Read .taskrc (or equivalent)
|
// Read .taskrc (or equivalent)
|
||||||
std::vector <std::string> contents;
|
std::vector <std::string> contents;
|
||||||
File::read (context.config._original_file, contents);
|
File::read (context.config._original_file, contents);
|
||||||
|
|
||||||
bool found = false;
|
auto found = false;
|
||||||
bool change = false;
|
auto change = false;
|
||||||
|
|
||||||
for (auto& line : contents)
|
for (auto& line : contents)
|
||||||
{
|
{
|
||||||
|
@ -76,10 +79,10 @@ bool CmdConfig::setConfigVariable (std::string name, std::string value, bool con
|
||||||
if (!confirmation ||
|
if (!confirmation ||
|
||||||
confirm (format (STRING_CMD_CONFIG_CONFIRM, name, context.config.get (name), value)))
|
confirm (format (STRING_CMD_CONFIG_CONFIRM, name, context.config.get (name), value)))
|
||||||
{
|
{
|
||||||
|
line = name + '=' + json::encode (value);
|
||||||
|
|
||||||
if (comment != std::string::npos)
|
if (comment != std::string::npos)
|
||||||
line = name + '=' + json::encode (value) + ' ' + line.substr (comment);
|
line += ' ' + line.substr (comment);
|
||||||
else
|
|
||||||
line = name + '=' + json::encode (value);
|
|
||||||
|
|
||||||
change = true;
|
change = true;
|
||||||
}
|
}
|
||||||
|
@ -87,8 +90,8 @@ bool CmdConfig::setConfigVariable (std::string name, std::string value, bool con
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not found, so append instead.
|
// Not found, so append instead.
|
||||||
if (!found &&
|
if (! found &&
|
||||||
(!confirmation ||
|
(! confirmation ||
|
||||||
confirm (format (STRING_CMD_CONFIG_CONFIRM2, name, value))))
|
confirm (format (STRING_CMD_CONFIG_CONFIRM2, name, value))))
|
||||||
{
|
{
|
||||||
contents.push_back (name + '=' + json::encode (value));
|
contents.push_back (name + '=' + json::encode (value));
|
||||||
|
@ -102,18 +105,18 @@ bool CmdConfig::setConfigVariable (std::string name, std::string value, bool con
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int CmdConfig::unsetConfigVariable (std::string name, bool confirmation /* = false */)
|
int CmdConfig::unsetConfigVariable (const std::string& name, bool confirmation /* = false */)
|
||||||
{
|
{
|
||||||
// Read .taskrc (or equivalent)
|
// Read .taskrc (or equivalent)
|
||||||
std::vector <std::string> contents;
|
std::vector <std::string> contents;
|
||||||
File::read (context.config._original_file, contents);
|
File::read (context.config._original_file, contents);
|
||||||
|
|
||||||
bool found = false;
|
auto found = false;
|
||||||
bool change = false;
|
auto change = false;
|
||||||
|
|
||||||
for (auto line = contents.begin (); line != contents.end (); )
|
for (auto line = contents.begin (); line != contents.end (); )
|
||||||
{
|
{
|
||||||
bool lineDeleted = false;
|
auto lineDeleted = false;
|
||||||
|
|
||||||
// If there is a comment on the line, it must follow the pattern.
|
// If there is a comment on the line, it must follow the pattern.
|
||||||
auto comment = line->find ('#');
|
auto comment = line->find ('#');
|
||||||
|
@ -143,9 +146,9 @@ int CmdConfig::unsetConfigVariable (std::string name, bool confirmation /* = fal
|
||||||
if (change)
|
if (change)
|
||||||
File::write (context.config._original_file, contents);
|
File::write (context.config._original_file, contents);
|
||||||
|
|
||||||
if ( change && found )
|
if (change && found)
|
||||||
return 0;
|
return 0;
|
||||||
else if ( found )
|
else if (found)
|
||||||
return 1;
|
return 1;
|
||||||
else
|
else
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -154,7 +157,7 @@ int CmdConfig::unsetConfigVariable (std::string name, bool confirmation /* = fal
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int CmdConfig::execute (std::string& output)
|
int CmdConfig::execute (std::string& output)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
auto rc = 0;
|
||||||
std::stringstream out;
|
std::stringstream out;
|
||||||
|
|
||||||
// Get the non-attribute, non-fancy command line arguments.
|
// Get the non-attribute, non-fancy command line arguments.
|
||||||
|
@ -166,10 +169,10 @@ int CmdConfig::execute (std::string& output)
|
||||||
// task config name # remove name
|
// task config name # remove name
|
||||||
if (words.size ())
|
if (words.size ())
|
||||||
{
|
{
|
||||||
bool confirmation = context.config.getBoolean ("confirmation");
|
auto confirmation = context.config.getBoolean ("confirmation");
|
||||||
bool found = false;
|
auto found = false;
|
||||||
|
|
||||||
std::string name = words[0];
|
auto name = words[0];
|
||||||
std::string value = "";
|
std::string value = "";
|
||||||
|
|
||||||
// Join the remaining words into config variable's value
|
// Join the remaining words into config variable's value
|
||||||
|
@ -186,7 +189,7 @@ int CmdConfig::execute (std::string& output)
|
||||||
|
|
||||||
if (name != "")
|
if (name != "")
|
||||||
{
|
{
|
||||||
bool change = false;
|
auto change = false;
|
||||||
|
|
||||||
// task config name value
|
// task config name value
|
||||||
// task config name ""
|
// task config name ""
|
||||||
|
@ -205,7 +208,7 @@ int CmdConfig::execute (std::string& output)
|
||||||
else if (rc == 1)
|
else if (rc == 1)
|
||||||
found = true;
|
found = true;
|
||||||
|
|
||||||
if (!found)
|
if (! found)
|
||||||
throw format (STRING_CMD_CONFIG_NO_ENTRY, name);
|
throw format (STRING_CMD_CONFIG_NO_ENTRY, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,7 +256,7 @@ int CmdCompletionConfig::execute (std::string& output)
|
||||||
context.config.all (configs);
|
context.config.all (configs);
|
||||||
std::sort (configs.begin (), configs.end ());
|
std::sort (configs.begin (), configs.end ());
|
||||||
|
|
||||||
for (auto& config : configs)
|
for (const auto& config : configs)
|
||||||
output += config + '\n';
|
output += config + '\n';
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -34,8 +34,8 @@ class CmdConfig : public Command
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CmdConfig ();
|
CmdConfig ();
|
||||||
static bool setConfigVariable (std::string name, std::string value, bool confirmation = false);
|
static bool setConfigVariable (const std::string&, const std::string&, bool confirmation = false);
|
||||||
static int unsetConfigVariable (std::string name, bool confirmation = false);
|
static int unsetConfigVariable (const std::string&, bool confirmation = false);
|
||||||
int execute (std::string&);
|
int execute (std::string&);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue