initial commit
This commit is contained in:
commit
be13205ffd
19 changed files with 33596 additions and 0 deletions
125
implementations/Nostro-Connector.cpp
Normal file
125
implementations/Nostro-Connector.cpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
#include "../lib/json.hpp"
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
|
||||
module Nostro:Connector_impl;
|
||||
import std;
|
||||
import :Connector;
|
||||
import :Banks;
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void Connector::LoadConnectors() {
|
||||
const fs::path path = GetConnectorPath();
|
||||
std::ifstream f(path);
|
||||
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>>());
|
||||
for(Bank& bank : Bank::connectors) {
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in a new issue