Commands - config

- Migrated config and _config to CmdConfig.
This commit is contained in:
Paul Beckingham
2011-05-30 11:18:59 -04:00
parent 0f5a4434ff
commit 6cf7644e02
9 changed files with 247 additions and 149 deletions

View File

@@ -51,21 +51,6 @@
extern Context context;
////////////////////////////////////////////////////////////////////////////////
int handleCompletionConfig (std::string& outs)
{
std::vector <std::string> configs;
context.config.all (configs);
std::sort (configs.begin (), configs.end ());
std::stringstream out;
foreach (config, configs)
out << *config << "\n";
outs = out.str ();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int handleQuery (std::string& outs)
{
@@ -290,126 +275,6 @@ void handlePull (std::string&)
"'pull.default.uri' entry in your .taskrc file.");
}
////////////////////////////////////////////////////////////////////////////////
int handleConfig (std::string& outs)
{
int rc = 0;
std::stringstream out;
// Obtain the arguments from the description. That way, things like '--'
// have already been handled.
std::vector <std::string> args;
split (args, context.task.get ("description"), ' ');
// Support:
// task config name value # set name to value
// task config name "" # set name to blank
// task config name # remove name
if (args.size () > 0)
{
std::string name = args[0];
std::string value = "";
if (args.size () > 1)
{
for (unsigned int i = 1; i < args.size (); ++i)
{
if (i > 1)
value += " ";
value += args[i];
}
}
if (name != "")
{
bool change = false;
// Read .taskrc (or equivalent)
std::string contents;
File::read (context.config.original_file, contents);
// task config name value
// task config name ""
if (args.size () > 1 ||
context.args[context.args.size () - 1] == "")
{
// Find existing entry & overwrite
std::string::size_type pos = contents.find (name + "=");
if (pos != std::string::npos)
{
std::string::size_type eol = contents.find_first_of ("\r\f\n", pos);
if (eol == std::string::npos)
throw std::string ("Cannot find EOL after entry '") + name + "'.";
if (confirm (std::string ("Are you sure you want to change the value of '")
+ name
+ "' from '"
+ context.config.get(name)
+ "' to '"
+ value + "'?"))
{
contents = contents.substr (0, pos)
+ name + "=" + value
+ contents.substr (eol);
change = true;
}
}
// Not found, so append instead.
else
{
if (confirm (std::string ("Are you sure you want to add '") + name + "' with a value of '" + value + "'?"))
{
contents = contents
+ "\n"
+ name + "=" + value
+ "\n";
change = true;
}
}
}
// task config name
else
{
// Remove name
std::string::size_type pos = contents.find (name + "=");
if (pos == std::string::npos)
throw std::string ("No entry named '") + name + "' found.";
std::string::size_type eol = contents.find_first_of ("\r\f\n", pos);
if (eol == std::string::npos)
throw std::string ("Cannot find EOL after entry '") + name + "'.";
if (confirm (std::string ("Are you sure you want to remove '") + name + "'?"))
{
contents = contents.substr (0, pos) + contents.substr (eol + 1);
change = true;
}
}
// Write .taskrc (or equivalent)
if (change)
{
File::write (context.config.original_file, contents);
out << "Config file "
<< context.config.original_file.data
<< " modified.\n";
}
else
out << "No changes made.\n";
}
else
throw std::string ("Specify the name of a config variable to modify.");
outs = out.str ();
}
else
throw std::string ("Specify the name of a config variable to modify.");
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int handleDelete (std::string& outs)
{