diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d2a9c5..9653739 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,13 @@ install(FILES src/dropshell-completion.bash RENAME dropshell ) -# Install templates +# Create pre-install script to clean old templates +install(CODE " + message(STATUS \"Removing old template files...\") + execute_process(COMMAND rm -rf /opt/dropshell/templates) +") + +# Install templates with pre-install script install(DIRECTORY templates/ DESTINATION /opt/dropshell/templates ) diff --git a/src/autocomplete.cpp b/src/autocomplete.cpp index 3008a7c..e3ae70c 100644 --- a/src/autocomplete.cpp +++ b/src/autocomplete.cpp @@ -3,6 +3,7 @@ #include "config.hpp" #include #include +#include namespace fs = boost::filesystem; @@ -58,4 +59,51 @@ std::vector autocomplete_list_services(const std::string& server_na return services; } +std::vector autocomplete_list_commands() { + std::vector commands; + std::set 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 \ No newline at end of file diff --git a/src/dropshell-completion.bash b/src/dropshell-completion.bash index e1f4c81..94330a2 100755 --- a/src/dropshell-completion.bash +++ b/src/dropshell-completion.bash @@ -7,7 +7,7 @@ _dropshell_completions() { prev="${COMP_WORDS[COMP_CWORD-1]}" # List of main commands - opts="help version status servers templates autocomplete_list_servers autocomplete_list_services run install backup" + opts="help version status servers templates autocomplete_list_servers autocomplete_list_services autocomplete_list_commands run install backup" # If we're completing the first argument, show all commands if [[ ${COMP_CWORD} -eq 1 ]] ; then @@ -37,6 +37,11 @@ _dropshell_completions() { COMPREPLY=( $(compgen -W "${servers[*]}" -- ${cur}) ) return 0 ;; + autocomplete_list_commands) + # No additional completions needed + COMPREPLY=() + return 0 + ;; run|install|backup) # First argument after run/install/backup is server name local servers=($(dropshell autocomplete_list_servers)) diff --git a/src/dropshell.hpp b/src/dropshell.hpp index 13215cf..86381f1 100644 --- a/src/dropshell.hpp +++ b/src/dropshell.hpp @@ -31,5 +31,8 @@ void show_server_details(const std::string& server_name); // Utility functions std::vector get_configured_servers(); +std::vector autocomplete_list_servers(); +std::vector autocomplete_list_services(const std::string& server_name); +std::vector autocomplete_list_commands(); } // namespace dropshell \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1e35c10..ae215c6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -117,6 +117,14 @@ int main(int argc, char* argv[]) { return 0; } + if (cmd == "autocomplete_list_commands") { + auto commands = dropshell::autocomplete_list_commands(); + for (const auto& command : commands) { + std::cout << command << std::endl; + } + return 0; + } + if (cmd == "install") { if (argc < 3) { std::cerr << "Error: install command requires server name and optionally service name" << std::endl; diff --git a/src/server_service.cpp b/src/server_service.cpp index b272143..a14118e 100644 --- a/src/server_service.cpp +++ b/src/server_service.cpp @@ -308,7 +308,7 @@ bool server_service::is_healthy() std::string env_path = service_dir + "/" + m_service_name + ".env"; // Run status script, does not display output. - return execute_ssh_command("'cd " + script_dir + " && /bin/bash status.sh " + env_path + " > /dev/null 2>&1'",""); + return execute_ssh_command("'cd " + script_dir + " && /bin/bash _status.sh " + env_path + " > /dev/null 2>&1'",""); } std::string server_service::healthtick() diff --git a/templates/squashkiwi/status.sh b/templates/squashkiwi/_status.sh similarity index 100% rename from templates/squashkiwi/status.sh rename to templates/squashkiwi/_status.sh