diff --git a/src/commands/CmdImport.cpp b/src/commands/CmdImport.cpp index d2c1f7d5..ab79db62 100644 --- a/src/commands/CmdImport.cpp +++ b/src/commands/CmdImport.cpp @@ -26,14 +26,77 @@ #include #include -#include #include #include - #include #include -std::vector import_file (const std::string &file_name); +//////////////////////////////////////////////////////////////////////////////// +std::vector parse_content (std::string content) { + std::unique_ptr json(json::parse(content)); + + if (content.empty() || (json == nullptr)) { + throw std::string("Contents invalid."); + } + + if (json->type() != json::j_array) { + throw std::string("Expected JSON array for import data."); + } + + std::vector intervals; + + for (auto item: dynamic_cast(json.get())->_data) { + Interval new_interval = IntervalFactory::fromJson(item->dump()); + intervals.push_back(new_interval); + } + + return intervals; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string read_input() { + std::string content; + std::string line; + + while (std::getline(std::cin, line)) { + content += line; + } + + return content; +} + +//////////////////////////////////////////////////////////////////////////////// +std::vector import_file(const std::string &file_name) { + Path file_path; + + if (file_name.at(0) == '/') { + file_path = file_name; + } else { + file_path = Directory::cwd() + "/" + file_name; + } + + std::string content; + + if (const bool exists = file_path.exists(); exists && File::read(file_path, content)) { + return parse_content(content); + } + + throw format("File {1} does not exist or cannot be read: ", file_name); +} + +void import_intervals(CLI &cli, Rules &rules, Database &database, Journal &journal, const bool verbose, std::vector intervals) { + journal.startTransaction (); + for (auto &interval: intervals) + { + // Add each interval to the database + if (validate(cli, rules, database, interval)) + { + database.addInterval(interval, verbose); + database.commit(); + } + } + journal.endTransaction (); +} //////////////////////////////////////////////////////////////////////////////// int CmdImport( @@ -41,80 +104,40 @@ int CmdImport( Rules &rules, Database &database, Journal &journal) { - const bool verbose = rules.getBoolean("verbose"); + const bool verbose = rules.getBoolean ("verbose"); - auto fileNames = cli.getWords(); - - for (const auto& fileName: fileNames) + if (const auto fileNames = cli.getWords (); fileNames.empty ()) { - try + const auto content = read_input (); + const auto intervals = parse_content (content); + + import_intervals (cli, rules, database, journal, verbose, intervals); + + if (verbose) { - auto intervals = import_file (fileName); - - journal.startTransaction (); - for (auto& interval: intervals) - { - // Add each interval to the database - if (validate (cli, rules, database, interval)) { - database.addInterval (interval, verbose); - database.commit (); - } - } - journal.endTransaction (); - - if (verbose) - { - std::cout << "Imported " << intervals.size () << " interval(s) from '" << fileName << "'." << std::endl; - } + std::cout << "Imported " << intervals.size () << " interval(s)." << std::endl; } - catch (const std::string &error) - { - throw format("Error importing '{1}': {2}", fileName, error); - } - } - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -std::vector import_file (const std::string& file_name) { - Path file_path; - - if (file_name.at (0) == '/') - { - file_path = file_name; } else { - file_path = Directory::cwd() + "/" + file_name; + for (const auto& fileName: fileNames) + { + try + { + auto intervals = import_file (fileName); + + import_intervals (cli, rules, database, journal, verbose, intervals); + + if (verbose) + { + std::cout << "Imported " << intervals.size () << " interval(s) from '" << fileName << "'." << std::endl; + } + } catch (const std::string& error) + { + throw format ("Error importing '{1}': {2}", fileName, error); + } + } } - - std::string content; - - if (const bool exists = file_path.exists(); exists && File::read(file_path, content)) - { - std::unique_ptr json(json::parse(content)); - - if (content.empty() || (json == nullptr)) - { - throw std::string("Contents invalid."); - } - - if (json->type() != json::j_array) - { - throw std::string("Expected JSON array for import data."); - } - - std::vector intervals; - - for (auto item: dynamic_cast(json.get())->_data) - { - Interval new_interval = IntervalFactory::fromJson(item->dump()); - intervals.push_back(new_interval); - } - - return intervals; - } - - throw format("File {1} does not exist or cannot be read: ", file_name); + return 0; }