Yay
This commit is contained in:
parent
63b9980a61
commit
65b6e2b193
10
src/main.cpp
10
src/main.cpp
@ -228,6 +228,16 @@ int main(int argc, char* argv[]) {
|
||||
dropshell::create_server(argv[2]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "create-service") {
|
||||
if (argc < 4) {
|
||||
std::cerr << "Error: create-service requires a server name and service name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
dropshell::create_service(argv[2], argv[3]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// handle running a command.
|
||||
for (const auto& command : commands) {
|
||||
if (cmd == command) {
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "utils/directories.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <iostream>
|
||||
|
||||
@ -109,4 +110,50 @@ std::set<std::string> get_used_commands(const std::string &server_name, const st
|
||||
return commands;
|
||||
}
|
||||
|
||||
bool create_service(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
if (server_name.empty() || service_name.empty())
|
||||
return false;
|
||||
|
||||
std::string service_dir = get_local_service_path(server_name, service_name);
|
||||
if (!service_dir.empty() && fs::exists(service_dir))
|
||||
{
|
||||
std::cerr << "Error: Service already exists: " << service_name << std::endl;
|
||||
std::cerr << "Current service path: " << service_dir << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (service_dir.empty())
|
||||
{
|
||||
std::cerr << "Error: Couldn't locate server " << server_name << " in any config directory" << std::endl;
|
||||
std::cerr << "Please check the server name is correct and try again" << std::endl;
|
||||
std::cerr << "You can list all servers with 'dropshell servers'" << std::endl;
|
||||
std::cerr << "You can create a new server with 'dropshell create-server " << server_name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
template_info tinfo;
|
||||
if (!get_template_info(service_name, tinfo))
|
||||
{
|
||||
std::cerr << "Error: Template for service '" << service_name << "' not found" << std::endl;
|
||||
std::cerr << "Please check the service name is correct and try again" << std::endl;
|
||||
std::cerr << "You can list all templates with 'dropshell templates'" << std::endl;
|
||||
std::cerr << "You can create a new template with 'dropshell create-template " << service_name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// create the service directory
|
||||
fs::create_directory(service_dir);
|
||||
|
||||
// copy the template example files to the service directory
|
||||
recursive_copy(tinfo.path+"/example", service_dir);
|
||||
|
||||
std::cout << "Service created successfully: " << service_name << std::endl;
|
||||
std::cout << "Please edit the service.env file to configure the service, it is located at: "<<std::endl;
|
||||
std::cout << " " << service_dir << "/service.env" << std::endl;
|
||||
std::cout << "Then run 'dropshell install " << server_name << " " << service_name << "' to install the service" << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
||||
|
@ -17,6 +17,9 @@ namespace dropshell {
|
||||
std::vector<ServiceInfo> get_server_services_info(const std::string& server_name);
|
||||
ServiceInfo get_service_info(const std::string& server_name, const std::string& service_name);
|
||||
std::set<std::string> get_used_commands(const std::string& server_name, const std::string& service_name);
|
||||
|
||||
bool create_service(const std::string& server_name, const std::string& service_name);
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
namespace dropshell {
|
||||
|
||||
void maketitle(const std::string& title) {
|
||||
@ -139,4 +140,22 @@ int str2int(const std::string &str)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void recursive_copy(const std::string & source, const std::string & destination) {
|
||||
try {
|
||||
if (std::filesystem::is_directory(source)) {
|
||||
if (!std::filesystem::exists(destination)) {
|
||||
std::filesystem::create_directory(destination);
|
||||
}
|
||||
for (const auto& entry : std::filesystem::directory_iterator(source)) {
|
||||
recursive_copy(entry.path(), destination / entry.path().filename());
|
||||
}
|
||||
} else if (std::filesystem::is_regular_file(source)) {
|
||||
std::filesystem::copy(source, destination, std::filesystem::copy_options::overwrite_existing);
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error& ex) {
|
||||
std::cerr << "Error copying " << source << " to " << destination << ": " << ex.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
@ -24,4 +24,6 @@ std::vector<std::string> string2multi(std::string values);
|
||||
|
||||
int str2int(const std::string & str);
|
||||
|
||||
void recursive_copy(const std::string & source, const std::string & destination);
|
||||
|
||||
} // namespace dropshell
|
Loading…
x
Reference in New Issue
Block a user