TW-1440: Implement "task import" from STDIN

- Read tasks from STDIN when no files are specified, or only "-"
  is specified.
This commit is contained in:
Wilhelm Schuermann 2015-05-25 14:25:43 +02:00
parent 3e043291f0
commit a78c9a6eb8
14 changed files with 124 additions and 42 deletions

View file

@ -41,7 +41,7 @@ extern Context context;
CmdImport::CmdImport ()
{
_keyword = "import";
_usage = "task import <file> [<file> ...]";
_usage = "task import [<file> ...]";
_description = STRING_CMD_IMPORT_USAGE;
_read_only = false;
_displays_id = false;
@ -53,45 +53,38 @@ int CmdImport::execute (std::string& output)
int rc = 0;
int count = 0;
// Use the description as a file name.
// Get filenames from command line arguments.
std::vector <std::string> words = context.cli.getWords ();
if (! words.size ())
throw std::string (STRING_CMD_IMPORT_NOFILE);
for (auto& word : words)
if (! words.size () || (words.size () == 1 && words[0] == "-"))
{
File incoming (word);
if (! incoming.exists ())
throw format (STRING_CMD_IMPORT_MISSING, word);
std::cout << format (STRING_CMD_IMPORT_FILE, word) << "\n";
// Load the file.
// No files or only "-" specified, import tasks from STDIN.
std::vector <std::string> lines;
incoming.read (lines);
std::string line;
for (auto& line : lines)
std::cout << format (STRING_CMD_IMPORT_FILE, "STDIN") << "\n";
while (std::getline (std::cin, line))
lines.push_back (line);
if (lines.size () > 0)
count = import (lines);
}
else
{
// Import tasks from all specified files.
for (auto& word : words)
{
std::string object = trimLeft (
trimRight (
trim (line),
"]"),
"[");
File incoming (word);
if (! incoming.exists ())
throw format (STRING_CMD_IMPORT_MISSING, word);
// Skip blanks. May be caused by the trim calls above.
if (! object.length ())
continue;
std::cout << format (STRING_CMD_IMPORT_FILE, word) << "\n";
// Parse the whole thing.
Task task (object);
context.tdb2.add (task);
// Load the file.
std::vector <std::string> lines;
incoming.read (lines);
++count;
std::cout << " "
<< task.get ("uuid")
<< " "
<< task.get ("description")
<< "\n";
count += import (lines);
}
}
@ -100,3 +93,34 @@ int CmdImport::execute (std::string& output)
}
////////////////////////////////////////////////////////////////////////////////
int CmdImport::import (std::vector <std::string>& lines)
{
int count = 0;
for (auto& line : lines)
{
std::string object = trimLeft (
trimRight (
trim (line),
"]"),
"[");
// Skip blanks. May be caused by the trim calls above.
if (! object.length ())
continue;
// Parse the whole thing.
Task task (object);
context.tdb2.add (task);
++count;
std::cout << " "
<< task.get ("uuid")
<< " "
<< task.get ("description")
<< "\n";
}
return count;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -35,6 +35,7 @@ class CmdImport : public Command
public:
CmdImport ();
int execute (std::string&);
int import (std::vector <std::string>& lines);
};
/*