Hooks
- Supports 'debug.hooks' configuration setting.
This commit is contained in:
@@ -45,6 +45,7 @@ extern Context context;
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Hooks::Hooks ()
|
||||
: _enabled (true)
|
||||
, _debug (0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -56,6 +57,8 @@ Hooks::~Hooks ()
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Hooks::initialize ()
|
||||
{
|
||||
_debug = context.config.getInteger ("debug.hooks");
|
||||
|
||||
// Scan <rc.data.location>/hooks
|
||||
Directory d (context.config.get ("data.location"));
|
||||
d += "hooks";
|
||||
@@ -64,7 +67,16 @@ void Hooks::initialize ()
|
||||
{
|
||||
_scripts = d.list ();
|
||||
std::sort (_scripts.begin (), _scripts.end ());
|
||||
|
||||
if (_debug >= 1)
|
||||
{
|
||||
std::vector <std::string>::iterator i;
|
||||
for (i = _scripts.begin (); i != _scripts.end (); ++i)
|
||||
context.debug ("Found hook script " + *i);
|
||||
}
|
||||
}
|
||||
else if (_debug >= 1)
|
||||
context.debug ("Hook directory not readable: " + d._data);
|
||||
|
||||
_enabled = context.config.getBoolean ("hooks");
|
||||
}
|
||||
@@ -103,10 +115,16 @@ void Hooks::onLaunch ()
|
||||
std::vector <std::string>::iterator i;
|
||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
||||
{
|
||||
if (_debug >= 1)
|
||||
context.debug ("Hooks: Calling " + *i);
|
||||
|
||||
std::string output;
|
||||
std::vector <std::string> args;
|
||||
int status = execute (*i, args, "", output);
|
||||
|
||||
if (_debug >= 2)
|
||||
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||
|
||||
std::vector <std::string> lines;
|
||||
split (lines, output, '\n');
|
||||
std::vector <std::string>::iterator line;
|
||||
@@ -117,6 +135,9 @@ void Hooks::onLaunch ()
|
||||
{
|
||||
if (line->length () && (*line)[0] == '{')
|
||||
{
|
||||
if (_debug >= 2)
|
||||
context.debug ("Hook output: " + *line);
|
||||
|
||||
// Only 'add' is possible.
|
||||
Task newTask (*line);
|
||||
context.tdb2.add (newTask);
|
||||
@@ -163,22 +184,37 @@ void Hooks::onExit ()
|
||||
std::string input = "";
|
||||
std::vector <Task>::const_iterator t;
|
||||
for (t = changes.begin (); t != changes.end (); ++t)
|
||||
input += t->composeJSON () + "\n";
|
||||
{
|
||||
std::string json = t->composeJSON ();
|
||||
if (_debug >= 2)
|
||||
context.debug ("Hook input: " + json);
|
||||
|
||||
input += json + "\n";
|
||||
}
|
||||
|
||||
std::vector <std::string> matchingScripts = scripts ("on-exit");
|
||||
std::vector <std::string>::iterator i;
|
||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
||||
{
|
||||
if (_debug >= 1)
|
||||
context.debug ("Hooks: Calling " + *i);
|
||||
|
||||
std::string output;
|
||||
std::vector <std::string> args;
|
||||
int status = execute (*i, args, input, output);
|
||||
|
||||
if (_debug >= 2)
|
||||
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||
|
||||
std::vector <std::string> lines;
|
||||
split (lines, output, '\n');
|
||||
std::vector <std::string>::iterator line;
|
||||
|
||||
for (line = lines.begin (); line != lines.end (); ++line)
|
||||
{
|
||||
if (_debug >= 2)
|
||||
context.debug ("Hook output: " + *line);
|
||||
|
||||
if (line->length () && (*line)[0] != '{')
|
||||
{
|
||||
if (status == 0)
|
||||
@@ -217,11 +253,21 @@ void Hooks::onAdd (std::vector <Task>& changes)
|
||||
std::vector <std::string>::iterator i;
|
||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
||||
{
|
||||
std::string input = changes[0].composeJSON () + "\n";
|
||||
if (_debug >= 1)
|
||||
context.debug ("Hooks: Calling " + *i);
|
||||
|
||||
std::string input = changes[0].composeJSON ();
|
||||
if (_debug >= 2)
|
||||
context.debug ("Hook input: " + input);
|
||||
|
||||
input += "\n";
|
||||
std::string output;
|
||||
std::vector <std::string> args;
|
||||
int status = execute (*i, args, input, output);
|
||||
|
||||
if (_debug >= 2)
|
||||
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||
|
||||
std::vector <std::string> lines;
|
||||
split (lines, output, '\n');
|
||||
std::vector <std::string>::iterator line;
|
||||
@@ -231,6 +277,9 @@ void Hooks::onAdd (std::vector <Task>& changes)
|
||||
changes.clear ();
|
||||
for (line = lines.begin (); line != lines.end (); ++line)
|
||||
{
|
||||
if (_debug >= 2)
|
||||
context.debug ("Hook output: " + *line);
|
||||
|
||||
if (line->length () && (*line)[0] == '{')
|
||||
changes.push_back (Task (*line));
|
||||
else
|
||||
@@ -276,8 +325,18 @@ void Hooks::onModify (const Task& before, std::vector <Task>& changes)
|
||||
std::vector <std::string>::iterator i;
|
||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
||||
{
|
||||
if (_debug >= 1)
|
||||
context.debug ("Hooks: Calling " + *i);
|
||||
|
||||
std::string beforeJSON = before.composeJSON ();
|
||||
std::string afterJSON = changes[0].composeJSON ();
|
||||
std::string input = before.composeJSON ()
|
||||
if (_debug >= 2)
|
||||
{
|
||||
context.debug ("Hook input: " + beforeJSON);
|
||||
context.debug ("Hook input: " + afterJSON);
|
||||
}
|
||||
|
||||
std::string input = beforeJSON
|
||||
+ "\n"
|
||||
+ afterJSON
|
||||
+ "\n";
|
||||
@@ -285,6 +344,9 @@ void Hooks::onModify (const Task& before, std::vector <Task>& changes)
|
||||
std::vector <std::string> args;
|
||||
int status = execute (*i, args, input, output);
|
||||
|
||||
if (_debug >= 2)
|
||||
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||
|
||||
std::vector <std::string> lines;
|
||||
split (lines, output, '\n');
|
||||
std::vector <std::string>::iterator line;
|
||||
@@ -294,6 +356,9 @@ void Hooks::onModify (const Task& before, std::vector <Task>& changes)
|
||||
changes.clear ();
|
||||
for (line = lines.begin (); line != lines.end (); ++line)
|
||||
{
|
||||
if (_debug >= 2)
|
||||
context.debug ("Hook output: " + *line);
|
||||
|
||||
if (line->length () && (*line)[0] == '{')
|
||||
changes.push_back (Task (*line));
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user