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);
};
/*

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importiert eine JSON-Datei"
#define STRING_CMD_IMPORT_SUMMARY "{1} Aufgabe importiert."
#define STRING_CMD_IMPORT_NOFILE "Sie müssen eine zu importierende Datei angeben."
#define STRING_CMD_IMPORT_FILE "Importiere '{1}'"
#define STRING_CMD_IMPORT_MISSING "Datei '{1}' nicht gefunden."
#define STRING_TASK_NO_DESC "Kommentar fehlt Beschreibung: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Imports JSON files"
#define STRING_CMD_IMPORT_SUMMARY "Imported {1} tasks."
#define STRING_CMD_IMPORT_NOFILE "You must specify a file to import."
#define STRING_CMD_IMPORT_FILE "Importing '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Annotation is missing a description: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importas JSON-dosierojn"
#define STRING_CMD_IMPORT_SUMMARY "Importis {1} taskojn."
#define STRING_CMD_IMPORT_NOFILE "Vi devas specifi kiu dosieron import."
#define STRING_CMD_IMPORT_FILE "Importanta '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Komento havas mankon de priskribo: {1}"

View file

@ -457,7 +457,6 @@
#define STRING_CMD_IMPORT_USAGE "Importa archivos JSON"
#define STRING_CMD_IMPORT_SUMMARY "Importadas {1} tareas."
#define STRING_CMD_IMPORT_NOFILE "Debe especificar un archivo a importar."
#define STRING_CMD_IMPORT_FILE "Importando '{1}'"
#define STRING_CMD_IMPORT_MISSING "Archivo '{1}' no encontrado."
#define STRING_TASK_NO_DESC "La anotación carece de descripción: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Imports JSON files"
#define STRING_CMD_IMPORT_SUMMARY "Imported {1} tasks."
#define STRING_CMD_IMPORT_NOFILE "You must specify a file to import."
#define STRING_CMD_IMPORT_FILE "Importing '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Annotation is missing a description: {1}"

View file

@ -448,7 +448,6 @@
#define STRING_CMD_IMPORT_USAGE "Importa file JSON"
#define STRING_CMD_IMPORT_SUMMARY "Importati {1} task."
#define STRING_CMD_IMPORT_NOFILE "Specificare il file da importare."
#define STRING_CMD_IMPORT_FILE "Importazione di '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Annotazione senza descrizione: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "JSON ファイルをインポート"
#define STRING_CMD_IMPORT_SUMMARY "{1} task をインポートしました。"
#define STRING_CMD_IMPORT_NOFILE "インポートするファイルを指定してください。"
#define STRING_CMD_IMPORT_FILE "'{1}' をインポート中"
#define STRING_CMD_IMPORT_MISSING "ファイル '{1}' が見つかりません。"
#define STRING_TASK_NO_DESC "Annotation is missing a description: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importuje pliki JSON"
#define STRING_CMD_IMPORT_SUMMARY "Zaimportowano {1} zadań."
#define STRING_CMD_IMPORT_NOFILE "Musisz podać plik do zaimportowania."
#define STRING_CMD_IMPORT_FILE "Importowanie '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Komentarz nie posiada treści: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importa ficheiros JSON"
#define STRING_CMD_IMPORT_SUMMARY "Importadas {1} tarefas."
#define STRING_CMD_IMPORT_NOFILE "Necessita especificar o ficheiro a importar."
#define STRING_CMD_IMPORT_FILE "A importar '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Descrição da anotação em falta: {1}"