This commit is contained in:
Your Name 2025-04-23 22:05:28 +12:00
parent f9b67ff4c0
commit 986b9be2b4
4 changed files with 17 additions and 15 deletions

View File

@ -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,8 +35,8 @@ 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;
@ -46,39 +46,42 @@ bool config::load_config() {
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 <path>' 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;
}

View File

@ -17,7 +17,7 @@ class config {
void init_local_config_directory(const std::string& path);
private:
std::string local_config_directory;
std::string mLocalConfigPath;
};

View File

@ -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 <path>' to initialise the user directory and create a configuration file." << std::endl;
std::cerr << "Please run 'dropshell init <path>' to initialise the user directory and create a configuration file." << std::endl;
return 1;
}

View File

@ -21,7 +21,6 @@ std::vector<ServerInfo> 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;
}