Skip to content

Commit

Permalink
Setting up command handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Coal committed Feb 19, 2022
1 parent 3563463 commit 1b5b701
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dev/samplepackage/hamming_demo.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion dev/samplerepository/repository.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
"1.0" = { source = "git://github.com/coalio/wolf.git" }
1 change: 1 addition & 0 deletions src/argparser.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <iostream>
#include <string>
#include <map>
Expand Down
23 changes: 23 additions & 0 deletions src/command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <string>
#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;
}
25 changes: 25 additions & 0 deletions src/command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <string>
#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);
}
15 changes: 15 additions & 0 deletions src/errorhandling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <string>
#include <exception>

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"; }
}
}
8 changes: 8 additions & 0 deletions src/handlers/handlers.h
Original file line number Diff line number Diff line change
@@ -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 {}
6 changes: 6 additions & 0 deletions src/handlers/install.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "install.h"
#include "lpm/macros.h"

void Handlers::install(args_t& args) {
LPM_PRINT("Handlers::install()");
}
7 changes: 7 additions & 0 deletions src/handlers/install.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include "lpm/types.h"

namespace Handlers {
// Handler for lpm install.
void install(args_t& args);
}
2 changes: 1 addition & 1 deletion src/library

0 comments on commit 1b5b701

Please sign in to comment.