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-09-13 18:27:29 -05:00
|
|
|
#include "../utilities/math.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-08 11:00:56 -05:00
|
|
|
auto a = accounts.find(accountOptHandler.getSetOpts()->delAccount);
|
|
|
|
if (a == accounts.end()) {
|
|
|
|
accounts.emplace(std::piecewise_construct,
|
|
|
|
std::make_tuple(accountOptHandler.getSetOpts()->createAccount), std::make_tuple(
|
|
|
|
storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json",
|
|
|
|
accountOptHandler.getSetOpts()->createAccount));
|
|
|
|
} else {
|
|
|
|
std::cout << "Account " << accountOptHandler.getSetOpts()->delAccount << " already exists."
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2022-09-07 22:41:18 -05:00
|
|
|
}
|
|
|
|
if (accountOptHandler.getSetOpts()->value) {
|
|
|
|
auto a = accounts.find(accountOptHandler.getSetOpts()->valueAccount);
|
|
|
|
if (a != accounts.end()) {
|
2022-09-16 19:53:05 -05:00
|
|
|
int value = a->second.getAccount()->getValue();
|
|
|
|
std::vector<DateMoney> timeline = a->second.getAccount()->getTimeline();
|
|
|
|
printf("Account value: %n\n", &value);
|
|
|
|
printf("Last 10 payments:\n");
|
|
|
|
for (auto transaction : timeline) {
|
|
|
|
|
|
|
|
}
|
2022-09-07 22:41:18 -05:00
|
|
|
}
|
2022-09-05 20:38:07 -05:00
|
|
|
}
|
2022-09-08 11:00:56 -05:00
|
|
|
if (accountOptHandler.getSetOpts()->del) {
|
|
|
|
auto a = accounts.find(accountOptHandler.getSetOpts()->delAccount);
|
|
|
|
if (a != accounts.end()) {
|
|
|
|
accounts.erase(a);
|
|
|
|
std::cout << "Deleted account: " << accountOptHandler.getSetOpts()->delAccount << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 11:26:15 -05:00
|
|
|
if (accountOptHandler.getSetOpts()->list) {
|
|
|
|
std::cout << "Accounts: " << std::endl;
|
|
|
|
for (auto &accountPair : accounts) {
|
|
|
|
std::cout << accountPair.second.getAccount()->getName() << ": " << std::endl;
|
|
|
|
std::cout << " Value: " << accountPair.second.getAccount()->getValue() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 15:26:25 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|