From 5be17a0a058e71fe4c0bf4516416fa31c6b78f8e Mon Sep 17 00:00:00 2001 From: Chris Punches Date: Sun, 23 Feb 2025 04:20:52 -0500 Subject: [PATCH] cleaning up --- include/ModuleLoader.hpp | 1 + include/dpm_interface.hpp | 7 +++++-- include/error.hpp | 1 + src/dpm.cpp | 31 ------------------------------- src/dpm_interface.cpp | 30 +++++++++++++++++++++++++++++- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/include/ModuleLoader.hpp b/include/ModuleLoader.hpp index 15fef79..75ef68d 100644 --- a/include/ModuleLoader.hpp +++ b/include/ModuleLoader.hpp @@ -9,6 +9,7 @@ class ModuleLoader { public: + // initializer explicit ModuleLoader(std::string module_path = "/usr/lib/dpm/modules/"); DPMError list_available_modules(std::vector& modules) const; DPMError get_module_path(std::string& path) const; diff --git a/include/dpm_interface.hpp b/include/dpm_interface.hpp index c72f593..d4cec7b 100644 --- a/include/dpm_interface.hpp +++ b/include/dpm_interface.hpp @@ -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[]); \ No newline at end of file +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); \ No newline at end of file diff --git a/include/error.hpp b/include/error.hpp index 4beb439..3d1887b 100644 --- a/include/error.hpp +++ b/include/error.hpp @@ -1,5 +1,6 @@ #pragma once +// global errors for the core DPM routing/execution component enum class DPMError { SUCCESS, PATH_NOT_FOUND, diff --git a/src/dpm.cpp b/src/dpm.cpp index 1a357c6..e112fec 100644 --- a/src/dpm.cpp +++ b/src/dpm.cpp @@ -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 diff --git a/src/dpm_interface.cpp b/src/dpm_interface.cpp index 2e64abf..66f7aa1 100644 --- a/src/dpm_interface.cpp +++ b/src/dpm_interface.cpp @@ -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'}, @@ -174,4 +174,32 @@ 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; + } } \ No newline at end of file