mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
CmdImport: Added support for free-form JSON import
This commit is contained in:
parent
5995320164
commit
c2c26b338b
4 changed files with 108 additions and 45 deletions
13
src/Task.cpp
13
src/Task.cpp
|
@ -151,6 +151,19 @@ Task::Task (const std::string& input)
|
|||
parse (input);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Task::Task (const json::object* obj)
|
||||
{
|
||||
id = 0;
|
||||
urgency_value = 0.0;
|
||||
recalc_urgency = true;
|
||||
is_blocked = false;
|
||||
is_blocking = false;
|
||||
annotation_count = 0;
|
||||
|
||||
parseJSON (obj);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Task::~Task ()
|
||||
{
|
||||
|
|
|
@ -63,6 +63,7 @@ public:
|
|||
Task& operator= (const Task&); // Assignment operator
|
||||
bool operator== (const Task&); // Comparison operator
|
||||
Task (const std::string&); // Parse
|
||||
Task (const json::object*); // Parse
|
||||
~Task (); // Destructor
|
||||
|
||||
void parse (const std::string&);
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
#include <sstream>
|
||||
#include <Context.h>
|
||||
#include <Filter.h>
|
||||
#include <JSON.h>
|
||||
#include <text.h>
|
||||
#include <util.h>
|
||||
#include <i18n.h>
|
||||
|
@ -64,17 +63,15 @@ int CmdImport::execute (std::string& output)
|
|||
std::vector <std::string> words = context.cli2.getWords ();
|
||||
if (! words.size () || (words.size () == 1 && words[0] == "-"))
|
||||
{
|
||||
// No files or only "-" specified, import tasks from STDIN.
|
||||
std::vector <std::string> lines;
|
||||
std::string line;
|
||||
|
||||
std::cout << format (STRING_CMD_IMPORT_FILE, "STDIN") << "\n";
|
||||
|
||||
std::string json;
|
||||
std::string line;
|
||||
while (std::getline (std::cin, line))
|
||||
lines.push_back (line);
|
||||
json += line + " ";
|
||||
|
||||
if (lines.size () > 0)
|
||||
count = import (lines);
|
||||
if (nontrivial (json))
|
||||
count = import (json);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -88,10 +85,10 @@ int CmdImport::execute (std::string& output)
|
|||
std::cout << format (STRING_CMD_IMPORT_FILE, word) << "\n";
|
||||
|
||||
// Load the file.
|
||||
std::vector <std::string> lines;
|
||||
incoming.read (lines);
|
||||
|
||||
count += import (lines);
|
||||
std::string json;
|
||||
incoming.read (json);
|
||||
if (nontrivial (json))
|
||||
count += import (json);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -158,3 +155,84 @@ int CmdImport::import (std::vector <std::string>& lines)
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int CmdImport::import (const std::string& input)
|
||||
{
|
||||
int count = 0;
|
||||
json::value* root = json::parse (input);
|
||||
|
||||
// Single object parse. Input looks like:
|
||||
// { ... }
|
||||
if (root->type () == json::j_object)
|
||||
{
|
||||
// For each object element...
|
||||
json::object* root_obj = (json::object*)root;
|
||||
if (root_obj)
|
||||
{
|
||||
importSingleTask (root_obj);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiple object array. Input looks like:
|
||||
// [ { ... } , { ... } ]
|
||||
else if (root->type () == json::j_array)
|
||||
{
|
||||
json::array* root_arr = (json::array*)root;
|
||||
|
||||
// For each object element...
|
||||
for (auto& element : root_arr->_data)
|
||||
{
|
||||
// For each object element...
|
||||
json::object* root_obj = (json::object*)element;
|
||||
if (root_obj)
|
||||
{
|
||||
importSingleTask (root_obj);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete root;
|
||||
return count;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void CmdImport::importSingleTask (json::object* obj)
|
||||
{
|
||||
// Parse the whole thing.
|
||||
Task task (obj);
|
||||
|
||||
// Check whether the imported task is new or a modified existing task.
|
||||
Task before;
|
||||
if (context.tdb2.get (task.get ("uuid"), before))
|
||||
{
|
||||
// "modified:" is automatically set to the current time when a task is
|
||||
// changed. If the imported task has a modification timestamp we need
|
||||
// to ignore it in taskDiff() in order to check for meaningful
|
||||
// differences. Setting it to the previous value achieves just that.
|
||||
task.set ("modified", before.get ("modified"));
|
||||
if (taskDiff (before, task))
|
||||
{
|
||||
CmdModify modHelper;
|
||||
modHelper.checkConsistency (before, task);
|
||||
modHelper.modifyAndUpdate (before, task);
|
||||
std::cout << " mod ";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << " skip ";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
context.tdb2.add (task);
|
||||
std::cout << " add ";
|
||||
}
|
||||
|
||||
std::cout << task.get ("uuid")
|
||||
<< " "
|
||||
<< task.get ("description")
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -29,16 +29,8 @@
|
|||
|
||||
#include <string>
|
||||
#include <Command.h>
|
||||
#include <JSON.h>
|
||||
|
||||
class CmdImport : public Command
|
||||
{
|
||||
public:
|
||||
CmdImport ();
|
||||
int execute (std::string&);
|
||||
int import (std::vector <std::string>& lines);
|
||||
};
|
||||
|
||||
/*
|
||||
class CmdImport : public Command
|
||||
{
|
||||
public:
|
||||
|
@ -46,31 +38,10 @@ public:
|
|||
int execute (std::string&);
|
||||
|
||||
private:
|
||||
enum fileType
|
||||
{
|
||||
type_not_a_clue,
|
||||
type_task_1_4_3,
|
||||
type_task_1_5_0,
|
||||
type_task_1_6_0,
|
||||
type_task_cmd_line,
|
||||
type_todo_sh_2_0,
|
||||
type_csv,
|
||||
type_yaml,
|
||||
type_text
|
||||
};
|
||||
|
||||
fileType determineFileType (const std::vector <std::string>&);
|
||||
void decorateTask (Task&);
|
||||
std::string task_1_4_3 (const std::vector <std::string>&);
|
||||
std::string task_1_5_0 (const std::vector <std::string>&);
|
||||
std::string task_1_6_0 (const std::vector <std::string>&);
|
||||
std::string taskCmdLine (const std::vector <std::string>&);
|
||||
std::string todoSh_2_0 (const std::vector <std::string>&);
|
||||
std::string text (const std::vector <std::string>&);
|
||||
std::string CSV (const std::vector <std::string>&);
|
||||
std::string YAML (const std::vector <std::string>&);
|
||||
int import (std::vector <std::string>&);
|
||||
int import (const std::string&);
|
||||
void importSingleTask (json::object*);
|
||||
};
|
||||
*/
|
||||
|
||||
#endif
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue