improved path mgmt

master
Chris Punches 2025-03-02 03:03:00 -05:00
parent 7f492d3b87
commit 146ce1972f
6 changed files with 30 additions and 17 deletions

2
data/modules.conf Normal file
View File

@ -0,0 +1,2 @@
[modules]
modules_path = /usr/lib/dpm/modules

View File

@ -43,10 +43,12 @@
#include <string.h> #include <string.h>
#include <dirent.h> #include <dirent.h>
#include "dpm_interface_helpers.hpp"
class ConfigManager { class ConfigManager {
public: public:
// Constructor // 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 // Load all configuration files from the config directory
bool loadConfigurations(); bool loadConfigurations();

View File

@ -43,3 +43,9 @@ struct CommandArgs {
// parse dpm cli arguments into a serialized structure // parse dpm cli arguments into a serialized structure
CommandArgs parse_args( int argc, char * argv[] ); CommandArgs parse_args( int argc, char * argv[] );
// default system paths
struct DPMDefaultPaths {
static const std::string MODULE_PATH;
static const std::string CONFIG_DIR;
};

View File

@ -64,7 +64,7 @@ int main(int argc, char* argv[])
{ {
// Load configuration files // Load configuration files
if (!g_config_manager.loadConfigurations()) { 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 // 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 CLI argument was provided, use it
if (!args.module_path.empty()) { if (!args.module_path.empty()) {
module_path = args.module_path; module_path = args.module_path;
} } else {
// Otherwise, check configuration file // Otherwise, check configuration file
else {
const char* config_module_path = g_config_manager.getConfigValue("modules", "module_path"); const char* config_module_path = g_config_manager.getConfigValue("modules", "module_path");
if (config_module_path) { if (config_module_path) {
module_path = config_module_path; module_path = config_module_path;
} }
// Finally, use default if nothing else is available // Finally, use default if nothing else is available
else { else {
module_path = "/usr/lib/dpm/modules/"; module_path = DPMDefaultPaths::MODULE_PATH;
} }
} }

View File

@ -65,23 +65,23 @@ int main_check_module_path(const ModuleLoader& loader)
loader.get_module_path(path); loader.get_module_path(path);
if (!std::filesystem::exists(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; return 1;
} }
if (!std::filesystem::is_directory(path)) { 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; return 1;
} }
try { try {
auto perms = std::filesystem::status(path).permissions(); auto perms = std::filesystem::status(path).permissions();
if ((perms & std::filesystem::perms::owner_read) == std::filesystem::perms::none) { 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; return 1;
} }
} catch (const std::filesystem::filesystem_error&) { } catch (const std::filesystem::filesystem_error&) {
std::cerr << "Permission denied: " << path << std::endl; std::cerr << "FATAL: Permission denied: " << path << std::endl;
return 1; return 1;
} }
@ -122,18 +122,18 @@ int main_list_modules(const ModuleLoader& loader)
// set the module path // set the module path
DPMErrorCategory get_path_error = loader.get_module_path(path); DPMErrorCategory get_path_error = loader.get_module_path(path);
if ( get_path_error != DPMErrorCategory::SUCCESS ) { 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; return 1;
} }
DPMErrorCategory list_error = loader.list_available_modules(modules); DPMErrorCategory list_error = loader.list_available_modules(modules);
if (list_error != DPMErrorCategory::SUCCESS) { 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; return 1;
} }
if (modules.empty()) { 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; return 0;
} }
@ -155,7 +155,7 @@ int main_list_modules(const ModuleLoader& loader)
} }
if (valid_modules.empty()) { 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; return 0;
} }
@ -193,7 +193,7 @@ int main_list_modules(const ModuleLoader& loader)
const int column_spacing = 4; const int column_spacing = 4;
// Display the table header // 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::cout << std::left << std::setw(max_name_length + column_spacing) << "MODULE"
<< std::setw(max_version_length + column_spacing) << "VERSION" << std::setw(max_version_length + column_spacing) << "VERSION"
<< "DESCRIPTION" << std::endl; << "DESCRIPTION" << std::endl;

View File

@ -30,6 +30,10 @@
#include "dpm_interface_helpers.hpp" #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. * Parse command line arguments for DPM.
* *
@ -56,7 +60,7 @@
CommandArgs parse_args(int argc, char* argv[]) CommandArgs parse_args(int argc, char* argv[])
{ {
CommandArgs args; CommandArgs args;
args.module_path = ""; // Start with empty path to allow for config fallback args.module_path = "";
static struct option long_options[] = { static struct option long_options[] = {
{"module-path", required_argument, 0, 'm'}, {"module-path", required_argument, 0, 'm'},
@ -74,7 +78,7 @@ CommandArgs parse_args(int argc, char* argv[])
case 'h': case 'h':
std::cout << "Usage: dpm [options] [module-name] [module args...] [module-command] [command-args]\n\n" std::cout << "Usage: dpm [options] [module-name] [module args...] [module-command] [command-args]\n\n"
<< "Options:\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"; << " -h, --help Show this help message\n\n";
exit(0); exit(0);
case '?': case '?':