mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
Hooks
- Supports 'debug.hooks' configuration setting.
This commit is contained in:
parent
b34fd64a9c
commit
7521dc56e8
4 changed files with 71 additions and 3 deletions
|
@ -204,6 +204,7 @@
|
||||||
- The filter form 'name:value' now maps to the partial match operator '=',
|
- The filter form 'name:value' now maps to the partial match operator '=',
|
||||||
rather than the exact match operator, '=='. This means that dates now
|
rather than the exact match operator, '=='. This means that dates now
|
||||||
match on the day by default, not the time also.
|
match on the day by default, not the time also.
|
||||||
|
- Supports 'debug.hooks' configuration setting.
|
||||||
|
|
||||||
------ current release ---------------------------
|
------ current release ---------------------------
|
||||||
|
|
||||||
|
|
1
NEWS
1
NEWS
|
@ -39,6 +39,7 @@ New configuration options in taskwarrior 2.4.0
|
||||||
- New truncated_count column style for the description field which as the
|
- New truncated_count column style for the description field which as the
|
||||||
name says is a combination of the existing truncated and count styles.
|
name says is a combination of the existing truncated and count styles.
|
||||||
- New 'hooks' setting is a master control switch for hook processing.
|
- New 'hooks' setting is a master control switch for hook processing.
|
||||||
|
- New 'debug.hooks' for debugging hook scripts.
|
||||||
|
|
||||||
Newly deprecated features in taskwarrior 2.4.0
|
Newly deprecated features in taskwarrior 2.4.0
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,7 @@ extern Context context;
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Hooks::Hooks ()
|
Hooks::Hooks ()
|
||||||
: _enabled (true)
|
: _enabled (true)
|
||||||
|
, _debug (0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +57,8 @@ Hooks::~Hooks ()
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Hooks::initialize ()
|
void Hooks::initialize ()
|
||||||
{
|
{
|
||||||
|
_debug = context.config.getInteger ("debug.hooks");
|
||||||
|
|
||||||
// Scan <rc.data.location>/hooks
|
// Scan <rc.data.location>/hooks
|
||||||
Directory d (context.config.get ("data.location"));
|
Directory d (context.config.get ("data.location"));
|
||||||
d += "hooks";
|
d += "hooks";
|
||||||
|
@ -64,7 +67,16 @@ void Hooks::initialize ()
|
||||||
{
|
{
|
||||||
_scripts = d.list ();
|
_scripts = d.list ();
|
||||||
std::sort (_scripts.begin (), _scripts.end ());
|
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");
|
_enabled = context.config.getBoolean ("hooks");
|
||||||
}
|
}
|
||||||
|
@ -103,10 +115,16 @@ void Hooks::onLaunch ()
|
||||||
std::vector <std::string>::iterator i;
|
std::vector <std::string>::iterator i;
|
||||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
||||||
{
|
{
|
||||||
|
if (_debug >= 1)
|
||||||
|
context.debug ("Hooks: Calling " + *i);
|
||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
std::vector <std::string> args;
|
std::vector <std::string> args;
|
||||||
int status = execute (*i, args, "", output);
|
int status = execute (*i, args, "", output);
|
||||||
|
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||||
|
|
||||||
std::vector <std::string> lines;
|
std::vector <std::string> lines;
|
||||||
split (lines, output, '\n');
|
split (lines, output, '\n');
|
||||||
std::vector <std::string>::iterator line;
|
std::vector <std::string>::iterator line;
|
||||||
|
@ -117,6 +135,9 @@ void Hooks::onLaunch ()
|
||||||
{
|
{
|
||||||
if (line->length () && (*line)[0] == '{')
|
if (line->length () && (*line)[0] == '{')
|
||||||
{
|
{
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug ("Hook output: " + *line);
|
||||||
|
|
||||||
// Only 'add' is possible.
|
// Only 'add' is possible.
|
||||||
Task newTask (*line);
|
Task newTask (*line);
|
||||||
context.tdb2.add (newTask);
|
context.tdb2.add (newTask);
|
||||||
|
@ -163,22 +184,37 @@ void Hooks::onExit ()
|
||||||
std::string input = "";
|
std::string input = "";
|
||||||
std::vector <Task>::const_iterator t;
|
std::vector <Task>::const_iterator t;
|
||||||
for (t = changes.begin (); t != changes.end (); ++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> matchingScripts = scripts ("on-exit");
|
||||||
std::vector <std::string>::iterator i;
|
std::vector <std::string>::iterator i;
|
||||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i)
|
||||||
{
|
{
|
||||||
|
if (_debug >= 1)
|
||||||
|
context.debug ("Hooks: Calling " + *i);
|
||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
std::vector <std::string> args;
|
std::vector <std::string> args;
|
||||||
int status = execute (*i, args, input, output);
|
int status = execute (*i, args, input, output);
|
||||||
|
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||||
|
|
||||||
std::vector <std::string> lines;
|
std::vector <std::string> lines;
|
||||||
split (lines, output, '\n');
|
split (lines, output, '\n');
|
||||||
std::vector <std::string>::iterator line;
|
std::vector <std::string>::iterator line;
|
||||||
|
|
||||||
for (line = lines.begin (); line != lines.end (); ++line)
|
for (line = lines.begin (); line != lines.end (); ++line)
|
||||||
{
|
{
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug ("Hook output: " + *line);
|
||||||
|
|
||||||
if (line->length () && (*line)[0] != '{')
|
if (line->length () && (*line)[0] != '{')
|
||||||
{
|
{
|
||||||
if (status == 0)
|
if (status == 0)
|
||||||
|
@ -217,11 +253,21 @@ void Hooks::onAdd (std::vector <Task>& changes)
|
||||||
std::vector <std::string>::iterator i;
|
std::vector <std::string>::iterator i;
|
||||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++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::string output;
|
||||||
std::vector <std::string> args;
|
std::vector <std::string> args;
|
||||||
int status = execute (*i, args, input, output);
|
int status = execute (*i, args, input, output);
|
||||||
|
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||||
|
|
||||||
std::vector <std::string> lines;
|
std::vector <std::string> lines;
|
||||||
split (lines, output, '\n');
|
split (lines, output, '\n');
|
||||||
std::vector <std::string>::iterator line;
|
std::vector <std::string>::iterator line;
|
||||||
|
@ -231,6 +277,9 @@ void Hooks::onAdd (std::vector <Task>& changes)
|
||||||
changes.clear ();
|
changes.clear ();
|
||||||
for (line = lines.begin (); line != lines.end (); ++line)
|
for (line = lines.begin (); line != lines.end (); ++line)
|
||||||
{
|
{
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug ("Hook output: " + *line);
|
||||||
|
|
||||||
if (line->length () && (*line)[0] == '{')
|
if (line->length () && (*line)[0] == '{')
|
||||||
changes.push_back (Task (*line));
|
changes.push_back (Task (*line));
|
||||||
else
|
else
|
||||||
|
@ -276,8 +325,18 @@ void Hooks::onModify (const Task& before, std::vector <Task>& changes)
|
||||||
std::vector <std::string>::iterator i;
|
std::vector <std::string>::iterator i;
|
||||||
for (i = matchingScripts.begin (); i != matchingScripts.end (); ++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 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"
|
+ "\n"
|
||||||
+ afterJSON
|
+ afterJSON
|
||||||
+ "\n";
|
+ "\n";
|
||||||
|
@ -285,6 +344,9 @@ void Hooks::onModify (const Task& before, std::vector <Task>& changes)
|
||||||
std::vector <std::string> args;
|
std::vector <std::string> args;
|
||||||
int status = execute (*i, args, input, output);
|
int status = execute (*i, args, input, output);
|
||||||
|
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug (format ("Hooks: Completed with status {1}", status));
|
||||||
|
|
||||||
std::vector <std::string> lines;
|
std::vector <std::string> lines;
|
||||||
split (lines, output, '\n');
|
split (lines, output, '\n');
|
||||||
std::vector <std::string>::iterator line;
|
std::vector <std::string>::iterator line;
|
||||||
|
@ -294,6 +356,9 @@ void Hooks::onModify (const Task& before, std::vector <Task>& changes)
|
||||||
changes.clear ();
|
changes.clear ();
|
||||||
for (line = lines.begin (); line != lines.end (); ++line)
|
for (line = lines.begin (); line != lines.end (); ++line)
|
||||||
{
|
{
|
||||||
|
if (_debug >= 2)
|
||||||
|
context.debug ("Hook output: " + *line);
|
||||||
|
|
||||||
if (line->length () && (*line)[0] == '{')
|
if (line->length () && (*line)[0] == '{')
|
||||||
changes.push_back (Task (*line));
|
changes.push_back (Task (*line));
|
||||||
else
|
else
|
||||||
|
|
|
@ -54,6 +54,7 @@ private:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _enabled;
|
bool _enabled;
|
||||||
|
int _debug;
|
||||||
std::vector <std::string> _scripts;
|
std::vector <std::string> _scripts;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue