Customisable backup directory.

This commit is contained in:
Your Name 2025-04-25 23:52:08 +12:00
parent 3c99ad1503
commit fa7236b4f5
7 changed files with 51 additions and 17 deletions

View File

@ -6,6 +6,7 @@
#include <boost/property_tree/ini_parser.hpp>
#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

View File

@ -17,8 +17,11 @@ class config {
const std::vector<std::string> & 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<std::string> mLocalConfigPaths;
std::string mLocalBackupPath;
};

View File

@ -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;
}

View File

@ -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;

View File

@ -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<std::string> 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);

View File

@ -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);

View File

@ -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<std::string, std::string> 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() {
@ -116,6 +116,7 @@ std::string envmanager::expand_patterns(std::string str) const {
result = result.replace(match.position(), match.length(), value);
}
// dequote the result
return result;
}