62 lines
2.5 KiB
C++
62 lines
2.5 KiB
C++
#include "versiond/versiond.hpp"
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
namespace fs = std::filesystem;
|
|
namespace versiond {
|
|
static std::string trim(std::string s) {
|
|
auto a = s.find_first_not_of(" \t\r\n");
|
|
if (a == std::string::npos) return "";
|
|
auto b = s.find_last_not_of(" \t\r\n");
|
|
return s.substr(a, b - a + 1);
|
|
}
|
|
static std::string unq(std::string s) {
|
|
s = trim(s);
|
|
if (s.size() >= 2 && s.front() == '"' && s.back() == '"') return s.substr(1, s.size() - 2);
|
|
return s;
|
|
}
|
|
Config default_config() { return Config{}; }
|
|
Config load_config(const std::string& home) {
|
|
Config c;
|
|
const char* e = std::getenv("VERSIOND_HOME");
|
|
c.home = home.empty() ? (e ? e : std::string("/tmp/versiond")) : home;
|
|
c.config_dir = c.home + "/config";
|
|
c.data_dir = c.home + "/data";
|
|
c.spool_dir = c.data_dir + "/spool";
|
|
c.index_file = c.data_dir + "/index.sqlite";
|
|
c.config_file = c.config_dir + "/config.toml";
|
|
fs::create_directories(c.spool_dir);
|
|
fs::create_directories(c.config_dir);
|
|
std::ifstream f(c.config_file);
|
|
if (!f) {
|
|
std::ofstream o(c.config_file);
|
|
o << "[server]\nhost = \"" << c.host << "\"\nport = " << c.port << "\n";
|
|
o << "\n[retention]\nkeep_days = " << c.keep_days << "\n";
|
|
o << "\n[remote]\nurl = \"\"\nusername = \"\"\nbase_path = \"" << c.remote_base << "\"\n";
|
|
o << "\n[limits]\nmax_file_bytes = " << c.max_file_bytes << "\n";
|
|
} else {
|
|
std::string line, section;
|
|
while (std::getline(f, line)) {
|
|
line = trim(line);
|
|
if (line.empty() || line[0] == '#') continue;
|
|
if (line.front() == '[' && line.back() == ']') {
|
|
section = line.substr(1, line.size() - 2);
|
|
continue;
|
|
}
|
|
auto eq = line.find('=');
|
|
if (eq == std::string::npos) continue;
|
|
std::string k = trim(line.substr(0, eq)), v = trim(line.substr(eq + 1));
|
|
if (section == "server" && k == "host") c.host = unq(v);
|
|
else if (section == "server" && k == "port") c.port = std::stoi(v);
|
|
else if (section == "retention" && k == "keep_days") c.keep_days = std::stoi(v);
|
|
else if (section == "remote" && k == "url") c.remote_url = unq(v);
|
|
else if (section == "remote" && k == "username") c.remote_user = unq(v);
|
|
else if (section == "remote" && k == "base_path") c.remote_base = unq(v);
|
|
else if (section == "limits" && k == "max_file_bytes") c.max_file_bytes = std::stoull(v);
|
|
else if (section == "server" && k == "verify_tls") c.verify_tls = (v == "true");
|
|
}
|
|
}
|
|
return c;
|
|
}
|
|
}
|