dropshell/source/src/commands/create-service.cpp
Your Name 7a710b525f
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
dropshell release 2025.0521.1906
2025-05-21 19:06:50 +12:00

163 lines
6.4 KiB
C++

#include "command_registry.hpp"
#include "directories.hpp"
#include "shared_commands.hpp"
#include "templates.hpp"
#include "utils/assert.hpp"
#include "utils/utils.hpp"
#include "services.hpp"
#include <fstream>
namespace dropshell
{
int create_service_handler(const CommandContext &ctx);
void create_service_autocomplete(const CommandContext &ctx);
static std::vector<std::string> create_service_name_list = {"create-service"};
// Static registration
struct CreateServiceCommandRegister
{
CreateServiceCommandRegister()
{
CommandRegistry::instance().register_command({create_service_name_list,
create_service_handler,
create_service_autocomplete,
false, // hidden
true, // requires_config
true, // requires_install
3, // min_args (after command)
3, // max_args (after command)
"create-service SERVER SERVICE TEMPLATE",
"Create a service on a server.",
// heredoc
R"(
Create a service on a server.
create-service SERVER SERVICE TEMPLATE create the given service on the given server.
)"});
}
} create_service_command_register;
int create_service_handler(const CommandContext &ctx)
{
std::string server = safearg(ctx.args, 0);
std::string service = safearg(ctx.args, 1);
std::string template_name = safearg(ctx.args, 2);
return shared_commands::create_service(server, template_name, service) ? 0 : 1;
}
void create_service_autocomplete(const CommandContext &ctx)
{
if (ctx.args.size() < 2)
shared_commands::std_autocomplete(ctx);
else
{
if (ctx.args.size() == 2)
{
std::set<std::string> templates = gTemplateManager().get_template_list();
for (const auto &template_name : templates)
rawout << template_name << std::endl;
}
}
}
namespace shared_commands
{
bool print_readme(const template_info &tinfo, std::string server, std::string service)
{
std::vector<std::string> variants_to_try = {"README.txt", "readme.txt", "ReadMe.txt", "README", "readme", "README.md", "readme.md"};
std::filesystem::path readme_path = tinfo.local_template_path();
for (const auto &variant : variants_to_try)
{
if (std::filesystem::exists(readme_path / variant))
{
readme_path = readme_path / variant;
break;
}
}
if (!std::filesystem::exists(readme_path))
return false;
std::map<std::string, std::string> all_env_vars;
get_all_service_env_vars(server, service, all_env_vars);
all_env_vars["LOCAL_CONFIG_PATH"] = localpath::service(server, service);
all_env_vars["LOCAL_TEMPLATE_PATH"] = tinfo.local_template_path().string();
info << std::endl;
std::ifstream readme_file(readme_path);
std::string line;
while (std::getline(readme_file, line))
{
rawout << substitute_provided_key_value_pairs(line, all_env_vars) << std::endl;
}
return true;
}
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name)
{
if (server_name.empty() || template_name.empty() || service_name.empty())
return false;
std::string service_dir = localpath::service(server_name, service_name);
if (service_dir.empty())
{
error << "Couldn't locate server " << server_name << " in any config directory" << std::endl;
info << "Please check the server name is correct and try again" << std::endl;
info << "You can list all servers with 'dropshell servers'" << std::endl;
info << "You can create a new server with 'dropshell create-server " << server_name << "'" << std::endl;
return false;
}
if (std::filesystem::exists(service_dir))
{
error << "Service already exists: " << service_name << std::endl;
debug << "Current service path: " << service_dir << std::endl;
return false;
}
template_info tinfo = gTemplateManager().get_template_info(template_name);
if (!tinfo.is_set())
{
error << "Template '" << template_name << "' not found" << std::endl;
info << "Please check the template name is correct and try again" << std::endl;
info << "You can list all templates with 'dropshell templates'" << std::endl;
info << "You can create a new template with 'dropshell create-template " << template_name << "'" << std::endl;
return false;
}
// check template is all good.
if (!gTemplateManager().test_template(tinfo.local_template_path()))
{
error << "Template '" << template_name << "' is not valid" << std::endl;
return false;
}
// create the service directory
std::filesystem::create_directory(service_dir);
// copy the template config files to the service directory
recursive_copy(tinfo.local_template_path() / "config", service_dir);
info << "Service " << service_name << " created successfully" << std::endl;
if (!print_readme(tinfo, server_name, service_name))
{
info << std::endl;
info << "To complete the installation, please:" << std::endl;
info << "1. edit the service config file: dropshell edit " << server_name << " " << service_name << std::endl;
info << "2. install the remote service: dropshell install " << server_name << " " << service_name << std::endl;
}
return true;
}
} // namespace shared_commands
} // namespace dropshell