Restore autocompletion working!

This commit is contained in:
Your Name 2025-04-26 00:11:23 +12:00
parent fa7236b4f5
commit 8e0683c997
6 changed files with 71 additions and 0 deletions

View File

@ -42,6 +42,17 @@ _dropshell_completions() {
local services=($(dropshell autocomplete_list_services "$server_name")) local services=($(dropshell autocomplete_list_services "$server_name"))
COMPREPLY=( $(compgen -W "${services[*]}" -- ${cur}) ) COMPREPLY=( $(compgen -W "${services[*]}" -- ${cur}) )
return 0 return 0
elif [[ ${COMP_CWORD} -eq 4 ]]; then
if [[ ${root} == "restore" ]]; then
# Fourth argument is backup file name
local server_name="${COMP_WORDS[2]}"
local service_name="${COMP_WORDS[3]}"
local backup_files=($(dropshell autocomplete_list_backups "$server_name" "$service_name"))
COMPREPLY=( $(compgen -W "${backup_files[*]}" -- ${cur}) )
return 0
fi
COMPREPLY=()
return 0
fi fi
;; ;;
esac esac

View File

@ -12,6 +12,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <iomanip> #include <iomanip>
namespace dropshell { namespace dropshell {
void print_help() { void print_help() {
@ -163,6 +164,19 @@ int main(int argc, char* argv[]) {
return 0; return 0;
} }
if (cmd == "autocomplete_list_backups") {
if (argc < 4) {
std::cerr << "Error: autocomplete_list_backups requires a server name and service name" << std::endl;
return 1;
}
if (cfg->is_config_set()) {
auto backups = dropshell::list_backups(argv[2], argv[3]);
for (const auto& backup : backups)
std::cout << backup << std::endl;
}
return 0;
}
// ------------------------------------------------------------ // ------------------------------------------------------------
// from here we require the config file to be loaded. // from here we require the config file to be loaded.
if (!cfg->is_config_set()) { if (!cfg->is_config_set()) {

View File

@ -557,6 +557,23 @@ void edit_file(const std::string &file_path, const std::string & aftertext)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
bool service_runner::restore(std::string backup_file)
{
std::string command = "restore";
std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
if (!template_command_exists(m_service_info.template_name, command)) {
std::cout << "No restore script for " << m_service_info.template_name << std::endl;
return true; // nothing to restore.
}
/// TOODOOOOOO!!!!!!
std::cout << "Restore not implemented yet" << std::endl;
return true;
// std::string run_cmd = construct_standard_command_run_cmd("restore");
// return execute_ssh_command(run_cmd, "Restore script failed");
}
void service_runner::interactive_ssh_service() void service_runner::interactive_ssh_service()
{ {
std::set<std::string> used_commands = get_used_commands(m_server_name, m_service_info.service_name); std::set<std::string> used_commands = get_used_commands(m_server_name, m_service_info.service_name);

View File

@ -80,6 +80,11 @@ class service_runner {
// 4. copy it to the local user_dir/backups folder // 4. copy it to the local user_dir/backups folder
bool backup(); bool backup();
// restore the service over ssh, using the credentials from server.env (via server_env.hpp)
// 1. copy the backup file to the server's DROPSHELL_DIR/backups folder
// 2. run the restore.sh script on the server, passing the {service_name}.env file as an argument
bool restore(std::string backup_file);
// launch an interactive ssh session on a server or service // launch an interactive ssh session on a server or service
// replaces the current dropshell process with the ssh process // replaces the current dropshell process with the ssh process
void interactive_ssh_service(); void interactive_ssh_service();

View File

@ -111,6 +111,27 @@ std::set<std::string> get_used_commands(const std::string &server_name, const st
return commands; return commands;
} }
std::vector<std::string> list_backups(const std::string &server_name, const std::string &service_name)
{
std::vector<std::string> backups;
if (server_name.empty() || service_name.empty())
return backups;
std::string backups_dir = get_local_backup_path();
if (backups_dir.empty())
return backups;
if (fs::exists(backups_dir)) {
for (const auto& entry : fs::directory_iterator(backups_dir)) {
if (fs::is_regular_file(entry) && entry.path().extension() == ".tgz")
if (entry.path().filename().string().find(service_name) != std::string::npos)
backups.push_back(entry.path().filename().string());
}
}
return backups;
}
bool create_service(const std::string &server_name, const std::string &service_name) bool create_service(const std::string &server_name, const std::string &service_name)
{ {
if (server_name.empty() || service_name.empty()) if (server_name.empty() || service_name.empty())

View File

@ -18,6 +18,9 @@ namespace dropshell {
ServiceInfo get_service_info(const std::string& server_name, const std::string& service_name); ServiceInfo get_service_info(const std::string& server_name, const std::string& service_name);
std::set<std::string> get_used_commands(const std::string& server_name, const std::string& service_name); std::set<std::string> get_used_commands(const std::string& server_name, const std::string& service_name);
// list all backups for a given server and service
std::vector<std::string> list_backups(const std::string& server_name, const std::string& service_name);
bool create_service(const std::string& server_name, const std::string& service_name); bool create_service(const std::string& server_name, const std::string& service_name);
} // namespace dropshell } // namespace dropshell