This commit is contained in:
parent
46396369d7
commit
62191cceed
@ -29,9 +29,22 @@ std::vector<std::string> CommandRegistry::list_commands(bool include_hidden) con
|
|||||||
return std::vector<std::string>(out.begin(), out.end());
|
return std::vector<std::string>(out.begin(), out.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> CommandRegistry::list_primary_commands(bool include_hidden) const {
|
||||||
|
std::set<std::string> 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<std::string>(out.begin(), out.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void CommandRegistry::autocomplete(const std::vector<std::string>& args) const {
|
void CommandRegistry::autocomplete(const std::vector<std::string>& args) const {
|
||||||
|
// dropshell autocomplete <command> <arg> <arg> ...
|
||||||
if (args.size() < 3) {
|
if (args.size() < 3) {
|
||||||
for (const auto& name : list_commands(false)) {
|
for (const auto& name : list_primary_commands(false)) {
|
||||||
std::cout << name << std::endl;
|
std::cout << name << std::endl;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -39,7 +52,7 @@ void CommandRegistry::autocomplete(const std::vector<std::string>& args) const {
|
|||||||
std::string cmd = args[2];
|
std::string cmd = args[2];
|
||||||
auto* info = find_command(cmd);
|
auto* info = find_command(cmd);
|
||||||
if (info && info->autocomplete) {
|
if (info && info->autocomplete) {
|
||||||
CommandContext ctx{args};
|
CommandContext ctx{args[0], cmd, std::vector<std::string>(args.begin() + 2, args.end())};
|
||||||
info->autocomplete(ctx);
|
info->autocomplete(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,7 +8,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
struct CommandContext {
|
struct CommandContext {
|
||||||
|
std::string exename;
|
||||||
|
std::string command;
|
||||||
std::vector<std::string> args;
|
std::vector<std::string> args;
|
||||||
|
|
||||||
// Add more fields as needed (e.g., config pointer, output stream, etc.)
|
// Add more fields as needed (e.g., config pointer, output stream, etc.)
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -35,6 +38,7 @@ public:
|
|||||||
|
|
||||||
// List all commands (optionally including hidden)
|
// List all commands (optionally including hidden)
|
||||||
std::vector<std::string> list_commands(bool include_hidden = false) const;
|
std::vector<std::string> list_commands(bool include_hidden = false) const;
|
||||||
|
std::vector<std::string> list_primary_commands(bool include_hidden = false) const;
|
||||||
|
|
||||||
// For autocomplete
|
// For autocomplete
|
||||||
void autocomplete(const std::vector<std::string>& args) const;
|
void autocomplete(const std::vector<std::string>& args) const;
|
||||||
|
@ -140,18 +140,18 @@ int edit_service_config(const std::string &server, const std::string &service)
|
|||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
int edit_handler(const CommandContext& ctx) {
|
int edit_handler(const CommandContext& ctx) {
|
||||||
// edit dropshell config
|
// edit dropshell config
|
||||||
if (ctx.args.size() < 3)
|
if (ctx.args.size() < 1)
|
||||||
return edit_config();
|
return edit_config();
|
||||||
|
|
||||||
// edit server config
|
// edit server config
|
||||||
if (ctx.args.size() < 4) {
|
if (ctx.args.size() < 2) {
|
||||||
edit_server(safearg(ctx.args,2));
|
edit_server(safearg(ctx.args,0));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// edit service config
|
// edit service config
|
||||||
if (ctx.args.size() < 5) {
|
if (ctx.args.size() < 3) {
|
||||||
edit_service_config(safearg(ctx.args,2), safearg(ctx.args,3));
|
edit_service_config(safearg(ctx.args,0), safearg(ctx.args,1));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
107
src/commands/list.cpp
Normal file
107
src/commands/list.cpp
Normal file
@ -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 <unistd.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <libassert/assert.hpp>
|
||||||
|
|
||||||
|
namespace dropshell {
|
||||||
|
|
||||||
|
void list_autocomplete(const CommandContext& ctx);
|
||||||
|
int list_handler(const CommandContext& ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> 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 "<<servers.size() << " servers: " << std::flush;
|
||||||
|
int checked = 0;
|
||||||
|
|
||||||
|
transwarp::parallel exec{servers.size()};
|
||||||
|
auto task = transwarp::for_each(exec, servers.begin(), servers.end(), [&](const ServerInfo& server) {
|
||||||
|
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server.name);
|
||||||
|
|
||||||
|
std::set<int> 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
|
@ -68,7 +68,7 @@ int main(int argc, char* argv[]) {
|
|||||||
<< info->help_description << std::endl;
|
<< info->help_description << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
CommandContext ctx{args};
|
CommandContext ctx{args[0], cmd, std::vector<std::string>(args.begin() + 2, args.end())};
|
||||||
return info->handler(ctx);
|
return info->handler(ctx);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -71,39 +71,6 @@ ServerInfo get_server_info(const std::string &server_name)
|
|||||||
return ServerInfo();
|
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 "<<servers.size() << " servers: " << std::flush;
|
|
||||||
int checked = 0;
|
|
||||||
|
|
||||||
transwarp::parallel exec{servers.size()};
|
|
||||||
auto task = transwarp::for_each(exec, servers.begin(), servers.end(), [&](const ServerInfo& server) {
|
|
||||||
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server.name);
|
|
||||||
|
|
||||||
std::set<int> 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) {
|
void show_server_details(const std::string& server_name) {
|
||||||
server_env_manager env(server_name);
|
server_env_manager env(server_name);
|
||||||
|
@ -324,13 +324,13 @@ int die(const std::string & msg) {
|
|||||||
|
|
||||||
std::string safearg(const std::vector<std::string> & args, int index)
|
std::string safearg(const std::vector<std::string> & args, int index)
|
||||||
{
|
{
|
||||||
if (index >= args.size()) return "";
|
if (index<0 || index >= args.size()) return "";
|
||||||
return args[index];
|
return args[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string safearg(int argc, char *argv[], int index)
|
std::string safearg(int argc, char *argv[], int index)
|
||||||
{
|
{
|
||||||
if (index >= argc) return "";
|
if (index<0 || index >= argc) return "";
|
||||||
return argv[index];
|
return argv[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user