error handling layer implemented, header boilerplated for licensing info

This commit is contained in:
Chris Punches
2025-02-27 01:57:21 -05:00
parent e30ed309bd
commit a5e2c86882
14 changed files with 855 additions and 204 deletions

View File

@@ -1,14 +1,34 @@
#include "dpm_interface.hpp"
/*
* DPM Interface methods.
*
* DPM Interface methods. These are wrappers of DPM functionality that are meant to handle user view, turning
* error codes into human-presentable information, etc. Features are defined internally, these will only ever be
* wrappers of existing features to provide the human/cli interface.
* These are for entry points for the DPM cli.
*
* These are wrappers of DPM functionality that are meant to handle user
* view, turning error codes into human-presentable information, etc.
*
* Also includes helpers related to the CLI.
*/
// check if the module path exists
/**
* Verify that the module path exists and is accessible.
*
* This function checks if the configured module path exists, is a directory,
* and has the necessary read permissions.
*
* @param loader Reference to a ModuleLoader object that provides the module path
*
* @return 0 if the path exists and is accessible, 1 otherwise
*
* The function performs the following checks:
* 1. Retrieves the module path from the loader
* 2. Verifies that the path exists in the filesystem
* 3. Confirms that the path is a directory
* 4. Checks that the directory has read permissions
*
* If any check fails, an appropriate error message is displayed to stderr.
*/
int main_check_module_path(const ModuleLoader& loader)
{
std::string path;
@@ -38,20 +58,46 @@ int main_check_module_path(const ModuleLoader& loader)
return 0;
}
// list the modules
/**
* List all available and valid DPM modules.
*
* This function retrieves and displays a formatted table of available DPM modules
* from the specified module path, including their versions and descriptions.
*
* @param loader Reference to a ModuleLoader object that provides access to modules
*
* @return 0 on success, 1 on failure
*
* The function performs the following operations:
* 1. Gets the configured module path from the loader
* 2. Retrieves a list of all potential modules in that path
* 3. Validates each module by checking for required symbols
* 4. Collects version and description information from valid modules
* 5. Formats and displays the information in a tabular format
*
* If no modules are found or if no valid modules are found, appropriate
* messages are displayed.
*
* Modules are considered valid if they expose all required interface
* symbols as defined in module_interface.hpp.
*/
int main_list_modules(const ModuleLoader& loader)
{
// initialize an empty modules list
std::vector<std::string> modules;
// initialize an empty path
std::string path;
DPMError get_path_error = loader.get_module_path(path);
if (get_path_error != DPMError::SUCCESS) {
// 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;
return 1;
}
DPMError list_error = loader.list_available_modules(modules);
if (list_error != DPMError::SUCCESS) {
DPMErrorCategory list_error = loader.list_available_modules(modules);
if (list_error != DPMErrorCategory::SUCCESS) {
std::cerr << "No modules found in: " << path << std::endl;
return 1;
}
@@ -61,17 +107,18 @@ int main_list_modules(const ModuleLoader& loader)
return 0;
}
// First pass: Identify valid modules
std::vector<std::string> valid_modules;
for (int i = 0; i < modules.size(); i++) {
void* handle;
DPMError load_error = loader.load_module(modules[i], handle);
if (load_error != DPMError::SUCCESS) {
DPMErrorCategory load_error = loader.load_module(modules[i], handle);
if (load_error != DPMErrorCategory::SUCCESS) {
continue;
}
std::vector<std::string> missing_symbols;
DPMError validate_error = loader.validate_module_interface(handle, missing_symbols);
if (validate_error == DPMError::SUCCESS) {
DPMErrorCategory validate_error = loader.validate_module_interface(handle, missing_symbols);
if (validate_error == DPMErrorCategory::SUCCESS) {
valid_modules.push_back(modules[i]);
}
dlclose(handle);
@@ -82,130 +129,53 @@ int main_list_modules(const ModuleLoader& loader)
return 0;
}
// Second pass: Collect module information and calculate column widths
size_t max_name_length = 0;
size_t max_version_length = 0;
std::vector<std::string> versions(valid_modules.size(), "unknown");
std::vector<std::string> descriptions(valid_modules.size(), "unknown");
for (int i = 0; i < valid_modules.size(); i++) {
void* module_handle;
std::string version;
max_name_length = std::max(max_name_length, valid_modules[i].length());
DPMError load_error = loader.load_module(valid_modules[i], module_handle);
if (load_error == DPMError::SUCCESS) {
DPMError version_error = loader.get_module_version(module_handle, version);
if (version_error == DPMError::SUCCESS) {
DPMErrorCategory load_error = loader.load_module(valid_modules[i], module_handle);
if (load_error == DPMErrorCategory::SUCCESS) {
// Get version
std::string version = "unknown";
DPMErrorCategory version_error = loader.get_module_version(module_handle, version);
if (version_error == DPMErrorCategory::SUCCESS) {
versions[i] = version;
max_version_length = std::max(max_version_length, version.length());
}
// Get description
std::string description = "unknown";
DPMErrorCategory desc_error = loader.get_module_description(module_handle, description);
if (desc_error == DPMErrorCategory::SUCCESS) {
descriptions[i] = description;
}
dlclose(module_handle);
}
}
const int column_spacing = 4;
// Display the table header
std::cout << "\nAvailable modules in '" << 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;
// Display the table rows
for (int i = 0; i < valid_modules.size(); i++) {
void* module_handle;
std::string version = "unknown";
std::string description = "unknown";
DPMError load_error = loader.load_module(valid_modules[i], module_handle);
if (load_error == DPMError::SUCCESS) {
DPMError version_error = loader.get_module_version(module_handle, version);
DPMError desc_error = loader.get_module_description(module_handle, description);
dlclose(module_handle);
}
std::cout << std::left << std::setw(max_name_length + column_spacing) << valid_modules[i]
<< std::setw(max_version_length + column_spacing) << version
<< description << std::endl;
<< std::setw(max_version_length + column_spacing) << versions[i]
<< descriptions[i] << std::endl;
}
return 0;
}
CommandArgs parse_args(int argc, char* argv[])
{
CommandArgs args;
args.module_path = "/usr/lib/dpm/modules/"; // Set to same default as ModuleLoader
static struct option long_options[] = {
{"module-path", required_argument, 0, 'm'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, "m:h", long_options, &option_index)) != -1) {
switch (opt) {
case 'm':
args.module_path = optarg;
break;
case 'h':
std::cout << "Usage: dpm [options] [module-name] [module args...]\n\n"
<< "Options:\n\n"
<< " -m, --module-path PATH Path to DPM modules\n"
<< " -h, --help Show this help message\n\n"
<< "If no module is specified, available modules will be listed.\n\n";
exit(0);
case '?':
exit(1);
}
}
if (optind < argc) {
args.module_name = argv[optind++];
for (int i = optind; i < argc; i++) {
if (!args.command.empty()) {
args.command += " ";
}
std::string arg = argv[i];
if (arg.find(' ') != std::string::npos) {
args.command += "\"" + arg + "\"";
} else {
args.command += arg;
}
}
}
return args;
}
int print_error(DPMError error, const std::string& module_name, const std::string& module_path) {
switch (error) {
case DPMError::SUCCESS:
return 0;
case DPMError::PATH_NOT_FOUND:
std::cerr << "Module path not found: " << module_path << std::endl;
return 1;
case DPMError::PATH_NOT_DIRECTORY:
std::cerr << "Module path is not a directory: " << module_path << std::endl;
return 1;
case DPMError::PERMISSION_DENIED:
std::cerr << "Permission denied accessing module: " << module_name << std::endl;
return 1;
case DPMError::MODULE_NOT_FOUND:
std::cerr << "Module not found: " << module_name << std::endl;
return 1;
case DPMError::MODULE_NOT_LOADED:
std::cerr << "Attempted to execute module before loading it: " << module_name << std::endl;
return 1;
case DPMError::MODULE_LOAD_FAILED:
std::cerr << "Failed to load module: " << module_name << std::endl;
return 1;
case DPMError::INVALID_MODULE:
std::cerr << "Invalid module format: " << module_name << std::endl;
return 1;
case DPMError::UNDEFINED_ERROR:
std::cerr << "Undefined error occurred with module: " << module_name << std::endl;
return 1;
default:
std::cerr << "Unknown error executing module: " << module_name << std::endl;
return 1;
}
}