Import intervals from stdin
Some checks failed
tests / tests (alpine-edge, Alpine Edge, ubuntu-latest) (push) Has been cancelled
tests / tests (alpine-latest, Alpine Latest, ubuntu-latest) (push) Has been cancelled
tests / tests (archlinux, Archlinux Base, ubuntu-latest) (push) Has been cancelled
tests / tests (centos-stream9, Centos Stream9, ubuntu-latest) (push) Has been cancelled
tests / tests (debianstable, Debian Stable, ubuntu-latest) (push) Has been cancelled
tests / tests (debiantesting, Debian Testing, ubuntu-latest) (push) Has been cancelled
tests / tests (fedora41, Fedora 41, ubuntu-latest) (push) Has been cancelled
tests / tests (fedora42, Fedora 42, ubuntu-latest) (push) Has been cancelled
tests / tests (opensuseleap, OpenSUSE Leap, ubuntu-latest) (push) Has been cancelled
tests / tests (opensusetumbleweed, OpenSUSE Tumbleweed, ubuntu-latest) (push) Has been cancelled
tests / tests (osx-13, macOS 13, macos-13) (push) Has been cancelled
tests / tests (osx-14, macOS 14, macos-14) (push) Has been cancelled
tests / tests (osx-15, macOS 15, macos-15) (push) Has been cancelled
tests / tests (ubuntu2204, Ubuntu 22.04, ubuntu-latest) (push) Has been cancelled
tests / tests (ubuntu2204, Ubuntu 24.04, ubuntu-latest) (push) Has been cancelled

Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
This commit is contained in:
Thomas Lauf 2025-07-06 23:28:00 +02:00
parent b3d9562c54
commit 3902bd885b

View file

@ -26,14 +26,77 @@
#include <commands.h>
#include <format.h>
#include <fstream>
#include <iostream>
#include <timew.h>
#include <IntervalFactory.h>
#include <JSON.h>
std::vector <Interval> import_file (const std::string &file_name);
////////////////////////////////////////////////////////////////////////////////
std::vector<Interval> parse_content (std::string content) {
std::unique_ptr<json::value> 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<Interval> intervals;
for (auto item: dynamic_cast<json::array *>(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<Interval> 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<Interval> 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<Interval> 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::value> 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<Interval> intervals;
for (auto item: dynamic_cast<json::array *>(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;
}