budget/src/main.cpp

101 lines
3.4 KiB
C++
Raw Normal View History

2022-08-04 18:22:48 -05:00
//
// Created by quentin on 8/3/22.
//
#include "main.h"
#include "data/accountData.h"
#include "optHandlers/accountOptHandler.h"
#include "optHandlers/mainOptHandler.h"
#include "../utilities/math.h"
2022-08-04 18:22:48 -05:00
#include <pwd.h>
#include <unistd.h>
#include <string>
#include <filesystem>
#include <unordered_map>
#include <iostream>
using namespace Budget;
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/";
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");
}
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)
OptHandlers::MainOptHandler mainOptHandler(args);
mainOptHandler.parse();
if (mainOptHandler.getSetOpts()->help)
mainOptHandler.help();
2022-09-05 20:38:07 -05:00
createRequiredFolders();
// Read all accounts saved and store them in accounts
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")) {
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.
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
if (accountOptHandler.getSetOpts()->help) {
accountOptHandler.help();
}
2022-09-05 20:38:07 -05:00
if (accountOptHandler.getSetOpts()->create) {
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;
}
}
if (accountOptHandler.getSetOpts()->value) {
auto a = accounts.find(accountOptHandler.getSetOpts()->valueAccount);
if (a != accounts.end()) {
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-05 20:38:07 -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;
}
}
}
return 0;
}