From fa7236b4f587f6b35abfe26791cb00338035bdcc Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 25 Apr 2025 23:52:08 +1200 Subject: [PATCH] Customisable backup directory. --- src/config.cpp | 27 ++++++++++++++++++++++----- src/config.hpp | 3 +++ src/main.cpp | 10 ++++------ src/service_runner.cpp | 2 +- src/utils/directories.cpp | 13 +++++++++++++ src/utils/directories.hpp | 2 ++ src/utils/envmanager.cpp | 11 ++++++----- 7 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 00fd8b1..5492b49 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -6,6 +6,7 @@ #include #include "config.hpp" #include "utils/envmanager.hpp" +#include "utils/utils.hpp" namespace fs = boost::filesystem; namespace pt = boost::property_tree; @@ -18,6 +19,7 @@ config *get_global_config() { return gConfig; } + config::config() { } config::~config() { @@ -32,12 +34,18 @@ bool config::load_config() { if (!config_env.load()) return false; - std::string mDirectories = config_env.get_variable_substituted("local.config.directories"); - if (mDirectories.empty()) - return false; + std::string directories = config_env.get_variable_substituted("local.config.directories"); + if (directories.empty()) + return false; // Split the directories string into a vector of strings - mLocalConfigPaths = string2multi(mDirectories); + mLocalConfigPaths = string2multi(directories); + + mLocalBackupPath = config_env.get_variable_substituted("local.backup.directory"); + + // legacy config file conversion + if (mLocalBackupPath.empty() && mLocalConfigPaths.size()>0) + mLocalBackupPath = mLocalConfigPaths[0] + "/backups"; //std::cout << "Local config path: " << mLocalConfigPath << std::endl; return true; @@ -56,12 +64,13 @@ void config::save_config() envmanager config_env(config_path); config_env.set_variable("local.config.directories", multi2string(mLocalConfigPaths)); + config_env.set_variable("local.backup.path", mLocalBackupPath); config_env.save(); } bool config::is_config_set() const { - return !mLocalConfigPaths.empty(); + return !mLocalConfigPaths.empty() && !mLocalBackupPath.empty(); } @@ -91,9 +100,17 @@ bool config::add_local_config_directory(const std::string &path) return true; } + if (mLocalBackupPath.empty()) + mLocalBackupPath = path_str + "/backups"; + std::cerr << "Warning: The local config directory is already registered: " << path_str << std::endl; std::cerr << "No changes made to the DropShell configuration." << std::endl; return false; } +const std::string &config::get_local_backup_path() const +{ + return mLocalBackupPath; +} + } // namespace dropshell \ No newline at end of file diff --git a/src/config.hpp b/src/config.hpp index 63a8206..32852b7 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -17,8 +17,11 @@ class config { const std::vector & get_local_config_directories() const; bool add_local_config_directory(const std::string& path); + const std::string & get_local_backup_path() const; + private: std::vector mLocalConfigPaths; + std::string mLocalBackupPath; }; diff --git a/src/main.cpp b/src/main.cpp index 5462e7e..1c643e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,7 +29,7 @@ void print_help() { std::cout << std::endl; std::cout << "Service commands: (if no service is specified, all services for the server are affected)" << std::endl; std::cout << " install SERVER [SERVICE] Install/reinstall/update service(s). Non-destructive." << std::endl; - std::cout << " edit SERVER [SERVICE] Edit the configuration of the server/service." << std::endl; + std::cout << " edit [SERVER] [SERVICE] Edit the configuration of dropshell/server/service." << std::endl; std::cout << " COMMAND SERVER [SERVICE] Run a command on service(s)." << std::endl; std::cout << std::endl; std::cout << "Standard commands: install, uninstall, backup, restore, start, stop" << std::endl; @@ -237,11 +237,9 @@ int main(int argc, char* argv[]) { if (cmd == "edit" && argc < 4) { if (argc < 3) - { - std::cerr << "Error: edit requires a server name and optionally service name" << std::endl; - return 1; - } - dropshell::edit_server(argv[2]); + dropshell::edit_file(dropshell::get_local_dropshell_config_path(), "Please ensure any directories you have introduced in the config file exist."); + else + dropshell::edit_server(argv[2]); return 0; } diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 8a1bba4..4ed7fd0 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -325,7 +325,7 @@ bool service_runner::backup() { } // Create backups directory locally if it doesn't exist - std::string local_backups_dir = get_local_config_backups_path(0); + std::string local_backups_dir = get_local_backup_path(); if (local_backups_dir.empty()) { std::cerr << "Error: Local backups directory not found - is DropShell initialised?" << std::endl; return false; diff --git a/src/utils/directories.cpp b/src/utils/directories.cpp index e0a9886..d2605dc 100644 --- a/src/utils/directories.cpp +++ b/src/utils/directories.cpp @@ -38,12 +38,25 @@ int getNumConfigDirectories() std::string get_local_config_path(int index) { config *cfg = get_global_config(); + if (!cfg) + return std::string(); + std::vector local_config_directories = cfg->get_local_config_directories(); if (index < 0 || index >= local_config_directories.size()) return std::string(); return local_config_directories[index]; } + +std::string get_local_backup_path() +{ + config *cfg = get_global_config(); + if (!cfg) + return std::string(); + + return cfg->get_local_backup_path(); +} + std::string get_local_config_templates_path(int index) { std::string config_path = get_local_config_path(index); diff --git a/src/utils/directories.hpp b/src/utils/directories.hpp index 492e5a8..871c053 100644 --- a/src/utils/directories.hpp +++ b/src/utils/directories.hpp @@ -9,6 +9,8 @@ namespace dropshell { std::string get_local_dropshell_config_path(); std::string get_local_system_templates_path(); + std::string get_local_backup_path(); + int getNumConfigDirectories(); std::string get_local_config_path(int index); std::string get_local_config_templates_path(int index); diff --git a/src/utils/envmanager.cpp b/src/utils/envmanager.cpp index 009f799..8a1916b 100644 --- a/src/utils/envmanager.cpp +++ b/src/utils/envmanager.cpp @@ -36,7 +36,7 @@ bool envmanager::load() { std::string value = line.substr(pos + 1); // trim whitespace from the key and value - m_variables[trim(key)] = trim(value); + m_variables[dequote(trim(key))] = dequote(trim(value)); } } file.close(); @@ -50,13 +50,13 @@ void envmanager::save() { } for (const auto& pair : m_variables) { - file << pair.first << "=" << pair.second << std::endl; + file << pair.first << "=" << quote(pair.second) << std::endl; } file.close(); } std::string envmanager::get_variable(std::string key) const { - key = trim(key); + key = dequote(trim(key)); // Use case-insensitive comparison to find the key for (const auto& pair : m_variables) { @@ -91,7 +91,7 @@ void envmanager::add_variables(std::map variables) { } void envmanager::set_variable(std::string key, std::string value) { - m_variables[trim(key)] = trim(value); + m_variables[dequote(trim(key))] = dequote(trim(value)); } void envmanager::clear_variables() { @@ -115,7 +115,8 @@ std::string envmanager::expand_patterns(std::string str) const { result = result.replace(match.position(), match.length(), value); } - + + // dequote the result return result; }