diff --git a/src/command_registry.cpp b/src/command_registry.cpp index 9f5c5bc..88e1516 100644 --- a/src/command_registry.cpp +++ b/src/command_registry.cpp @@ -29,9 +29,22 @@ std::vector CommandRegistry::list_commands(bool include_hidden) con return std::vector(out.begin(), out.end()); } +std::vector CommandRegistry::list_primary_commands(bool include_hidden) const { + std::set out; + for (const auto& cmd : all_commands_) { + if (!cmd->hidden || include_hidden) { + if (cmd->names.size() > 0) + out.insert(cmd->names[0]); + } + } + return std::vector(out.begin(), out.end()); +} + + void CommandRegistry::autocomplete(const std::vector& args) const { + // dropshell autocomplete ... if (args.size() < 3) { - for (const auto& name : list_commands(false)) { + for (const auto& name : list_primary_commands(false)) { std::cout << name << std::endl; } return; @@ -39,7 +52,7 @@ void CommandRegistry::autocomplete(const std::vector& args) const { std::string cmd = args[2]; auto* info = find_command(cmd); if (info && info->autocomplete) { - CommandContext ctx{args}; + CommandContext ctx{args[0], cmd, std::vector(args.begin() + 2, args.end())}; info->autocomplete(ctx); } } \ No newline at end of file diff --git a/src/command_registry.hpp b/src/command_registry.hpp index faf2d76..338631f 100644 --- a/src/command_registry.hpp +++ b/src/command_registry.hpp @@ -8,7 +8,10 @@ #include struct CommandContext { + std::string exename; + std::string command; std::vector args; + // Add more fields as needed (e.g., config pointer, output stream, etc.) }; @@ -35,6 +38,7 @@ public: // List all commands (optionally including hidden) std::vector list_commands(bool include_hidden = false) const; + std::vector list_primary_commands(bool include_hidden = false) const; // For autocomplete void autocomplete(const std::vector& args) const; diff --git a/src/commands/edit.cpp b/src/commands/edit.cpp index 4fb3752..8d2df34 100644 --- a/src/commands/edit.cpp +++ b/src/commands/edit.cpp @@ -140,18 +140,18 @@ int edit_service_config(const std::string &server, const std::string &service) // ------------------------------------------------------------------------------------------------ int edit_handler(const CommandContext& ctx) { // edit dropshell config - if (ctx.args.size() < 3) + if (ctx.args.size() < 1) return edit_config(); // edit server config - if (ctx.args.size() < 4) { - edit_server(safearg(ctx.args,2)); + if (ctx.args.size() < 2) { + edit_server(safearg(ctx.args,0)); return 0; } // edit service config - if (ctx.args.size() < 5) { - edit_service_config(safearg(ctx.args,2), safearg(ctx.args,3)); + if (ctx.args.size() < 3) { + edit_service_config(safearg(ctx.args,0), safearg(ctx.args,1)); return 0; } diff --git a/src/commands/list.cpp b/src/commands/list.cpp new file mode 100644 index 0000000..613ab89 --- /dev/null +++ b/src/commands/list.cpp @@ -0,0 +1,107 @@ +#include "command_registry.hpp" +#include "config.hpp" +#include "utils/utils.hpp" +#include "service_runner.hpp" +#include "utils/directories.hpp" +#include "standard_autocomplete.hpp" +#include "servers.hpp" +#include "tableprint.hpp" +#include "transwarp.hpp" + +#include +#include +#include +#include +#include +#include + +namespace dropshell { + +void list_autocomplete(const CommandContext& ctx); +int list_handler(const CommandContext& ctx); + +static std::vector list_name_list={"list","ls","info","-l"}; + +// Static registration +struct ListCommandRegister { + ListCommandRegister() { + CommandRegistry::instance().register_command({ + list_name_list, + list_handler, + list_autocomplete, + false, // hidden + false, // requires_config + 0, // min_args (after command) + 2, // max_args (after command) + "list [SERVER] [SERVICE]", + "List dropshell, server or service configuration" + }); + } +} list_command_register; + + +// ------------------------------------------------------------------------------------------------ +// list command autocomplete +// ------------------------------------------------------------------------------------------------ +void list_autocomplete(const CommandContext& ctx) { + std_autocomplete(list_name_list, ctx); +} + +// ------------------------------------------------------------------------------------------------ +// list command handler +// ------------------------------------------------------------------------------------------------ +int list_handler(const CommandContext& ctx) { + if (ctx.args.size() == 0) { + list_servers(); + return 0; + } + + std::cout << "List handler called with " << ctx.args.size() << " args\n"; + return 0; +} + + + +// https://github.com/bloomen/transwarp?tab=readme-ov-file#range-functions +void list_servers() { + auto servers = get_configured_servers(); + + if (servers.empty()) { + std::cout << "No servers found" << std::endl; + std::cout << "Please run 'dropshell edit' to set up dropshell." << std::endl; + std::cout << "Then run 'dropshell create-server' to create a server." << std::endl; + return; + } + + tableprint tp("All DropShell Servers"); + tp.add_row({"Name", "User", "Address", "Health", "Ports"}); + + std::cout << "Checking "< status = service_runner::get_all_services_status(server.name); + + std::set ports_used; + std::string serviceticks = ""; + for (const auto& [service_name, service_status] : status) { + ports_used.insert(service_status.ports.begin(), service_status.ports.end()); + serviceticks += service_runner::HealthStatus2String(service_status.health) + " "; + } + std::string ports_used_str = ""; + for (const auto& port : ports_used) + ports_used_str += std::to_string(port) + " "; + + tp.add_row({server.name, server.ssh_user, server.ssh_host, serviceticks, ports_used_str}); + ++checked; + // print out a tick character for each server checked. + std::cout << checked << " ✓ " << std::flush; + }); + task->wait(); + std::cout << std::endl << std::endl; + tp.print(); +} + + +} // namespace dropshell \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index deee8cd..4bb2d9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,7 @@ int main(int argc, char* argv[]) { << info->help_description << std::endl; return 1; } - CommandContext ctx{args}; + CommandContext ctx{args[0], cmd, std::vector(args.begin() + 2, args.end())}; return info->handler(ctx); } diff --git a/src/servers.cpp b/src/servers.cpp index 551fa45..c477d65 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -71,39 +71,6 @@ ServerInfo get_server_info(const std::string &server_name) return ServerInfo(); } -// https://github.com/bloomen/transwarp?tab=readme-ov-file#range-functions -void list_servers() { - auto servers = get_configured_servers(); - - tableprint tp("All DropShell Servers"); - tp.add_row({"Name", "User", "Address", "Health", "Ports"}); - - std::cout << "Checking "< status = service_runner::get_all_services_status(server.name); - - std::set ports_used; - std::string serviceticks = ""; - for (const auto& [service_name, service_status] : status) { - ports_used.insert(service_status.ports.begin(), service_status.ports.end()); - serviceticks += service_runner::HealthStatus2String(service_status.health) + " "; - } - std::string ports_used_str = ""; - for (const auto& port : ports_used) - ports_used_str += std::to_string(port) + " "; - - tp.add_row({server.name, server.ssh_user, server.ssh_host, serviceticks, ports_used_str}); - ++checked; - // print out a tick character for each server checked. - std::cout << checked << " ✓ " << std::flush; - }); - task->wait(); - std::cout << std::endl << std::endl; - tp.print(); -} void show_server_details(const std::string& server_name) { server_env_manager env(server_name); diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index d9f5e12..b39f5c8 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -324,13 +324,13 @@ int die(const std::string & msg) { std::string safearg(const std::vector & args, int index) { - if (index >= args.size()) return ""; + if (index<0 || index >= args.size()) return ""; return args[index]; } std::string safearg(int argc, char *argv[], int index) { - if (index >= argc) return ""; + if (index<0 || index >= argc) return ""; return argv[index]; }