Fairly big refactor...

This commit is contained in:
Your Name
2025-04-23 21:50:04 +12:00
parent 50e340f1c5
commit 9a3dd18525
24 changed files with 853 additions and 660 deletions

160
src/utils/directories.cpp Normal file
View File

@ -0,0 +1,160 @@
#include "directories.hpp"
#include "config.hpp"
#include "server_env.hpp"
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
namespace dropshell {
std::string get_local_dropshell_config_path()
{
// Try ~/.config/dropshell/dropshell.conf
const char* home = std::getenv("HOME");
if (home) {
fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.env";
return user_path.string();
}
std::cerr << "Warning: Couldn't determine user directory" << std::endl;
return std::string();
}
std::string get_local_system_templates_path()
{
return "/opt/dropshell/templates";
}
std::string get_local_config_path()
{
config *cfg = get_global_config();
std::string user_dir;
if (!cfg->get_local_config_directory(user_dir)) {
return std::string();
}
return user_dir;
}
std::string get_local_config_templates_path()
{
std::string config_path = get_local_config_path();
if (config_path.empty()) {
return std::string();
}
return config_path + "/templates";
}
std::string get_local_config_servers_path()
{
std::string config_path = get_local_config_path();
if (config_path.empty()) {
return std::string();
}
return config_path + "/servers";
}
std::string get_local_config_backups_path()
{
std::string config_path = get_local_config_path();
if (config_path.empty()) {
return std::string();
}
return config_path + "/backups";
}
std::string get_local_server_path(const std::string &server_name)
{
std::string config_path = get_local_config_path();
if (config_path.empty())
return std::string();
return config_path + "/servers/" + server_name;
}
std::string get_local_server_env_path(const std::string &server_name)
{
std::string serverpath = get_local_server_path(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)
{
std::string serverpath = get_local_server_path(server_name);
if (serverpath.empty())
return std::string();
return (fs::path(serverpath) / service_name).string();
}
std::string get_local_service_env_path(const std::string &server_name, const std::string &service_name)
{
std::string servicepath = get_local_service_path(server_name, service_name);
if (servicepath.empty())
return std::string();
return (fs::path(servicepath) / "service.env").string();
}
// ------------------------------------------------------------------------------------------
// remote paths
// DROPSHELL_DIR
// |-- service name
// |-- config
// |-- service.env
// |-- (user config files)
// |-- template
// |-- (script files)
// |-- backups
std::string get_remote_DROPSHELL_path(const std::string &server_name)
{
server_env env(server_name);
if (!env.is_valid())
return std::string();
return env.get_DROPSHELL_DIR();
}
std::string get_remote_service_path(const std::string &server_name, const std::string &service_name)
{
std::string dropshell_path = get_remote_DROPSHELL_path(server_name);
if (dropshell_path.empty())
return std::string();
return (fs::path(dropshell_path) / service_name).string();
}
std::string get_remote_service_config_path(const std::string &server_name, const std::string &service_name)
{
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 get_remote_service_template_path(const std::string &server_name, const std::string &service_name)
{
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 get_remote_service_backups_path(const std::string &server_name, const std::string &service_name)
{
std::string service_path = get_remote_service_path(server_name, service_name);
if (service_path.empty())
return std::string();
return (fs::path(service_path) / "backups").string();
}
std::string get_remote_service_env_file(const std::string &server_name, const std::string &service_name)
{
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();
}
} // namespace dropshell