dropshell/src/services.cpp
2025-04-25 17:42:02 +12:00

113 lines
3.9 KiB
C++

#include "services.hpp"
#include "utils/envmanager.hpp"
#include "utils/directories.hpp"
#include "templates.hpp"
#include "config.hpp"
#include <boost/filesystem.hpp>
#include <iostream>
namespace fs = boost::filesystem;
namespace dropshell {
std::vector<ServiceInfo> get_server_services_info(const std::string& server_name) {
std::vector<ServiceInfo> services;
if (server_name.empty())
return services;
std::vector<std::string> local_config_directories = get_global_config()->get_local_config_directories();
if (local_config_directories.empty()) {
std::cerr << "Error: No local config directories found" << std::endl;
std::cerr << "Run 'dropshell init' to initialise DropShell" << std::endl;
return services;
}
for (int i = 0; i < getNumConfigDirectories(); i++) {
std::string serverpath = get_local_config_servers_path(i);
if (serverpath.empty()) {
std::cerr << "Error: Server directory not found: " << serverpath << std::endl;
return services;
}
fs::path server_dir = fs::path(serverpath) / server_name;
if (fs::exists(server_dir)) {
for (const auto& entry : fs::directory_iterator(server_dir)) {
if (fs::is_directory(entry)) {
ServiceInfo service = get_service_info(server_name, entry.path().filename().string());
if (!service.template_name.empty()) {
services.push_back(service);
}
}
}
} // end of for (int i = 0; i < getNumConfigDirectories(); i++)
}
return services;
}
ServiceInfo get_service_info(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return ServiceInfo();
std::string service_dir = get_local_service_path(server_name, service_name);
if (service_dir.empty())
return ServiceInfo();
ServiceInfo service;
service.path = service_dir;
service.service_name = service_name;
// now set the template name and path.
std::string local_service_env_path = get_local_service_env_path(server_name, service_name);
envmanager env(local_service_env_path);
if (!env.load()) {
std::cerr << "Error: service.env missing from " << local_service_env_path << std::endl;
return ServiceInfo();
}
service.template_name = env.get_variable("TEMPLATE");
if (service.template_name.empty()) {
std::cerr << "Error: TEMPLATE variable not defined in " << local_service_env_path << std::endl;
return ServiceInfo();
}
template_info tinfo;
if (!get_template_info(service.template_name, tinfo)) {
std::cerr << "Error: Template '" << service.template_name << "' not found" << std::endl;
return ServiceInfo();
}
// find the template path
service.template_path = tinfo.path;
return service;
}
std::set<std::string> get_used_commands(const std::string &server_name, const std::string &service_name)
{
std::set<std::string> commands;
if (server_name.empty() || service_name.empty())
return commands;
ServiceInfo service = get_service_info(server_name, service_name);
if (service.template_path.empty()) {
std::cerr << "Error: Service not found: " << service_name << std::endl;
return commands;
}
// iterate over all files in the template path, and add the command name to the set.
// commands are .sh files that don't begin with _
fs::path template_path = fs::path(service.template_path);
for (const auto& entry : fs::directory_iterator(template_path)) {
if (fs::is_regular_file(entry) && entry.path().extension() == ".sh" && (entry.path().filename().string().rfind("_", 0) != 0))
commands.insert(entry.path().stem().string());
}
return commands;
}
} // namespace dropshell