2022-08-04 18:22:48 -05:00
|
|
|
//
|
|
|
|
// Created by quentin on 8/3/22.
|
|
|
|
//
|
|
|
|
|
2022-08-13 18:42:16 -05:00
|
|
|
#include "optHandlers/mainOptHandler.h"
|
2023-01-21 17:50:07 -06:00
|
|
|
#include "exceptions/helpRequested.h"
|
|
|
|
#include "exceptions/badValue.h"
|
2023-01-22 18:09:30 -06:00
|
|
|
#include "sqliteDb.h"
|
2022-08-04 18:22:48 -05:00
|
|
|
|
2022-08-12 15:26:25 -05:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string>
|
|
|
|
#include <filesystem>
|
2023-01-21 17:50:07 -06:00
|
|
|
#include <sqlite3.h>
|
|
|
|
#include <iostream>
|
2023-01-15 17:50:01 -06:00
|
|
|
|
2022-08-13 16:35:01 -05:00
|
|
|
|
|
|
|
using namespace Budget;
|
2022-08-12 15:26:25 -05:00
|
|
|
|
|
|
|
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
2022-09-05 20:38:07 -05:00
|
|
|
const std::string configD = homeDirectory + "/.config/budget/";
|
|
|
|
const std::string storageD = homeDirectory + "/.local/share/budget/";
|
2023-01-21 17:50:07 -06:00
|
|
|
const std::string databaseFile = homeDirectory + "/.local/share/budget/budget.sqlite";
|
|
|
|
|
2022-08-12 15:26:25 -05:00
|
|
|
|
|
|
|
void createRequiredFolders() {
|
2022-09-05 20:38:07 -05:00
|
|
|
std::filesystem::create_directory(configD);
|
|
|
|
std::filesystem::create_directories(storageD);
|
|
|
|
std::filesystem::create_directories(storageD + "receipts");
|
2022-08-12 15:26:25 -05:00
|
|
|
}
|
|
|
|
|
2022-08-13 16:35:01 -05:00
|
|
|
int main(int argc, char *argv[]) {
|
2023-01-21 17:50:07 -06:00
|
|
|
sqlite3 *db;
|
2023-01-22 18:09:30 -06:00
|
|
|
SqliteDb dbRAII(db);
|
|
|
|
|
2023-01-21 17:50:07 -06:00
|
|
|
int rc;
|
|
|
|
rc = sqlite3_open(databaseFile.c_str(), &db);
|
|
|
|
|
2023-01-22 18:09:30 -06:00
|
|
|
if (rc != SQLITE_OK)
|
2023-01-21 17:50:07 -06:00
|
|
|
throw std::runtime_error("Error opening database connection.");
|
2023-01-22 18:09:30 -06:00
|
|
|
|
|
|
|
rc = sqlite3_exec(db, "PRAGMA foreign_keys = 1;", nullptr, nullptr, nullptr);
|
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
throw std::runtime_error("Error enabling foreign_keys. Database might be malformed.");
|
2023-01-21 17:50:07 -06:00
|
|
|
|
2023-01-28 12:28:43 -06:00
|
|
|
rc = sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
|
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
throw std::runtime_error("Couldn't begin transaction");
|
|
|
|
|
2022-08-13 16:35:01 -05:00
|
|
|
std::vector<char *> args(argv, argv + argc);
|
2023-01-21 17:50:07 -06:00
|
|
|
try {
|
|
|
|
OptHandlers::MainOptHandler moh(args, db);
|
|
|
|
std::queue<std::unique_ptr<OptHandlers::Operation>> *opts = &moh.operations;
|
|
|
|
|
|
|
|
while (!opts->empty()) {
|
|
|
|
opts->front()->commit();
|
|
|
|
opts->pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (const Budget::Exceptions::HelpRequested &e) {
|
|
|
|
return 0;
|
|
|
|
} catch (const Budget::Exceptions::BadValue &e) {
|
|
|
|
std::cout << e.what() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-28 12:28:43 -06:00
|
|
|
rc = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
|
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
throw std::runtime_error("Couldn't commit transaction");
|
|
|
|
|
2022-08-12 15:26:25 -05:00
|
|
|
return 0;
|
|
|
|
}
|