mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-27 19:17:19 +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 '=',
|
||||
rather than the exact match operator, '=='. This means that dates now
|
||||
match on the day by default, not the time also.
|
||||
- Supports 'debug.hooks' configuration setting.
|
||||
|
||||
------ 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
|
||||
name says is a combination of the existing truncated and count styles.
|
||||
- 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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -54,6 +54,7 @@ private:
|
|||
|
||||
private:
|
||||
bool _enabled;
|
||||
int _debug;
|
||||
std::vector <std::string> _scripts;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue