diff --git a/data/modules.conf b/data/modules.conf new file mode 100644 index 0000000..3d5c08c --- /dev/null +++ b/data/modules.conf @@ -0,0 +1,2 @@ +[modules] +modules_path = /usr/lib/dpm/modules \ No newline at end of file diff --git a/include/ConfigManager.hpp b/include/ConfigManager.hpp index 90e7554..fc71db6 100644 --- a/include/ConfigManager.hpp +++ b/include/ConfigManager.hpp @@ -43,10 +43,12 @@ #include #include +#include "dpm_interface_helpers.hpp" + class ConfigManager { public: // Constructor - ConfigManager(const std::string& config_dir = "/etc/dpm/conf.d/"); + ConfigManager( const std::string& config_dir = DPMDefaultPaths::CONFIG_DIR ); // Load all configuration files from the config directory bool loadConfigurations(); diff --git a/include/dpm_interface_helpers.hpp b/include/dpm_interface_helpers.hpp index 8c947e9..b7d95cd 100644 --- a/include/dpm_interface_helpers.hpp +++ b/include/dpm_interface_helpers.hpp @@ -43,3 +43,9 @@ struct CommandArgs { // parse dpm cli arguments into a serialized structure CommandArgs parse_args( int argc, char * argv[] ); + +// default system paths +struct DPMDefaultPaths { + static const std::string MODULE_PATH; + static const std::string CONFIG_DIR; +}; diff --git a/src/dpm.cpp b/src/dpm.cpp index fd232fb..f883f5f 100644 --- a/src/dpm.cpp +++ b/src/dpm.cpp @@ -64,7 +64,7 @@ int main(int argc, char* argv[]) { // Load configuration files if (!g_config_manager.loadConfigurations()) { - std::cerr << "Warning: Some configuration files could not be loaded." << std::endl; + std::cerr << "Warning: No configuration files present or loaded from '" << DPMDefaultPaths::CONFIG_DIR << "*.conf', reverting to defaults." << std::endl; // Continue execution, as we might be able to use default values } @@ -79,16 +79,15 @@ int main(int argc, char* argv[]) // If CLI argument was provided, use it if (!args.module_path.empty()) { module_path = args.module_path; - } - // Otherwise, check configuration file - else { + } else { + // Otherwise, check configuration file const char* config_module_path = g_config_manager.getConfigValue("modules", "module_path"); if (config_module_path) { module_path = config_module_path; } // Finally, use default if nothing else is available else { - module_path = "/usr/lib/dpm/modules/"; + module_path = DPMDefaultPaths::MODULE_PATH; } } diff --git a/src/dpm_interface.cpp b/src/dpm_interface.cpp index f81cfc8..f391763 100644 --- a/src/dpm_interface.cpp +++ b/src/dpm_interface.cpp @@ -65,23 +65,23 @@ int main_check_module_path(const ModuleLoader& loader) loader.get_module_path(path); if (!std::filesystem::exists(path)) { - std::cerr << "Module path does not exist: " << path << std::endl; + std::cerr << "FATAL: modules.modules_path does not exist: " << path << std::endl; return 1; } if (!std::filesystem::is_directory(path)) { - std::cerr << "Module path is not a directory: " << path << std::endl; + std::cerr << "FATAL: modules.modules_path is not a directory: " << path << std::endl; return 1; } try { auto perms = std::filesystem::status(path).permissions(); if ((perms & std::filesystem::perms::owner_read) == std::filesystem::perms::none) { - std::cerr << "Permission denied: " << path << std::endl; + std::cerr << "FATAL: Permission denied: " << path << std::endl; return 1; } } catch (const std::filesystem::filesystem_error&) { - std::cerr << "Permission denied: " << path << std::endl; + std::cerr << "FATAL: Permission denied: " << path << std::endl; return 1; } @@ -122,18 +122,18 @@ int main_list_modules(const ModuleLoader& loader) // set the module path DPMErrorCategory get_path_error = loader.get_module_path(path); if ( get_path_error != DPMErrorCategory::SUCCESS ) { - std::cerr << "Failed to get module path" << std::endl; + std::cerr << "Failed to get modules.modules_path" << std::endl; return 1; } DPMErrorCategory list_error = loader.list_available_modules(modules); if (list_error != DPMErrorCategory::SUCCESS) { - std::cerr << "No modules found in: " << path << std::endl; + std::cerr << "FATAL: No modules found in modules.modules_path: " << path << std::endl; return 1; } if (modules.empty()) { - std::cout << "No modules found in '" << path << "'." << std::endl; + std::cerr << "FATAL: No modules found in modules.modules_path: '" << path << "'." << std::endl; return 0; } @@ -155,7 +155,7 @@ int main_list_modules(const ModuleLoader& loader) } if (valid_modules.empty()) { - std::cout << "No valid modules found in '" << path << "'." << std::endl; + std::cerr << "FATAL: No valid modules found in modules.modules_path: '" << path << "'." << std::endl; return 0; } @@ -193,7 +193,7 @@ int main_list_modules(const ModuleLoader& loader) const int column_spacing = 4; // Display the table header - std::cout << "\nAvailable modules in '" << path << "':" << std::endl << std::endl; + std::cout << "\nAvailable modules in modules.modules_path: '" << path << "':" << std::endl << std::endl; std::cout << std::left << std::setw(max_name_length + column_spacing) << "MODULE" << std::setw(max_version_length + column_spacing) << "VERSION" << "DESCRIPTION" << std::endl; diff --git a/src/dpm_interface_helpers.cpp b/src/dpm_interface_helpers.cpp index 35c4d40..f8a8bd7 100644 --- a/src/dpm_interface_helpers.cpp +++ b/src/dpm_interface_helpers.cpp @@ -30,6 +30,10 @@ #include "dpm_interface_helpers.hpp" +// Define the static constants +const std::string DPMDefaultPaths::MODULE_PATH = "/usr/lib/dpm/modules/"; +const std::string DPMDefaultPaths::CONFIG_DIR = "/etc/dpm/conf.d/"; + /** * Parse command line arguments for DPM. * @@ -56,7 +60,7 @@ CommandArgs parse_args(int argc, char* argv[]) { CommandArgs args; - args.module_path = ""; // Start with empty path to allow for config fallback + args.module_path = ""; static struct option long_options[] = { {"module-path", required_argument, 0, 'm'}, @@ -74,7 +78,7 @@ CommandArgs parse_args(int argc, char* argv[]) case 'h': std::cout << "Usage: dpm [options] [module-name] [module args...] [module-command] [command-args]\n\n" << "Options:\n\n" - << " -m, --module-path PATH Path to DPM modules (overrides config)\n" + << " -m, --module-path PATH Path to DPM modules (overrides modules.modules_path in config)\n" << " -h, --help Show this help message\n\n"; exit(0); case '?':