List tidy
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 11s

This commit is contained in:
Your Name 2025-05-24 17:00:43 +12:00
parent b3398582ca
commit 60907e5e02
18 changed files with 128 additions and 89 deletions

View File

@ -53,7 +53,7 @@ namespace dropshell
namespace shared_commands namespace shared_commands
{ {
bool backupdata_service(const server_config &server_env, const std::string &service) bool backupdata_service(const ServerConfig &server_env, const std::string &service)
{ {
ASSERT(server_env.is_valid(), "Invalid server environment for " + server_env.get_server_name()); ASSERT(server_env.is_valid(), "Invalid server environment for " + server_env.get_server_name());
std::string server = server_env.get_server_name(); std::string server = server_env.get_server_name();

View File

@ -103,7 +103,7 @@ namespace dropshell
if (server_name.empty() || template_name.empty() || service_name.empty()) if (server_name.empty() || template_name.empty() || service_name.empty())
return false; return false;
server_config server_info(server_name); ServerConfig server_info(server_name);
if (!server_info.is_valid()) if (!server_info.is_valid())
{ {
error << "Server " << server_name << " is not valid" << std::endl; error << "Server " << server_name << " is not valid" << std::endl;

View File

@ -52,7 +52,7 @@ namespace dropshell
bool nuke_service(const std::string &server, const std::string &service) bool nuke_service(const std::string &server, const std::string &service)
{ {
server_config server_env(server); ServerConfig server_env(server);
// step 1 - nuke on remote server. // step 1 - nuke on remote server.
if (server_env.is_valid()) if (server_env.is_valid())

View File

@ -63,7 +63,7 @@ namespace dropshell
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// install service over ssh : SHARED COMMAND // install service over ssh : SHARED COMMAND
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool install_service(const server_config &server_env, const std::string &service) bool install_service(const ServerConfig &server_env, const std::string &service)
{ {
std::string server = server_env.get_server_name(); std::string server = server_env.get_server_name();
LocalServiceInfo service_info = get_service_info(server_env.get_server_name(), service); LocalServiceInfo service_info = get_service_info(server_env.get_server_name(), service);
@ -271,7 +271,7 @@ namespace dropshell
return 0; return 0;
} }
int install_server(const server_config &server) int install_server(const ServerConfig &server)
{ {
// install the dropshell agent on the given server. // install the dropshell agent on the given server.
maketitle("Installing dropshell agent on " + server.get_server_name(), sColour::INFO); maketitle("Installing dropshell agent on " + server.get_server_name(), sColour::INFO);
@ -321,7 +321,7 @@ namespace dropshell
return rval; return rval;
// install the dropshell agent on all servers. // install the dropshell agent on all servers.
std::vector<server_config> servers = get_configured_servers(); std::vector<ServerConfig> servers = get_configured_servers();
for (const auto &server : servers) for (const auto &server : servers)
{ {
rval = install_server(server); rval = install_server(server);
@ -364,7 +364,7 @@ namespace dropshell
return 1; return 1;
} }
server_config server_env(server); ServerConfig server_env(server);
ASSERT(server_env.is_valid(), "Invalid server environment for " + server); ASSERT(server_env.is_valid(), "Invalid server environment for " + server);
if (safearg(ctx.args, 1) == "all") if (safearg(ctx.args, 1) == "all")
{ {

View File

@ -83,44 +83,61 @@ void list_servers() {
tableprint tp("All DropShell Servers"); tableprint tp("All DropShell Servers");
tp.add_row({"Name", "Address", "User", "Health", "Ports"}); tp.add_row({"Name", "Address", "User", "Health", "Ports"});
info << "Checking "<<servers.size() << " servers: " << std::flush;
typedef std::map<std::string, shared_commands::ServiceStatus> tServiceStatusMap;
std::vector<tServiceStatusMap> service_status_maps;
typedef struct {dropshell::ServerConfig server; dropshell::UserConfig user;} server_user_pair;
std::vector<server_user_pair> server_user_pairs;
for (const auto& server : servers)
for (const auto& user : server.get_users())
server_user_pairs.push_back({server, user});
// mutex for the tableprint
std::mutex tp_mutex;
info << "Checking "<<server_user_pairs.size() << " agents: " << std::flush;
int checked = 0; int checked = 0;
transwarp::parallel exec{servers.size()};
auto task = transwarp::for_each(exec, servers.begin(), servers.end(), [&](const server_config& server) {
server_config server_env(server.get_server_name()); transwarp::parallel exec{server_user_pairs.size()};
auto task = transwarp::for_each(exec, server_user_pairs.begin(), server_user_pairs.end(), [&](const server_user_pair& sup) {
ServerConfig server_env(sup.server.get_server_name());
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
error << "Invalid server environment for " << server.get_server_name() << std::endl; error << "Invalid server environment for " << sup.server.get_server_name() << std::endl;
return; return;
} }
int first=true; std::string serviceticks = "";
for (const auto &user : server_env.get_users()) std::string ports_used_str = "";
{ std::set<int> ports_used;
std::map<std::string, shared_commands::ServiceStatus> status = shared_commands::get_all_services_status(server.get_server_name(),user.user);
std::set<int> ports_used; std::map<std::string, shared_commands::ServiceStatus> status = shared_commands::get_all_services_status(sup.server.get_server_name(),sup.user.user);
std::string serviceticks = "";
for (const auto& [service_name, service_status] : status) {
ports_used.insert(service_status.ports.begin(), service_status.ports.end());
serviceticks += shared_commands::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({(first ? server.get_server_name() : ""), (first ? server.get_SSH_HOST() : ""), user.user, serviceticks, ports_used_str}); for (const auto& [service_name, service_status] : status) {
first = false; ports_used.insert(service_status.ports.begin(), service_status.ports.end());
} serviceticks += shared_commands::HealthStatus2String(service_status.health) + " ";
}
++checked; for (const auto& port : ports_used)
// print out a tick character for each server checked. ports_used_str += std::to_string(port) + " ";
info << checked << "" << std::flush; // critical section
{
std::lock_guard<std::mutex> lock(tp_mutex);
tp.add_row({sup.server.get_server_name(), sup.server.get_SSH_HOST(), sup.user.user, serviceticks, ports_used_str});
++checked;
// print out a tick character for each server checked.
info << checked << "" << std::flush;
}
}); });
task->wait(); task->wait();
info << std::endl << std::endl; info << std::endl << std::endl;
tp.sort({0,2});
tp.print(); tp.print();
} }
@ -128,7 +145,7 @@ void list_servers() {
void show_server_details(const std::string& server_name) { void show_server_details(const std::string& server_name) {
server_config env(server_name); ServerConfig env(server_name);
if (!env.is_valid()) { if (!env.is_valid()) {
error << "Error: Invalid server environment file: " << server_name << std::endl; error << "Error: Invalid server environment file: " << server_name << std::endl;
return; return;

View File

@ -92,7 +92,7 @@ namespace dropshell
std::string service = ctx.args[1]; std::string service = ctx.args[1];
std::string backup_arg = ctx.args[2]; std::string backup_arg = ctx.args[2];
server_config server_env(server); ServerConfig server_env(server);
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
error << "Server " << server << " is not valid" << std::endl; error << "Server " << server << " is not valid" << std::endl;
@ -189,7 +189,7 @@ namespace dropshell
{ // installing fresh service { // installing fresh service
info << "4) Install of fresh service..." << std::endl; info << "4) Install of fresh service..." << std::endl;
server_config server_env(server); ServerConfig server_env(server);
if (!shared_commands::install_service(server_env, service)) if (!shared_commands::install_service(server_env, service))
return 1; return 1;
} }

View File

@ -21,7 +21,7 @@ namespace dropshell
if (ctx.args.size() == 0) if (ctx.args.size() == 0)
{ // just the command, no args yet. { // just the command, no args yet.
// list servers // list servers
std::vector<server_config> servers = get_configured_servers(); std::vector<ServerConfig> servers = get_configured_servers();
for (const auto &server : servers) for (const auto &server : servers)
{ {
rawout << server.get_server_name() << std::endl; rawout << server.get_server_name() << std::endl;
@ -54,7 +54,7 @@ namespace dropshell
bool rsync_tree_to_remote( bool rsync_tree_to_remote(
const std::string &local_path, const std::string &local_path,
const std::string &remote_path, const std::string &remote_path,
const server_config &server_env, const ServerConfig &server_env,
bool silent, bool silent,
std::string user) std::string user)
{ {
@ -85,7 +85,7 @@ namespace dropshell
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// cRemoteTempFolder : SHARED CLASS // cRemoteTempFolder : SHARED CLASS
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
cRemoteTempFolder::cRemoteTempFolder(const server_config &server_env, std::string user) : cRemoteTempFolder::cRemoteTempFolder(const ServerConfig &server_env, std::string user) :
mServerEnv(server_env), mUser(user) mServerEnv(server_env), mUser(user)
{ {
std::string p = remotepath(server_env.get_server_name(),user).temp_files() + "/" + random_alphanumeric_string(10); std::string p = remotepath(server_env.get_server_name(),user).temp_files() + "/" + random_alphanumeric_string(10);
@ -110,7 +110,7 @@ namespace dropshell
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// get_all_services_status : SHARED COMMAND // get_all_services_status : SHARED COMMAND
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
std::map<std::string, ServiceStatus> get_all_services_status(const server_config & server_env) std::map<std::string, ServiceStatus> get_all_services_status(const ServerConfig & server_env)
{ {
std::map<std::string, ServiceStatus> status; std::map<std::string, ServiceStatus> status;
for (const auto& user : server_env.get_users()) { for (const auto& user : server_env.get_users()) {
@ -119,7 +119,7 @@ namespace dropshell
return status; return status;
} }
std::map<std::string, ServiceStatus> get_all_services_status(const server_config & server_env, std::string user) std::map<std::string, ServiceStatus> get_all_services_status(const ServerConfig & server_env, std::string user)
{ {
std::map<std::string, ServiceStatus> status; std::map<std::string, ServiceStatus> status;
std::string server_name = server_env.get_server_name(); std::string server_name = server_env.get_server_name();
@ -216,7 +216,7 @@ namespace dropshell
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
HealthStatus is_healthy(const std::string &server, const std::string &service) HealthStatus is_healthy(const std::string &server, const std::string &service)
{ {
server_config env(server); ServerConfig env(server);
if (!env.is_valid()) if (!env.is_valid())
{ {
error << "Server service not initialized" << std::endl; error << "Server service not initialized" << std::endl;
@ -309,7 +309,7 @@ namespace dropshell
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// scp_file_to_remote : SHARED COMMAND // scp_file_to_remote : SHARED COMMAND
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool scp_file_to_remote(const server_config &server_env, const std::string &local_path, const std::string &remote_path, bool silent, std::string user) bool scp_file_to_remote(const ServerConfig &server_env, const std::string &local_path, const std::string &remote_path, bool silent, std::string user)
{ {
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
@ -324,7 +324,7 @@ namespace dropshell
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// scp_file_from_remote : SHARED COMMAND // scp_file_from_remote : SHARED COMMAND
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool scp_file_from_remote(const server_config &server_env, const std::string &remote_path, const std::string &local_path, bool silent, std::string user) bool scp_file_from_remote(const ServerConfig &server_env, const std::string &remote_path, const std::string &local_path, bool silent, std::string user)
{ {
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {

View File

@ -31,26 +31,26 @@ namespace dropshell
class cRemoteTempFolder class cRemoteTempFolder
{ {
public: public:
cRemoteTempFolder(const server_config &server_env, std::string user); // create a temp folder on the remote server cRemoteTempFolder(const ServerConfig &server_env, std::string user); // create a temp folder on the remote server
~cRemoteTempFolder(); // delete the temp folder on the remote server ~cRemoteTempFolder(); // delete the temp folder on the remote server
std::string path() const; // get the path to the temp folder on the remote server std::string path() const; // get the path to the temp folder on the remote server
private: private:
std::string mPath; std::string mPath;
const server_config &mServerEnv; const ServerConfig &mServerEnv;
std::string mUser; std::string mUser;
}; };
bool rsync_tree_to_remote( bool rsync_tree_to_remote(
const std::string &local_path, const std::string &local_path,
const std::string &remote_path, const std::string &remote_path,
const server_config &server_env, const ServerConfig &server_env,
bool silent, bool silent,
std::string user); std::string user);
std::string get_arch(); std::string get_arch();
std::map<std::string, ServiceStatus> get_all_services_status(const server_config & server_env); std::map<std::string, ServiceStatus> get_all_services_status(const ServerConfig & server_env);
std::map<std::string, ServiceStatus> get_all_services_status(const server_config & server_env, std::string user); std::map<std::string, ServiceStatus> get_all_services_status(const ServerConfig & server_env, std::string user);
std::string healthtick(const std::string &server, const std::string &service); std::string healthtick(const std::string &server, const std::string &service);
std::string HealthStatus2String(HealthStatus status); std::string HealthStatus2String(HealthStatus status);
@ -81,20 +81,20 @@ namespace dropshell
std::string mDatetime; std::string mDatetime;
}; };
bool scp_file_to_remote(const server_config &server_env, const std::string &local_path, const std::string &remote_path, bool silent, std::string user); bool scp_file_to_remote(const ServerConfig &server_env, const std::string &local_path, const std::string &remote_path, bool silent, std::string user);
bool scp_file_from_remote(const server_config &server_env, const std::string &remote_path, const std::string &local_path, bool silent, std::string user); bool scp_file_from_remote(const ServerConfig &server_env, const std::string &remote_path, const std::string &local_path, bool silent, std::string user);
// defined in backupdata.cpp, used by restoredata.cpp. // defined in backupdata.cpp, used by restoredata.cpp.
bool backupdata_service(const server_config &server_env, const std::string& service); bool backupdata_service(const ServerConfig &server_env, const std::string& service);
// defined in uninstall.cpp // defined in uninstall.cpp
bool uninstall_service(const server_config &server_env, const std::string &service); bool uninstall_service(const ServerConfig &server_env, const std::string &service);
// defined in nuke.cpp // defined in nuke.cpp
bool nuke_service(const std::string &server, const std::string &service); bool nuke_service(const std::string &server, const std::string &service);
// defined in install.cpp // defined in install.cpp
bool install_service(const server_config &server_env, const std::string &service); bool install_service(const ServerConfig &server_env, const std::string &service);
// defined in create-service.cpp // defined in create-service.cpp
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name, std::string user_override=""); bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name, std::string user_override="");

View File

@ -42,7 +42,7 @@ namespace dropshell
bool ssh_into_server(const std::string &server, std::string user) bool ssh_into_server(const std::string &server, std::string user)
{ {
server_config server_env(server); ServerConfig server_env(server);
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
error << "Server " << server << " is not valid" << std::endl; error << "Server " << server << " is not valid" << std::endl;
@ -54,7 +54,7 @@ namespace dropshell
bool ssh_into_service(const std::string &server, const std::string &service) bool ssh_into_service(const std::string &server, const std::string &service)
{ {
server_config server_env(server); ServerConfig server_env(server);
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
error << "Server " << server << " is not valid" << std::endl; error << "Server " << server << " is not valid" << std::endl;
@ -109,7 +109,7 @@ namespace dropshell
server = arg1; server = arg1;
// get the first user from the server.env file, and ssh in as that user. // get the first user from the server.env file, and ssh in as that user.
server_config server_env(server); ServerConfig server_env(server);
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
error << "Server " << server << " is not valid" << std::endl; error << "Server " << server << " is not valid" << std::endl;

View File

@ -44,7 +44,7 @@ namespace dropshell
bool start_service(const std::string &server, const std::string &service) bool start_service(const std::string &server, const std::string &service)
{ {
server_config server_env(server); ServerConfig server_env(server);
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
error << "Server " << server << " is not valid" << std::endl; error << "Server " << server << " is not valid" << std::endl;

View File

@ -44,7 +44,7 @@ namespace dropshell
bool stop_service(const std::string &server, const std::string &service) bool stop_service(const std::string &server, const std::string &service)
{ {
server_config server_env(server); ServerConfig server_env(server);
if (!server_env.is_valid()) if (!server_env.is_valid())
{ {
error << "Server " << server << " is not valid" << std::endl; error << "Server " << server << " is not valid" << std::endl;

View File

@ -42,7 +42,7 @@ namespace dropshell
} uninstall_command_register; } uninstall_command_register;
namespace shared_commands { namespace shared_commands {
bool uninstall_service(const server_config & server_env, const std::string &service) bool uninstall_service(const ServerConfig & server_env, const std::string &service)
{ {
ASSERT(server_env.is_valid(), "Invalid server environment for " + server_env.get_server_name()); ASSERT(server_env.is_valid(), "Invalid server environment for " + server_env.get_server_name());
std::string server = server_env.get_server_name(); std::string server = server_env.get_server_name();

View File

@ -23,7 +23,7 @@
namespace dropshell namespace dropshell
{ {
server_config::server_config(const std::string &server_name) : mValid(false), mServerName(server_name) ServerConfig::ServerConfig(const std::string &server_name) : mValid(false), mServerName(server_name)
{ {
if (server_name.empty()) if (server_name.empty())
return; return;
@ -110,22 +110,22 @@ namespace dropshell
} }
} }
std::string server_config::get_SSH_HOST() const std::string ServerConfig::get_SSH_HOST() const
{ {
return get_variable("SSH_HOST"); return get_variable("SSH_HOST");
} }
std::string server_config::get_SSH_PORT() const std::string ServerConfig::get_SSH_PORT() const
{ {
return get_variable("SSH_PORT"); return get_variable("SSH_PORT");
} }
std::vector<UserConfig> server_config::get_users() const std::vector<UserConfig> ServerConfig::get_users() const
{ {
return mUsers; return mUsers;
} }
std::string server_config::get_user_dir(const std::string &user) const std::string ServerConfig::get_user_dir(const std::string &user) const
{ {
for (const auto &u : mUsers) for (const auto &u : mUsers)
{ {
@ -137,12 +137,12 @@ namespace dropshell
return ""; return "";
} }
std::string server_config::get_server_name() const std::string ServerConfig::get_server_name() const
{ {
return mServerName; return mServerName;
} }
std::string server_config::get_user_for_service(const std::string &service) const std::string ServerConfig::get_user_for_service(const std::string &service) const
{ {
return dropshell::get_user_for_service(mServerName, service); return dropshell::get_user_for_service(mServerName, service);
} }
@ -158,7 +158,7 @@ namespace dropshell
return ""; return "";
} }
sSSHInfo server_config::get_SSH_INFO(std::string user) const sSSHInfo ServerConfig::get_SSH_INFO(std::string user) const
{ {
ASSERT(!user.empty(), "User is empty, cannot get SSH info."); ASSERT(!user.empty(), "User is empty, cannot get SSH info.");
// Find user in mUsers vector // Find user in mUsers vector
@ -169,24 +169,24 @@ namespace dropshell
return sSSHInfo(get_SSH_HOST(), it->user, get_SSH_PORT(), get_server_name(), it->dir); return sSSHInfo(get_SSH_HOST(), it->user, get_SSH_PORT(), get_server_name(), it->dir);
} }
bool server_config::hasRootUser() const bool ServerConfig::hasRootUser() const
{ {
auto it = std::find_if(mUsers.begin(), mUsers.end(),[](const UserConfig &u) auto it = std::find_if(mUsers.begin(), mUsers.end(),[](const UserConfig &u)
{ return u.user == "root"; }); { return u.user == "root"; });
return it != mUsers.end(); return it != mUsers.end();
} }
bool server_config::hasDocker() const bool ServerConfig::hasDocker() const
{ {
return get_variable("HAS_DOCKER") == "true"; return get_variable("HAS_DOCKER") == "true";
} }
bool server_config::hasRootDocker() const bool ServerConfig::hasRootDocker() const
{ {
return get_variable("DOCKER_ROOTLESS") == "false"; return get_variable("DOCKER_ROOTLESS") == "false";
} }
bool server_config::hasUser(const std::string &user) const bool ServerConfig::hasUser(const std::string &user) const
{ {
auto it = std::find_if(mUsers.begin(), mUsers.end(), auto it = std::find_if(mUsers.begin(), mUsers.end(),
[&user](const UserConfig &u) [&user](const UserConfig &u)
@ -194,19 +194,19 @@ namespace dropshell
return it != mUsers.end(); return it != mUsers.end();
} }
bool server_config::check_remote_dir_exists(const std::string &dir_path, std::string user) const bool ServerConfig::check_remote_dir_exists(const std::string &dir_path, std::string user) const
{ {
sCommand scommand("", "test -d " + quote(dir_path), {}); sCommand scommand("", "test -d " + quote(dir_path), {});
return execute_ssh_command(get_SSH_INFO(user), scommand, cMode::Silent); return execute_ssh_command(get_SSH_INFO(user), scommand, cMode::Silent);
} }
bool server_config::check_remote_file_exists(const std::string &file_path, std::string user) const bool ServerConfig::check_remote_file_exists(const std::string &file_path, std::string user) const
{ {
sCommand scommand("", "test -f " + quote(file_path), {}); sCommand scommand("", "test -f " + quote(file_path), {});
return execute_ssh_command(get_SSH_INFO(user), scommand, cMode::Silent); return execute_ssh_command(get_SSH_INFO(user), scommand, cMode::Silent);
} }
bool server_config::check_remote_items_exist(const std::vector<std::string> &file_paths, std::string user) const bool ServerConfig::check_remote_items_exist(const std::vector<std::string> &file_paths, std::string user) const
{ {
// convert file_paths to a single string, separated by spaces // convert file_paths to a single string, separated by spaces
std::string file_paths_str; std::string file_paths_str;
@ -229,7 +229,7 @@ namespace dropshell
return true; return true;
} }
bool server_config::remove_remote_dir( bool ServerConfig::remove_remote_dir(
const std::string &dir_path, bool silent, std::string user) const const std::string &dir_path, bool silent, std::string user) const
{ {
std::filesystem::path path(dir_path); std::filesystem::path path(dir_path);
@ -258,7 +258,7 @@ namespace dropshell
return execute_ssh_command(sshinfo, scommand, mode); return execute_ssh_command(sshinfo, scommand, mode);
} }
bool server_config::run_remote_template_command( bool ServerConfig::run_remote_template_command(
const std::string &service_name, const std::string &service_name,
const std::string &command, const std::string &command,
std::vector<std::string> args, std::vector<std::string> args,
@ -280,7 +280,7 @@ namespace dropshell
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), mode); return execute_ssh_command(get_SSH_INFO(user), scommand.value(), mode);
} }
bool server_config::run_remote_template_command_and_capture_output( bool ServerConfig::run_remote_template_command_and_capture_output(
const std::string &service_name, const std::string &service_name,
const std::string &command, const std::string &command,
std::vector<std::string> args, std::vector<std::string> args,
@ -300,7 +300,7 @@ namespace dropshell
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), cMode::Defaults, &output); return execute_ssh_command(get_SSH_INFO(user), scommand.value(), cMode::Defaults, &output);
} }
std::string server_config::get_variable(const std::string &name) const std::string ServerConfig::get_variable(const std::string &name) const
{ {
auto it = mVariables.find(name); auto it = mVariables.find(name);
if (it == mVariables.end()) if (it == mVariables.end())
@ -310,7 +310,7 @@ namespace dropshell
return it->second; return it->second;
} }
std::optional<sCommand> server_config::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args, const bool silent) const std::optional<sCommand> ServerConfig::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args, const bool silent) const
{ {
if (command.empty()) if (command.empty())
return std::nullopt; return std::nullopt;
@ -346,9 +346,9 @@ namespace dropshell
return sc; return sc;
} }
std::vector<server_config> get_configured_servers() std::vector<ServerConfig> get_configured_servers()
{ {
std::vector<server_config> servers; std::vector<ServerConfig> servers;
std::vector<std::string> lsdp = gConfig().get_local_server_definition_paths(); std::vector<std::string> lsdp = gConfig().get_local_server_definition_paths();
if (lsdp.empty()) if (lsdp.empty())
@ -367,7 +367,7 @@ namespace dropshell
if (server_name.empty() || server_name[0] == '.' || server_name[0] == '_') if (server_name.empty() || server_name[0] == '.' || server_name[0] == '_')
continue; continue;
server_config env(server_name); ServerConfig env(server_name);
if (!env.is_valid()) if (!env.is_valid())
{ {
std::cerr << "Error: Invalid server environment file: " << entry.path().string() << std::endl; std::cerr << "Error: Invalid server environment file: " << entry.path().string() << std::endl;
@ -430,7 +430,7 @@ namespace dropshell
void get_all_used_commands(std::set<std::string> &commands) void get_all_used_commands(std::set<std::string> &commands)
{ {
std::vector<server_config> servers = get_configured_servers(); std::vector<ServerConfig> servers = get_configured_servers();
for (const auto &server : servers) for (const auto &server : servers)
{ {
auto services = get_server_services_info(server.get_server_name()); auto services = get_server_services_info(server.get_server_name());

View File

@ -32,10 +32,10 @@ namespace dropshell
// SSH_PORT // SSH_PORT
// the following replacements are made in the values: // the following replacements are made in the values:
// ${USER} -> the username of the user running dropshell // ${USER} -> the username of the user running dropshell
class server_config class ServerConfig
{ {
public: public:
server_config(const std::string &server_name); ServerConfig(const std::string &server_name);
bool is_valid() const { return mValid; } bool is_valid() const { return mValid; }
@ -85,11 +85,11 @@ namespace dropshell
std::map<std::string, std::string> mVariables; std::map<std::string, std::string> mVariables;
std::vector<UserConfig> mUsers; std::vector<UserConfig> mUsers;
bool mValid; bool mValid;
}; // class server_config }; // class ServerConfig
std::vector<server_config> get_configured_servers(); std::vector<ServerConfig> get_configured_servers();
std::string get_user_for_service(const std::string &server, const std::string &service); std::string get_user_for_service(const std::string &server, const std::string &service);

View File

@ -73,7 +73,7 @@
// private: // private:
// std::string mServer; // std::string mServer;
// server_config mServerEnv; // ServerConfig mServerEnv;
// LocalServiceInfo mServiceInfo; // LocalServiceInfo mServiceInfo;
// std::string mService; // std::string mService;
// bool mValid; // bool mValid;
@ -240,7 +240,7 @@
// return false; // return false;
// } // }
// server_config env(server_name); // ServerConfig env(server_name);
// if (!env.is_valid()) { // if (!env.is_valid()) {
// std::cerr << "Error: Invalid server environment file: " << server_name << std::endl; // std::cerr << "Error: Invalid server environment file: " << server_name << std::endl;
// return false; // return false;

View File

@ -173,7 +173,7 @@ namespace localpath {
std::string remotepath::DROPSHELL_DIR() const std::string remotepath::DROPSHELL_DIR() const
{ {
return server_config(mServer_name).get_user_dir(mUser); return ServerConfig(mServer_name).get_user_dir(mUser);
} }
std::string remotepath::services() const std::string remotepath::services() const

View File

@ -147,6 +147,27 @@ void tableprint::set_title(const std::string title) {
this->title = title; this->title = title;
} }
// gives the columns to sort by, starting at 0.
void tableprint::sort(std::vector<int> sort_columns)
{
// Skip header row and sort remaining rows
if (rows.size() <= 1) return; // Only header or empty table
// Create a custom comparator that compares rows based on the specified columns
auto comparator = [this, &sort_columns](const std::vector<std::string>& a, const std::vector<std::string>& b) {
for (int col : sort_columns) {
if (col >= 0 && col < a.size() && col < b.size()) {
int cmp = a[col].compare(b[col]);
if (cmp != 0) return cmp < 0;
}
}
return false; // Equal rows maintain original order
};
// Sort rows starting from index 1 (after header)
std::sort(rows.begin() + 1, rows.end(), comparator);
}
void tableprint::add_row(const std::vector<std::string>& row) { void tableprint::add_row(const std::vector<std::string>& row) {
std::vector<std::string> trimmed_row; std::vector<std::string> trimmed_row;
for (const auto& cell : row) { for (const auto& cell : row) {

View File

@ -16,6 +16,7 @@ class tableprint {
void add_row(const std::vector<std::string>& row); void add_row(const std::vector<std::string>& row);
void print(); void print();
void set_title(const std::string title); void set_title(const std::string title);
void sort(std::vector<int> sort_columns);
private: private:
std::vector<std::vector<std::string>> rows; std::vector<std::vector<std::string>> rows;
std::string title; std::string title;