diff --git a/include/dpm_interface.hpp b/include/dpm_interface.hpp index 2625707..1ac5616 100644 --- a/include/dpm_interface.hpp +++ b/include/dpm_interface.hpp @@ -86,4 +86,20 @@ int main_list_modules(const ModuleLoader& loader); */ int main_show_help(); +/** + * @brief Executes a DPM module with the specified command + * + * Attempts to execute the specified module with the given command string and + * handles any errors that occur during execution. If the execution is successful, + * returns 0. If an error occurs, constructs an appropriate error context and + * processes it through the error handling system. + * + * @param loader Reference to a ModuleLoader object that provides access to modules + * @param module_name Name of the module to execute + * @param command Command string to pass to the module + * @return 0 on successful execution, appropriate error code otherwise + */ +int main_execute_module( const ModuleLoader& loader, std::string module_name, std::string command ); + + /** @} */ // end of dpm_interface group \ No newline at end of file diff --git a/src/dpm.cpp b/src/dpm.cpp index 0a1c26d..98403fc 100644 --- a/src/dpm.cpp +++ b/src/dpm.cpp @@ -149,16 +149,5 @@ int main( int argc, char* argv[] ) } // execute the module - DPMErrorCategory execute_error = loader.execute_module(args.module_name, args.command); - - std::string absolute_modules_path; - loader.get_module_path(absolute_modules_path); - - // construct an error object - FlexDPMError result = make_error(execute_error); - result.module_name = args.module_name.c_str(); - result.module_path = absolute_modules_path.c_str(); - - // pair result with a message and exit with the appropriate error code - return handle_error(result); + int return_code = main_execute_module( loader, args.module_name, args.command ); } \ No newline at end of file diff --git a/src/dpm_interface.cpp b/src/dpm_interface.cpp index 87964c5..92c5563 100644 --- a/src/dpm_interface.cpp +++ b/src/dpm_interface.cpp @@ -30,6 +30,8 @@ #include "dpm_interface.hpp" +#include + /* * DPM Interface methods. * @@ -185,4 +187,23 @@ int main_show_help() { << " -h, --help Show this help message\n\n" << "For module-specific help, use: dpm help\n\n"; return 0; +} + +int main_execute_module( const ModuleLoader& loader, std::string module_name, std::string command ) { + DPMErrorCategory execute_error = loader.execute_module(module_name, command); + if (execute_error != DPMErrorCategory::SUCCESS) { + // get the absolute module path + std::string absolute_module_path = ""; + loader.get_module_path(absolute_module_path); + + // construct an error object + FlexDPMError result = make_error( execute_error ); + result.module_name = module_name.c_str(); + result.module_path = absolute_module_path.c_str(); + + // pair result with a message and return with the appropriate error code + return handle_error(result); + } else { + return 0; + } } \ No newline at end of file