This commit is contained in:
Jorijn van der Graaf 2026-10-31 20:50:57 +01:00
commit 367c19386c
17 changed files with 561 additions and 166 deletions

View file

@ -14,75 +14,27 @@ module Nostro:Connector_impl;
import std;
import :Connector;
import :Banks;
import :File;
using namespace Nostro;
using namespace nlohmann;
namespace fs = std::filesystem;
fs::path Connector::GetConnectorPath() {
const char* const xdg = std::getenv("XDG_CONFIG_HOME");
if(xdg) {
return fs::path(fs::path(std::move(xdg)) / "nostro/connectors.json");
}
const char* const home = std::getenv("HOME");
if(home) {
return fs::path(fs::path(std::move(home)) / ".config/nostro/connectors.json");
}
throw std::runtime_error("XDG_CONFIG_HOME and HOME not set");
}
void Connector::SaveConnectors() {
const fs::path path = GetConnectorPath();
const fs::path dir(path.parent_path());
fs::create_directories(dir.parent_path());
if(mkdir(dir.c_str(), S_IRWXU) == -1) {
if(errno == EEXIST) {
fs::permissions(dir, fs::perms::owner_all);
} else {
throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", dir.string()));
}
}
const std::int32_t fd = ::open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
if(fd == -1) {
throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", path.string()));
}
const fs::path path = configPath/"connectors.json";
CreatePrivateFile(path);
json jsonConnectors = json::object();
tuple_for<EnabledBanks>([&jsonConnectors]<typename Bank>() {
jsonConnectors[Bank::connectorId] = Bank::connectors;
});
const std::string text = jsonConnectors.dump(4);
const char* const textPtr = text.c_str();
for(ssize_t i = 0; i < text.size();) {
const ssize_t written = write(fd, textPtr + i, text.size() - i);
if(written == -1) {
if(errno == EINTR) {
continue;
}
close(fd);
throw std::system_error(errno, std::generic_category(), std::format("cannot write {}", path.string()));
}
i += written;
}
close(fd);
std::ofstream out(path, std::ios::binary);
out << jsonConnectors.dump(4);
}
void Connector::LoadConnectors() {
const fs::path path = GetConnectorPath();
std::ifstream f(path);
std::ifstream f(configPath/"connectors.json");
const json jsonConnectors = json::parse(f);
tuple_for<EnabledBanks>([&jsonConnectors]<typename Bank>() {
Bank::connectors = std::move(jsonConnectors.at(Bank::connectorId).template get<std::vector<Bank>>());
@ -90,36 +42,4 @@ void Connector::LoadConnectors() {
bank.RefreshAccounts();
}
});
}
std::string Connector::ListConnectors() {
std::string out;
tuple_for<EnabledBanks>([&out]<typename Bank>() {
for(Bank& bank : Bank::connectors) {
out += std::format("{}\t{}\t{}\n", bank, Bank::connectorId, bank.TestConnector());
}
});
out.pop_back();
return out;
}
std::string Connector::ListBalance() {
std::string out;
tuple_for<EnabledBanks>([&out]<typename Bank>() {
for(Bank& bank : Bank::connectors) {
out += std::format("{}\t{}\n", bank, Bank::connectorId);
for(typename Bank::Account& account : bank.accounts) {
out += std::format("\t{}\n", account);
}
}
});
out.pop_back();
return out;
}