dropshell/src/autocomplete.cpp
Your Name b49bc0c5de Tidy
2025-04-21 16:33:46 +12:00

109 lines
3.5 KiB
C++

#include "dropshell.hpp"
#include "init_user_directory.hpp"
#include "config.hpp"
#include <boost/filesystem.hpp>
#include <iostream>
#include <set>
namespace fs = boost::filesystem;
namespace dropshell {
std::vector<std::string> autocomplete_list_servers() {
std::vector<std::string> servers;
std::string user_dir;
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)) {
std::cerr << "Error: Servers directory not found" << std::endl;
return servers;
}
// List all server directories
for (const auto& entry : fs::directory_iterator(servers_dir)) {
if (fs::is_directory(entry)) {
servers.push_back(entry.path().filename().string());
}
}
return servers;
}
std::vector<std::string> autocomplete_list_services(const std::string& server_name) {
std::vector<std::string> services;
std::string user_dir;
if (!get_user_directory(user_dir)) {
std::cerr << "Error: User directory not set" << std::endl;
return services;
}
fs::path server_dir = fs::path(user_dir) / "servers" / server_name;
if (!fs::exists(server_dir)) {
std::cerr << "Error: Server directory not found" << std::endl;
return services;
}
// Look for .env files in the server directory
for (const auto& entry : fs::directory_iterator(server_dir)) {
if (entry.path().extension() == ".env" && entry.path().filename().string() != "_server.env") {
services.push_back(entry.path().stem().string());
}
}
return services;
}
std::vector<std::string> autocomplete_list_commands() {
std::vector<std::string> commands;
std::set<std::string> unique_commands; // To ensure deduplication
// System templates directory
const std::string system_templates_dir = "/opt/dropshell/templates";
// User templates directory
std::string user_templates_dir;
if (!get_user_directory(user_templates_dir)) {
std::cerr << "Error: User directory not set" << std::endl;
return commands;
}
user_templates_dir += "/usertemplates";
// Helper function to add commands from a directory
auto add_commands_from_dir = [&unique_commands](const std::string& dir_path) {
if (!fs::exists(dir_path)) {
return;
}
// Iterate through all template directories
for (const auto& template_entry : fs::directory_iterator(dir_path)) {
if (!fs::is_directory(template_entry)) {
continue;
}
// Look for shell files in each template directory
for (const auto& file_entry : fs::directory_iterator(template_entry.path())) {
if (fs::is_regular_file(file_entry) &&
file_entry.path().extension() == ".sh" &&
file_entry.path().filename().string()[0] != '_') {
unique_commands.insert(file_entry.path().stem().string());
}
}
}
};
// Add commands from both template locations
add_commands_from_dir(system_templates_dir);
add_commands_from_dir(user_templates_dir);
// Convert set to vector for return
commands.assign(unique_commands.begin(), unique_commands.end());
return commands;
}
} // namespace dropshell