diff --git a/source/src/commands/create-server.cpp b/source/src/commands/create-server.cpp new file mode 100644 index 0000000..0bfce2f --- /dev/null +++ b/source/src/commands/create-server.cpp @@ -0,0 +1,63 @@ +#include "command_registry.hpp" +#include "config.hpp" +#include "utils/utils.hpp" +#include "utils/directories.hpp" +#include "shared_commands.hpp" +#include "version.hpp" + +#include +#include +#include +#include +#include +#include "utils/assert.hpp" + +namespace dropshell { + +void create_server_autocomplete(const CommandContext& ctx); +int create_server_handler(const CommandContext& ctx); + +static std::vector create_server_name_list={"create-server"}; + +// Static registration +struct CreateServerCommandRegister { + CreateServerCommandRegister() { + CommandRegistry::instance().register_command({ + create_server_name_list, + create_server_handler, + create_server_autocomplete, + false, // hidden + false, // requires_config + false, // requires_install + 0, // min_args (after command) + 1, // max_args (after command) + "create-server [SERVER]", + "Create a new server entry on this host.", + // heredoc + R"( + Create a new server entry on this host. + Note you will need to use ds install SERVER to prepare the service for use. + + create-server SERVER + )" + }); + } +} create_server_command_register; + + +void create_server_autocomplete(const CommandContext& ctx) { + return; // can't autocomplete as it's a new server! +} + + +int create_server_handler(const CommandContext& ctx) { + // create a new server entry on this host + if (ctx.args.size() == 0) { + error << "No server name provided" << std::endl; + return 1; + } + bool ok = create_server(ctx.args[0]); + return ok ? 0 : 1; +} + +} // namespace dropshell \ No newline at end of file diff --git a/source/src/commands/nuke.cpp b/source/src/commands/destroy.cpp similarity index 93% rename from source/src/commands/nuke.cpp rename to source/src/commands/destroy.cpp index f388042..ccd4e5a 100644 --- a/source/src/commands/nuke.cpp +++ b/source/src/commands/destroy.cpp @@ -14,7 +14,7 @@ namespace dropshell { int nuke_handler(const CommandContext &ctx); - static std::vector nuke_name_list = {"nuke"}; + static std::vector nuke_name_list = {"destroy","nuke"}; // Static registration struct NukeCommandRegister @@ -29,15 +29,15 @@ namespace dropshell true, // requires_install 2, // min_args (after command) 2, // max_args (after command) - "nuke SERVER SERVICE|all", - "Nuke a service on a server. Destroys everything, both local and remote!", + "destroy SERVER SERVICE|all", + "Destroy a service on a server. Erases everything, both local and remote!", // heredoc R"( Nuke a service. Examples: - nuke SERVER SERVICE nuke the given service on the given server. - nuke SERVER all nuke all services on the given server. + destroy SERVER SERVICE destroy the given service on the given server. + destroy SERVER all destroy all services on the given server. Note: This command is destructive and will destroy all data and all configuration, both on the dropshell host and on the remote server. diff --git a/source/src/commands/help.cpp b/source/src/commands/help.cpp index 9d4244b..0de6f9a 100644 --- a/source/src/commands/help.cpp +++ b/source/src/commands/help.cpp @@ -148,6 +148,7 @@ int help_handler(const CommandContext& ctx) { info << std::endl; show_command("ssh"); info << std::endl; + show_command("create-server"); show_command("create-service"); } return 0; diff --git a/source/src/servers.cpp b/source/src/servers.cpp index a0ee97f..7ac0648 100644 --- a/source/src/servers.cpp +++ b/source/src/servers.cpp @@ -7,6 +7,7 @@ #include "config.hpp" #include "templates.hpp" #include "contrib/transwarp.hpp" +#include "utils/output.hpp" #include #include @@ -76,16 +77,16 @@ bool create_server(const std::string &server_name) // 1. check if server name already exists std::string server_existing_dir = localpath::server(server_name); if (!server_existing_dir.empty()) { - std::cerr << "Error: Server name already exists: " << server_name << std::endl; - std::cerr << "Current server path: " << server_existing_dir << std::endl; + error << "Error: Server name already exists: " << server_name << std::endl; + info << "Current server path: " << server_existing_dir << std::endl; return false; } // 2. create a new directory in the user config directory auto lsdp = gConfig().get_local_server_definition_paths(); if (lsdp.empty() || lsdp[0].empty()) { - std::cerr << "Error: Local server definition path not found" << std::endl; - std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl; + error << "Error: Local server definition path not found" << std::endl; + info << "Run 'dropshell edit' to configure DropShell" << std::endl; return false; } std::string server_dir = lsdp[0] + "/" + server_name; @@ -93,20 +94,20 @@ bool create_server(const std::string &server_name) // 3. create a template server.env file in the server directory std::string user = getenv("USER"); - std::string server_env_path = server_dir + "/server.env"; + std::string server_env_path = server_dir + "/server.json"; std::ofstream server_env_file(server_env_path); - server_env_file << "SSH_HOST=" << server_name << std::endl; - server_env_file << "SSH_USER=" << user << std::endl; - server_env_file << "SSH_PORT=" << 22 << std::endl; - server_env_file << std::endl; - server_env_file << "DROPSHELL_DIR=/home/"+user+"/.dropshell" << std::endl; + server_env_file << "{" << std::endl; + server_env_file << " \"SSH_HOST\": \"" << server_name << "\"," << std::endl; + server_env_file << " \"SSH_USER\": \"" << user << "\"," << std::endl; + server_env_file << " \"SSH_PORT\": " << 22 << "," << std::endl; + server_env_file << " \"DROPSHELL_DIR\": \"" << "/home/"+user+"/.dropshell\"" << std::endl; + server_env_file << "}" << std::endl; server_env_file.close(); std::cout << "Server created successfully: " << server_name << std::endl; std::cout << "Please complete the installation:" <