diff --git a/src/config.cpp b/src/config.cpp index 40a0017..8f02de7 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -25,7 +25,7 @@ config::~config() { bool config::load_config() { std::string config_path = get_local_dropshell_config_path(); - if (config_path.empty()) + if (config_path.empty() || !fs::exists(config_path)) return false; envmanager config_env(config_path); @@ -35,50 +35,53 @@ bool config::load_config() { return false; } - local_config_directory = config_env.get_variable_substituted("local.config.directory"); - if (local_config_directory.empty()) + mLocalConfigPath = config_env.get_variable_substituted("local.config.directory"); + if (mLocalConfigPath.empty()) { std::cerr << "Warning: User directory not set in config" << std::endl; - return false; + return false; } return true; } void config::save_config() { - std::string config_path = get_local_dropshell_config_path(); - if (config_path.empty()) + if (mLocalConfigPath.empty()) { std::cerr << "Warning: Unable to save configuration file, as DropShell has not been initialised."<< std::endl; std::cerr << "Please run 'dropshell init ' to initialise DropShell." << std::endl; return; } + std::string config_path = get_local_dropshell_config_path(); envmanager config_env(config_path); - config_env.set_variable("local.config.directory", local_config_directory); + config_env.set_variable("local.config.directory", mLocalConfigPath); config_env.save(); } bool config::is_config_set() const -{ - return !local_config_directory.empty(); +{ + return !mLocalConfigPath.empty(); } bool config::get_local_config_directory(std::string& path) const { - path = local_config_directory; + path = mLocalConfigPath; return !path.empty(); } void config::init_local_config_directory(const std::string& path) { + if (path.empty()) + throw std::runtime_error("Warning: Unable to initialise local config directory, as the path is empty."); + // Convert to canonical path fs::path abs_path = fs::canonical(path); // The directory must exist if (!fs::exists(abs_path)) { - throw std::runtime_error("The specifieduser directory does not exist: " + abs_path.string()); + throw std::runtime_error("The local config directory does not exist: " + abs_path.string()); } - local_config_directory = abs_path.string(); + mLocalConfigPath = abs_path.string(); save_config(); std::cout << "Local config directory initialized to: " << abs_path.string() << std::endl; } diff --git a/src/config.hpp b/src/config.hpp index e960aa0..2ef159f 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -17,7 +17,7 @@ class config { void init_local_config_directory(const std::string& path); private: - std::string local_config_directory; + std::string mLocalConfigPath; }; diff --git a/src/main.cpp b/src/main.cpp index 4ed121e..a9fd657 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -144,7 +144,7 @@ int main(int argc, char* argv[]) { // ------------------------------------------------------------ // from here we require the config file to be loaded. if (!cfg->is_config_set()) { - std::cerr << "Error: Failed to load configuration." << std::endl << "Please run 'dropshell init ' to initialise the user directory and create a configuration file." << std::endl; + std::cerr << "Please run 'dropshell init ' to initialise the user directory and create a configuration file." << std::endl; return 1; } diff --git a/src/servers.cpp b/src/servers.cpp index fb3e4b8..ea07271 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -21,7 +21,6 @@ std::vector get_configured_servers() { std::string servers_dir = get_local_config_servers_path(); if (servers_dir.empty()) { - std::cerr << "Error: Servers directory not found" << std::endl; return servers; }