diff --git a/src/main.cpp b/src/main.cpp index 9931e95..67a65e6 100644 --- a/src/main.cpp +++ b/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) { diff --git a/src/services.cpp b/src/services.cpp index 9ab256a..a98d659 100644 --- a/src/services.cpp +++ b/src/services.cpp @@ -3,6 +3,7 @@ #include "utils/directories.hpp" #include "templates.hpp" #include "config.hpp" +#include "utils/utils.hpp" #include #include @@ -109,4 +110,50 @@ std::set 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: "< 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 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 diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index b7e9092..76f0819 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -4,6 +4,7 @@ #include #include #include +#include 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 \ No newline at end of file diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index 4ffbc13..bd85ac2 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -24,4 +24,6 @@ std::vector string2multi(std::string values); int str2int(const std::string & str); +void recursive_copy(const std::string & source, const std::string & destination); + } // namespace dropshell \ No newline at end of file