Files
dropshell/source/src/config.cpp
Your Name 27a2d25fb2
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 1m33s
Fixed.
2025-05-25 19:44:10 +12:00

209 lines
5.4 KiB
C++

#include "utils/directories.hpp"
#include <iostream>
#include <fstream>
#include "config.hpp"
#include "utils/utils.hpp"
#include <filesystem>
#include "utils/execute.hpp"
#include "output.hpp"
namespace dropshell {
config & gConfig() {
static config *globalConfig = new config();
return *globalConfig;
}
config::config() : mIsConfigSet(false) {
}
config::~config() {
}
bool config::load_config() { // load json config file.
std::string config_path = localfile::dropshell_json();
if (config_path.empty() || !std::filesystem::exists(config_path))
return false;
std::ifstream config_file(config_path);
if (!config_file.is_open())
return false;
try {
mConfig = nlohmann::json::parse(config_file);
}
catch (nlohmann::json::parse_error& ex)
{
error << "Failed to parse config file: " << ex.what() << std::endl;
return false;
}
mIsConfigSet = true;
return true;
}
void _append(std::vector<std::string> & a, const std::vector<std::string> & b) {
if (b.empty())
return;
if (a.empty())
a = b;
else
a.insert(std::end(a), std::begin(b), std::end(b));
}
bool config::save_config(bool create_aux_directories)
{
std::string config_path = localfile::dropshell_json();
if (config_path.empty())
return false;
std::filesystem::create_directories(get_parent(config_path));
std::ofstream config_file(config_path);
if (!config_file.is_open())
return false;
if (!mIsConfigSet)
{
std::string homedir = localpath::current_user_home();
std::string dropshell_base = homedir + "/.dropshell";
mConfig["server_definition_paths"] = {
dropshell_base + "/servers"
};
mConfig["template_local_paths"] = {
dropshell_base + "/local_templates"
};
mConfig["template_registry_URLs"] = {
"https://templates.dropshell.app"
};
mConfig["template_upload_token"] = "SECRETTOKEN";
mConfig["backups_path"] = {
dropshell_base + "/backups"
};
}
config_file << mConfig.dump(4);
config_file.close();
if (create_aux_directories) {
std::vector<std::string> paths;
_append(paths, get_local_template_paths());
_append(paths, get_local_server_definition_paths());
for (auto & p : paths)
if (!std::filesystem::exists(p))
{
std::cout << "Creating directory: " << p << std::endl;
std::filesystem::create_directories(p);
}
}
debug << "Config paths: " << std::endl;
for (auto [key,value] : mConfig.items()) {
debug << " " << key << ": " << value << std::endl;
}
return true;
}
bool config::is_config_set() const
{
return mIsConfigSet;
}
bool config::is_agent_installed()
{
return std::filesystem::exists(localfile::bb64());
}
std::vector<tRegistryEntry> config::get_template_registry_urls() {
nlohmann::json template_registries = mConfig["template_registries"];
std::vector<tRegistryEntry> registries;
for (auto &registry : template_registries) {
if (registry.is_object() && !registry.empty())
registries.push_back(tRegistryEntry(registry));
}
return registries;
}
std::vector<std::string> config::get_local_template_paths()
{
nlohmann::json template_local_paths = mConfig["template_local_paths"];
std::vector<std::string> paths;
for (auto &path : template_local_paths) {
if (path.is_string() && !path.empty())
paths.push_back(path);
}
return paths;
}
std::vector<std::string> config::get_local_server_definition_paths() {
nlohmann::json server_definition_paths = mConfig["server_definition_paths"];
std::vector<std::string> paths;
for (auto &path : server_definition_paths) {
if (path.is_string() && !path.empty())
paths.push_back(path);
}
return paths;
}
std::string config::get_server_create_path()
{
std::vector<std::string> paths = get_local_server_definition_paths();
if (paths.empty())
return "";
return paths[0];
}
std::string config::get_template_create_path()
{
std::vector<std::string> paths = get_local_template_paths();
if (paths.empty())
return "";
return paths[0];
}
std::string config::get_backups_path()
{
nlohmann::json backups_path = mConfig["backups_path"];
if (backups_path.empty())
return "";
if (backups_path.is_string())
return backups_path;
warning << "backups_path is not a string: " << backups_path << std::endl;
return "";
}
dropshell::tRegistryEntry::tRegistryEntry(nlohmann::json json)
{
valid = false;
if (json.is_object() && !json.empty()) {
for (auto &[key, value] : json.items()) {
if (value.is_string() && !value.empty())
switch (switchhash(key.c_str())) {
case switchhash("name"):
name = value;
break;
case switchhash("url"):
url = value;
break;
case switchhash("token"):
token = value;
break;
default:
break;
}
}
valid = (!url.empty()&&!name.empty()); // token can be empty.
}
}
tRegistryEntry::~tRegistryEntry()
{
}
} // namespace dropshell