Major refactor

This commit is contained in:
Your Name
2025-04-28 20:39:49 +12:00
parent bd043eaed8
commit 1d6986cda0
8 changed files with 177 additions and 233 deletions

View File

@ -1,6 +1,6 @@
#include "directories.hpp"
#include "config.hpp"
#include "server_env.hpp"
#include "server_env_manager.hpp"
#include <iostream>
#include <string>
#include <filesystem>
@ -10,135 +10,88 @@ namespace fs = std::filesystem;
namespace dropshell {
std::string get_local_dropshell_config_parent_path()
{
namespace localfile {
std::string dropshell_conf() {
// Try ~/.config/dropshell/dropshell.conf
const char* home = std::getenv("HOME");
if (home) {
fs::path user_path = fs::path(home) / ".config" / "dropshell";
fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.conf";
return user_path.string();
}
std::cerr << "Warning: Couldn't determine user directory" << std::endl;
return std::string();
}
std::string get_local_dropshell_config_file()
{
std::string parent_path = get_local_dropshell_config_parent_path();
if (parent_path.empty())
return std::string();
return parent_path + "/dropshell.conf";
}
std::string get_local_system_templates_path()
{
return "/opt/dropshell/templates";
}
std::string get_local_backup_path()
{
config *cfg = get_global_config();
if (!cfg)
return std::string();
return cfg->get_local_backup_path();
}
int getNumConfigDirectories()
{
config *cfg = get_global_config();
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
return local_config_directories.size();
}
std::string get_primary_local_config_path()
{
return get_local_config_path(0);
}
std::string get_local_config_path(int index)
{
config *cfg = get_global_config();
if (!cfg)
return std::string();
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
if (index < 0 || index >= local_config_directories.size())
return std::string();
return local_config_directories[index];
}
std::string get_local_config_templates_path(int index)
{
std::string config_path = get_local_config_path(index);
if (config_path.empty())
return std::string();
return config_path + "/templates";
}
std::string get_local_config_servers_path(int index)
{
std::string config_path = get_local_config_path(index);
if (config_path.empty())
return std::string();
return config_path + "/servers";
}
std::string get_local_server_path(const std::string &server_name)
{
config *cfg = get_global_config();
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
for (auto &dir : local_config_directories) {
std::string server_path = dir + "/servers/" + server_name;
if (fs::exists(server_path)) {
return server_path;
}
}
return std::string();
}
std::string get_local_server_env_path(const std::string &server_name)
{
if (server_name.empty())
return std::string();
std::string serverpath = get_local_server_path(server_name);
if (serverpath.empty())
return std::string();
return (fs::path(serverpath) / "server.env").string();
}
std::string server_env(const std::string &server_name) {
if (server_name.empty())
return std::string();
std::string serverpath = localpath::server(server_name);
if (serverpath.empty())
return std::string();
return (fs::path(serverpath) / "server.env").string();
}
std::string get_local_service_path(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string service_env(const std::string &server_name, const std::string &service_name) {
if (server_name.empty() || service_name.empty())
return std::string();
std::string servicepath = localpath::service(server_name, service_name);
if (servicepath.empty())
return std::string();
return (fs::path(servicepath) / "service.env").string();
}
std::string serverpath = get_local_server_path(server_name);
if (serverpath.empty())
return std::string();
return (fs::path(serverpath) / service_name).string();
}
std::string service_hash(const std::string &server_name, const std::string &service_name) {
std::string config_path = localpath::config();
if (server_name.empty() || service_name.empty() || config_path.empty())
return std::string();
return (fs::path(config_path) / ".remote_versions" / server_name / (service_name + ".hash.env")).string();
}
std::string get_local_service_env_path(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string servicepath = get_local_service_path(server_name, service_name);
if (servicepath.empty())
return std::string();
return (fs::path(servicepath) / "service.env").string();
}
} // namespace localfile
std::string get_local_service_hash_path(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string config_path = get_primary_local_config_path();
if (config_path.empty())
return std::string();
// ------------------------------------------------------------------------------------------
namespace localpath {
// /opt/dropshell/templates
std::string system_templates() {
return "/opt/dropshell/templates";
}
// configured by user - defaults to first config_path/backups.
std::string backups_path() {
return gConfig().get_local_backup_path();
}
int num_config_directories() {
return gConfig().get_local_config_directories().size();
}
std::string config(int index) {
return (num_config_directories()>index) ? gConfig().get_local_config_directories()[index] : "";
}
std::string config_templates(int index) {
return (num_config_directories()>index) ? (gConfig().get_local_config_directories()[index]+"/templates") : "";
}
std::string config_servers(int index) {
return (num_config_directories()>index) ? (gConfig().get_local_config_directories()[index]+"/servers") : "";
}
std::string server(const std::string &server_name) {
for (auto &dir : gConfig().get_local_config_directories())
if (fs::exists(dir + "/servers/" + server_name))
return dir + "/servers/" + server_name;
return "";
}
std::string service(const std::string &server_name, const std::string &service_name) {
std::string serverpath = localpath::server(server_name);
return (serverpath.empty() ? "" : (serverpath+"/"+service_name));
}
} // namespace localpath
return (fs::path(config_path) / ".remote_versions" / server_name / (service_name + ".hash.env")).string();
}
// ------------------------------------------------------------------------------------------
@ -152,74 +105,53 @@ std::string get_local_service_hash_path(const std::string &server_name, const st
// |-- (script files)
// |-- backups
std::string get_remote_DROPSHELL_path(const std::string &server_name)
{
if (server_name.empty())
return std::string();
server_env env(server_name);
if (!env.is_valid())
return std::string();
return env.get_DROPSHELL_DIR();
namespace remotepath {
std::string DROPSHELL_DIR(const std::string &server_name)
{
return server_env_manager(server_name).get_DROPSHELL_DIR();
}
std::string get_remote_services_path(const std::string &server_name)
std::string services(const std::string &server_name)
{
if (server_name.empty())
return std::string();
std::string dropshell_path = get_remote_DROPSHELL_path(server_name);
if (dropshell_path.empty())
return std::string();
return (fs::path(dropshell_path) / "services").string();
std::string dsp = DROPSHELL_DIR(server_name);
return (dsp.empty() ? "" : (dsp + "/services"));
}
std::string get_remote_service_path(const std::string &server_name, const std::string &service_name)
std::string service(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string services_path = get_remote_services_path(server_name);
if (services_path.empty())
return std::string();
return (fs::path(services_path) / service_name).string();
std::string services_path = services(server_name);
return (services_path.empty() ? "" : (services_path + "/" + service_name));
}
std::string get_remote_service_config_path(const std::string &server_name, const std::string &service_name)
std::string service_config(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string service_path = get_remote_service_path(server_name, service_name);
if (service_path.empty())
return std::string();
return (fs::path(service_path) / "config").string();
std::string service_path = service(server_name, service_name);
return (service_path.empty() ? "" : (service_path + "/config"));
}
std::string get_remote_service_template_path(const std::string &server_name, const std::string &service_name)
std::string service_template(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string service_path = get_remote_service_path(server_name, service_name);
if (service_path.empty())
return std::string();
return (fs::path(service_path) / "template").string();
std::string service_path = service(server_name, service_name);
return (service_path.empty() ? "" : (service_path + "/template"));
}
std::string get_remote_backups_path(const std::string &server_name)
std::string backups(const std::string &server_name)
{
if (server_name.empty())
return std::string();
std::string dropshell_path = get_remote_DROPSHELL_path(server_name);
if (dropshell_path.empty())
return std::string();
return (fs::path(dropshell_path) / "backups").string();
std::string dsp = DROPSHELL_DIR(server_name);
return (dsp.empty() ? "" : (dsp + "/backups"));
}
std::string get_remote_service_env_file(const std::string &server_name, const std::string &service_name)
std::string service_env(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string service_path = get_remote_service_config_path(server_name, service_name);
if (service_path.empty())
return std::string();
return (fs::path(service_path) / "service.env").string();
std::string service_path = service_config(server_name, service_name);
return (service_path.empty() ? "" : (service_path + "/service.env"));
}
} // namespace remotepath
std::string get_parent(std::string path)
{
if (path.empty())
return std::string();
return fs::path(path).parent_path().string();
}
} // namespace dropshell