From 1b5b701d77e350d6f9a29a4fb133a63bbc2a2106 Mon Sep 17 00:00:00 2001 From: Coal Date: Fri, 18 Feb 2022 21:47:03 -0600 Subject: [PATCH] Setting up command handling --- dev/samplepackage/hamming_demo.lua | 2 +- dev/samplerepository/repository.toml | 2 +- src/argparser.h | 1 + src/command.cpp | 23 +++++++++++++++++++++++ src/command.h | 25 +++++++++++++++++++++++++ src/errorhandling.h | 15 +++++++++++++++ src/handlers/handlers.h | 8 ++++++++ src/handlers/install.cpp | 6 ++++++ src/handlers/install.h | 7 +++++++ src/library | 2 +- 10 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/command.cpp create mode 100644 src/command.h create mode 100644 src/errorhandling.h create mode 100644 src/handlers/handlers.h create mode 100644 src/handlers/install.cpp create mode 100644 src/handlers/install.h diff --git a/dev/samplepackage/hamming_demo.lua b/dev/samplepackage/hamming_demo.lua index b4eadaf..cc65795 100644 --- a/dev/samplepackage/hamming_demo.lua +++ b/dev/samplepackage/hamming_demo.lua @@ -1,7 +1,7 @@ -- This is useful: import = require("lpm").import --- Optionally, if your module returns a table, you can do it as follows: +-- Optionally, if your module returns an object, you can do it as follows: -- local hamming = import "hamming:latest" import "hamming:latest" diff --git a/dev/samplerepository/repository.toml b/dev/samplerepository/repository.toml index 73b09f9..284ecb9 100644 --- a/dev/samplerepository/repository.toml +++ b/dev/samplerepository/repository.toml @@ -10,4 +10,4 @@ package_type = "zip" [packages.wolf] summary = "A Lua library for creating static websites" package_type = "git" -"1.0" = { source = "git://github.com/coalio/wolf" } \ No newline at end of file +"1.0" = { source = "git://github.com/coalio/wolf.git" } \ No newline at end of file diff --git a/src/argparser.h b/src/argparser.h index 6053c57..cbfd09e 100644 --- a/src/argparser.h +++ b/src/argparser.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/src/command.cpp b/src/command.cpp new file mode 100644 index 0000000..b67cd5a --- /dev/null +++ b/src/command.cpp @@ -0,0 +1,23 @@ +#include +#include "lpm/types.h" +#include "lpm/macros.h" +#include "command.h" +#include "errorhandling.h" + +bool Command::execute(Type command_type, args_t& args) { + try { + switch (command_type) { + case Type::INSTALL: + Handlers::install(args); + break; + default: + LPM_PRINT_ERROR("Unsupported command: " << command_type); + return false; + } + } catch (...) { + LPM_PRINT_ERROR("Failed to execute command: " << command_type); + LPM_PRINT_DEBUG("Exception: " << ErrorHandling::what()); + } + + return true; +} \ No newline at end of file diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..02ae343 --- /dev/null +++ b/src/command.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include "lpm/types.h" +#include "lpm/macros.h" + +namespace Command { + enum Type { + HELP, + INIT, + INSTALL, + ADD, + RUN, + REMOVE, + UPDATE, + LIST, + SEARCH, + REPOSITORY, + AUDIT, + SHOW, + PURGE + }; + + // Pass a command to its respective handler. + bool execute(Command::Type command_type, args_t& args); +} \ No newline at end of file diff --git a/src/errorhandling.h b/src/errorhandling.h new file mode 100644 index 0000000..208b288 --- /dev/null +++ b/src/errorhandling.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include + +namespace ErrorHandling { + std::string what(const std::exception_ptr &eptr = std::current_exception()) { + if (!eptr) { throw std::bad_exception(); } + + try { std::rethrow_exception(eptr); } + catch (const std::exception &e) { return e.what() ; } + catch (const std::string &e) { return e ; } + catch (const char *e) { return e ; } + catch (...) { return "Unknown error"; } + } +} diff --git a/src/handlers/handlers.h b/src/handlers/handlers.h new file mode 100644 index 0000000..7e38374 --- /dev/null +++ b/src/handlers/handlers.h @@ -0,0 +1,8 @@ +// Its convenient to have all commands in separate compilation units +// to improve compilation speed. + +#pragma once + +#include "install.h" + +namespace Handlers {} \ No newline at end of file diff --git a/src/handlers/install.cpp b/src/handlers/install.cpp new file mode 100644 index 0000000..f9a7719 --- /dev/null +++ b/src/handlers/install.cpp @@ -0,0 +1,6 @@ +#include "install.h" +#include "lpm/macros.h" + +void Handlers::install(args_t& args) { + LPM_PRINT("Handlers::install()"); +} \ No newline at end of file diff --git a/src/handlers/install.h b/src/handlers/install.h new file mode 100644 index 0000000..0bce7f1 --- /dev/null +++ b/src/handlers/install.h @@ -0,0 +1,7 @@ +#pragma once +#include "lpm/types.h" + +namespace Handlers { + // Handler for lpm install. + void install(args_t& args); +} \ No newline at end of file diff --git a/src/library b/src/library index d373c20..e4ffd60 160000 --- a/src/library +++ b/src/library @@ -1 +1 @@ -Subproject commit d373c202985255b9a6108befc662a5b32834986c +Subproject commit e4ffd60d301106fad435c4413b09bd1769f30919