2022-08-04 18:22:48 -05:00
|
|
|
//
|
|
|
|
// Created by quentin on 8/3/22.
|
|
|
|
//
|
|
|
|
|
2022-08-12 15:26:25 -05:00
|
|
|
#include "main.h"
|
|
|
|
#include "data/accountData.h"
|
2022-08-13 16:35:01 -05:00
|
|
|
#include "optHandlers/accountOptHandler.h"
|
2022-08-13 18:42:16 -05:00
|
|
|
#include "optHandlers/mainOptHandler.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>
|
2022-09-07 22:41:18 -05:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <iostream>
|
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/";
|
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 + "accounts");
|
|
|
|
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[]) {
|
|
|
|
std::vector<char *> args(argv, argv + argc);
|
|
|
|
|
2022-09-05 20:38:07 -05:00
|
|
|
// Parse main options (-ah)
|
2022-08-13 18:42:16 -05:00
|
|
|
OptHandlers::MainOptHandler mainOptHandler(args);
|
|
|
|
mainOptHandler.parse();
|
2022-08-14 12:48:02 -05:00
|
|
|
if (mainOptHandler.getSetOpts()->help)
|
|
|
|
mainOptHandler.help();
|
|
|
|
|
2022-09-05 20:38:07 -05:00
|
|
|
createRequiredFolders();
|
|
|
|
// Read all accounts saved and store them in accounts
|
2022-09-07 22:41:18 -05:00
|
|
|
std::unordered_map<std::string, AccountData> accounts;
|
2022-09-05 20:38:07 -05:00
|
|
|
for (const auto &file : std::filesystem::directory_iterator(
|
|
|
|
homeDirectory + "/.local/share/budget/accounts")) {
|
2022-09-07 22:41:18 -05:00
|
|
|
AccountData account(file.path());
|
|
|
|
accounts.insert(std::pair(account.getAccount()->getName(), account));
|
2022-09-05 20:38:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse account options if main options tells us to.
|
2022-08-14 12:48:02 -05:00
|
|
|
if (mainOptHandler.getSetOpts()->account) {
|
|
|
|
std::vector<char *> vec{argv[0]};
|
|
|
|
vec.insert(vec.end(), args.begin() + mainOptHandler.getSetOpts()->accountArgStart, args.end());
|
|
|
|
OptHandlers::AccountOptHandler accountOptHandler(vec);
|
|
|
|
accountOptHandler.parse();
|
2022-09-05 20:38:07 -05:00
|
|
|
|
|
|
|
// Do what we need to do for parsed options
|
2022-08-14 12:48:02 -05:00
|
|
|
if (accountOptHandler.getSetOpts()->help) {
|
|
|
|
accountOptHandler.help();
|
|
|
|
}
|
2022-09-05 20:38:07 -05:00
|
|
|
if (accountOptHandler.getSetOpts()->create) {
|
2022-09-07 22:41:18 -05:00
|
|
|
accounts.insert(std::pair(
|
|
|
|
accountOptHandler.getSetOpts()->createAccount,
|
|
|
|
(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount +
|
|
|
|
".json", accountOptHandler.getSetOpts()->createAccount)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if (accountOptHandler.getSetOpts()->value) {
|
|
|
|
auto a = accounts.find(accountOptHandler.getSetOpts()->valueAccount);
|
|
|
|
if (a != accounts.end()) {
|
|
|
|
std::cout << "Account value: " << a->second.getAccount()->getValue() << std::endl;
|
|
|
|
}
|
2022-09-05 20:38:07 -05:00
|
|
|
}
|
2022-08-12 15:26:25 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|