This commit is contained in:
Your Name
2025-04-21 11:19:05 +12:00
parent 8c85fe8819
commit 10d663971c
20 changed files with 839 additions and 280 deletions

89
src/servers.cpp Normal file
View File

@ -0,0 +1,89 @@
#include "dropshell.hpp"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
namespace fs = boost::filesystem;
namespace dropshell {
std::vector<ServerInfo> get_configured_servers() {
std::vector<ServerInfo> servers;
std::string user_dir;
if (!is_config_loaded()) {
std::cerr << "Error: Config not loaded" << std::endl;
return servers;
}
if (!get_user_directory(user_dir)) {
std::cerr << "Error: User directory not set" << std::endl;
return servers;
}
fs::path servers_dir = fs::path(user_dir) / "servers";
if (!fs::exists(servers_dir)) {
return servers;
}
for (const auto& entry : fs::directory_iterator(servers_dir)) {
if (fs::is_directory(entry)) {
fs::path env_file = entry.path() / "_server.env";
if (fs::exists(env_file)) {
std::ifstream file(env_file.string());
std::string line;
std::string address;
while (std::getline(file, line)) {
if (boost::starts_with(line, "SSH_ADDRESS=")) {
address = line.substr(12);
break;
}
}
if (!address.empty()) {
servers.push_back({
entry.path().filename().string(),
address
});
}
}
}
}
return servers;
}
void list_servers() {
auto servers = get_configured_servers();
if (servers.empty()) {
std::cout << "No servers configured." << std::endl;
return;
}
// Find maximum lengths for formatting
size_t max_name_len = 4; // "Name" is 4 chars
size_t max_addr_len = 7; // "Address" is 7 chars
for (const auto& server : servers) {
max_name_len = std::max(max_name_len, server.name.length());
max_addr_len = std::max(max_addr_len, server.address.length());
}
// Print header
std::cout << std::left << std::setw(max_name_len) << "Name" << " | "
<< std::setw(max_addr_len) << "Address" << std::endl;
// Print separator
std::cout << std::string(max_name_len, '-') << "-+-"
<< std::string(max_addr_len, '-') << std::endl;
// Print server rows
for (const auto& server : servers) {
std::cout << std::left << std::setw(max_name_len) << server.name << " | "
<< std::setw(max_addr_len) << server.address << std::endl;
}
}
} // namespace dropshell