This commit is contained in:
Your Name 2025-04-21 11:54:37 +12:00
parent a718f23375
commit c220fca691
4 changed files with 28 additions and 22 deletions

View File

@ -16,7 +16,9 @@ const std::string LICENSE = "MIT";
// Server information structure // Server information structure
struct ServerInfo { struct ServerInfo {
std::string name; std::string name;
std::string address; std::string ssh_host;
std::string ssh_user;
std::string ssh_port;
}; };
// Command handlers // Command handlers

View File

@ -6,7 +6,11 @@
namespace dropshell { namespace dropshell {
server_env::server_env(const std::string& path) { bool server_env::is_valid() {
return mValid;
}
server_env::server_env(const std::string& path) : mValid(false) {
// Construct the full path to _server.env // Construct the full path to _server.env
boost::filesystem::path env_path = boost::filesystem::path(path) / "_server.env"; boost::filesystem::path env_path = boost::filesystem::path(path) / "_server.env";
@ -33,6 +37,9 @@ server_env::server_env(const std::string& path) {
variables.find("SSH_PORT") == variables.end()) { variables.find("SSH_PORT") == variables.end()) {
throw std::runtime_error("Missing required variables in server environment file"); throw std::runtime_error("Missing required variables in server environment file");
} }
mValid = true;
} catch (const boost::property_tree::ini_parser_error& e) { } catch (const boost::property_tree::ini_parser_error& e) {
throw std::runtime_error("Failed to parse server environment file: " + std::string(e.what())); throw std::runtime_error("Failed to parse server environment file: " + std::string(e.what()));
} }

View File

@ -24,8 +24,11 @@ class server_env {
std::string get_SSH_USER(); std::string get_SSH_USER();
std::string get_SSH_PORT(); std::string get_SSH_PORT();
bool is_valid();
private: private:
std::map<std::string, std::string> variables; std::map<std::string, std::string> variables;
bool mValid;
}; };
} // namespace dropshell } // namespace dropshell

View File

@ -1,4 +1,5 @@
#include "dropshell.hpp" #include "dropshell.hpp"
#include "server_env.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
@ -23,31 +24,24 @@ std::vector<ServerInfo> get_configured_servers() {
} }
fs::path servers_dir = fs::path(user_dir) / "servers"; fs::path servers_dir = fs::path(user_dir) / "servers";
if (!fs::exists(servers_dir)) { if (!fs::exists(servers_dir)) {
std::cerr << "Error: Servers directory not found" << std::endl;
return servers; return servers;
} }
for (const auto& entry : fs::directory_iterator(servers_dir)) { for (const auto& entry : fs::directory_iterator(servers_dir)) {
if (fs::is_directory(entry)) { if (fs::is_directory(entry)) {
fs::path env_file = entry.path() / "_server.env"; fs::path env_file = entry.path() / "_server.env";
if (fs::exists(env_file)) { server_env env(env_file.string());
std::ifstream file(env_file.string()); if (!env.is_valid()) {
std::string line; std::cerr << "Error: Invalid server environment file: " << env_file.string() << std::endl;
std::string address; continue;
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
});
}
} }
servers.push_back({
entry.path().filename().string(),
env.get_SSH_HOST(),
env.get_SSH_USER(),
env.get_SSH_PORT()
});
} }
} }
@ -68,7 +62,7 @@ void list_servers() {
for (const auto& server : servers) { for (const auto& server : servers) {
max_name_len = std::max(max_name_len, server.name.length()); max_name_len = std::max(max_name_len, server.name.length());
max_addr_len = std::max(max_addr_len, server.address.length()); max_addr_len = std::max(max_addr_len, server.ssh_host.length());
} }
// Print header // Print header
@ -82,7 +76,7 @@ void list_servers() {
// Print server rows // Print server rows
for (const auto& server : servers) { for (const auto& server : servers) {
std::cout << std::left << std::setw(max_name_len) << server.name << " | " std::cout << std::left << std::setw(max_name_len) << server.name << " | "
<< std::setw(max_addr_len) << server.address << std::endl; << std::setw(max_addr_len) << server.ssh_host << std::endl;
} }
} }