cleaning up

master
Chris Punches 2025-02-23 04:20:52 -05:00
parent 6a91f30ce2
commit 5be17a0a05
5 changed files with 36 additions and 34 deletions

View File

@ -9,6 +9,7 @@
class ModuleLoader {
public:
// initializer
explicit ModuleLoader(std::string module_path = "/usr/lib/dpm/modules/");
DPMError list_available_modules(std::vector<std::string>& modules) const;
DPMError get_module_path(std::string& path) const;

View File

@ -24,10 +24,13 @@ int main_list_modules(const ModuleLoader& loader);
// data structure for supplied arguments
struct CommandArgs {
std::string module_path = "/usr/lib/dpm/modules/";
std::string module_path;
std::string module_name;
std::string command; // All arguments combined into a single command string
};
// parser for populating data structure for supplied arguments
CommandArgs parse_args(int argc, char* argv[]);
// pairs DPMErrors to error messages, prints those error messages, and returns
int print_error(DPMError error, const std::string& module_name, const std::string& module_path);

View File

@ -1,5 +1,6 @@
#pragma once
// global errors for the core DPM routing/execution component
enum class DPMError {
SUCCESS,
PATH_NOT_FOUND,

View File

@ -11,35 +11,6 @@
* 3. Provide a module-agnostic unified interface for modules.
*/
// prints error message and returns error code
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_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;
default:
std::cerr << "Unknown error executing module: " << module_name << std::endl;
return 1;
}
}
// the default behaviour if dpm is executed without being told to do anything
int default_behavior(const ModuleLoader& loader)
{
@ -55,8 +26,6 @@ int main( int argc, char* argv[] )
auto args = parse_args( argc, argv );
// create a module loader object at the supplied or default path
// TODO: the default is set in the header instead of the
// implementation, fix that
ModuleLoader loader( args.module_path );
// check the module path for the loader object

View File

@ -126,10 +126,10 @@ int main_list_modules(const ModuleLoader& loader)
return 0;
}
// parser for populating data structure for supplied arguments
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'},
@ -175,3 +175,31 @@ CommandArgs parse_args(int argc, char* argv[])
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_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;
default:
std::cerr << "Unknown error executing module: " << module_name << std::endl;
return 1;
}
}