-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
370 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "command.hpp" | ||
|
||
// yess::cmd::Command::Command(const Action_handler &handler) : | ||
// handler_(handler) | ||
yess::cmd::Command::Command() {} | ||
yess::cmd::Command::~Command() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef YESS_COMMAND_HPP | ||
#define YESS_COMMAND_HPP | ||
|
||
#include "command_result.hpp" | ||
namespace yess::cmd | ||
{ | ||
class Command | ||
{ | ||
public: | ||
Command(); | ||
virtual ~Command(); | ||
virtual Command_result execute() = 0; | ||
}; | ||
} // namespace yess::cmd | ||
|
||
#endif // YESS_COMMAND_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "command_result.hpp" | ||
namespace yess::cmd | ||
{ | ||
Command_result::Command_result(Status status, | ||
const std::string &msg, | ||
std::any data) | ||
: status_(status), msg_(msg), data_(data) | ||
{ | ||
} | ||
std::string Command_result::message() { return msg_; } | ||
Command_result::Status Command_result::status() { return status_; } | ||
std::any Command_result::data() { return data_; } | ||
bool Command_result::ok() { return status_ == Status::ok; } | ||
} // namespace yess::cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef YESS_COMMAND_RESULT_HPP | ||
#define YESS_COMMAND_RESULT_HPP | ||
|
||
#include <any> | ||
#include <string> | ||
|
||
namespace yess::cmd | ||
{ | ||
class Command_result | ||
{ | ||
public: | ||
enum class Status { ok, error, exit }; | ||
Command_result(Status status, const std::string &msg, std::any data); | ||
std::string message(); | ||
Status status(); | ||
std::any data(); | ||
bool ok(); | ||
|
||
private: | ||
Status status_; | ||
std::string msg_; | ||
std::any data_; | ||
}; | ||
} // namespace yess::cmd | ||
#endif // YESS_COMMAND_RESULT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "domain_commands.hpp" | ||
|
||
using namespace yess::cmd; | ||
|
||
Create_stream_req::Create_stream_req(const std::string &type) : type_(type) {} | ||
|
||
Create_stream::Create_stream(const Action_handler &handler, | ||
const Create_stream_req &request) | ||
: Domain_command(handler), request_(request) | ||
{ | ||
} | ||
|
||
Command_result Create_stream::execute() | ||
{ | ||
try { | ||
handler_.create_stream(request_.type_); | ||
} catch (std::exception /* ex */) { | ||
std::string msg = "Unexpected error: cannot create stream"; | ||
return Command_result(Command_result::Status::error, msg, nullptr); | ||
} | ||
return Command_result(Command_result::Status::ok, "OK", nullptr); | ||
} | ||
|
||
Domain_command::Domain_command(const yess::Action_handler &handler) | ||
: handler_(handler) | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef YESS_DOMAIN_COMMANDS_HPP | ||
#define YESS_DOMAIN_COMMANDS_HPP | ||
|
||
#include "command.hpp" | ||
#include "../action_handler.hpp" | ||
#include <string> | ||
|
||
namespace yess::cmd | ||
{ | ||
class Domain_command : public Command | ||
{ | ||
public: | ||
explicit Domain_command(const Action_handler &handler); | ||
protected: | ||
const Action_handler &handler_; | ||
|
||
|
||
}; | ||
struct Create_stream_req | ||
{ | ||
explicit Create_stream_req(const std::string& type); | ||
std::string type_; | ||
}; | ||
|
||
class Create_stream : public Domain_command | ||
{ | ||
const Create_stream_req &request_; | ||
|
||
public: | ||
Create_stream(const Action_handler& handler, const Create_stream_req& req); | ||
Command_result execute() override; | ||
}; | ||
} // namespace yess::cmd | ||
|
||
#endif // YESS_DOMAIN_COMMANDS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "shell.hpp" | ||
// TODO: fix path | ||
#include "../../build/debug/include/version.h" | ||
#include "domain_commands.hpp" | ||
#include "shell_commands.hpp" | ||
#include <sstream> | ||
|
||
using namespace yess; | ||
|
||
std::unique_ptr<cmd::Command> cmd::Shell::interpret(std::string in) | ||
{ | ||
std::tuple<cmd::Shell::Shell_cmd, std::vector<std::string>> cmd_argv = | ||
tokens(in); | ||
auto c = std::get<0>(cmd_argv); | ||
auto argv = std::get<1>(cmd_argv); | ||
switch (c) { | ||
case Shell_cmd::unknown: { | ||
return std::make_unique<cmd::Unknown>(); | ||
} | ||
case Shell_cmd::quit: { | ||
return std::make_unique<cmd::Quit>(); | ||
} | ||
case Shell_cmd::help: { | ||
return std::make_unique<cmd::Help>(); | ||
} | ||
case Shell_cmd::create_stream: { | ||
if (argv.size() != 1) { | ||
// TODO: provide usage hints | ||
return std::make_unique<cmd::Unknown>(); | ||
} | ||
auto req = new cmd::Create_stream_req(argv[0]); | ||
return std::make_unique<cmd::Create_stream>(handler_, *req); | ||
} | ||
} | ||
} | ||
cmd::Shell::Shell(const Action_handler &handler) : handler_(handler) {} | ||
void cmd::Shell::execute(const cmd::Command &cmd) {} | ||
void cmd::Shell::run() | ||
{ | ||
std::cout << "yess interactive shell ("; | ||
std::cout << YESS_VER << ")" << std::endl; | ||
std::cout << std::endl; | ||
std::cout << "Enter 'help' for usage hints." << std::endl; | ||
bool should_exit = false; | ||
while (!should_exit) { | ||
std::cout << prompt_; | ||
std::string line; | ||
std::getline(std::cin, line); | ||
std::unique_ptr<cmd::Command> cmd = interpret(line); | ||
cmd::Command_result result = cmd->execute(); | ||
std::cout << result.message() << std::endl; | ||
if (result.status() == Command_result::Status::exit) | ||
should_exit = true; | ||
} | ||
} | ||
std::vector<std::string> to_words(std::string in) | ||
{ | ||
std::vector<std::string> result; | ||
std::istringstream ss(in); | ||
do { | ||
std::string word; | ||
ss >> word; | ||
if (!word.empty()) | ||
result.push_back(word); | ||
} while (ss); | ||
return result; | ||
} | ||
|
||
std::tuple<cmd::Shell::Shell_cmd, std::vector<std::string>> | ||
cmd::Shell::tokens(std::string in) | ||
{ | ||
std::vector<std::string> argv = to_words(in); | ||
std::string first = argv[0]; | ||
argv.erase(argv.begin()); | ||
|
||
auto c = cmd::Shell::Shell_cmd::unknown; | ||
if (first == "help") | ||
c = cmd::Shell::Shell_cmd::help; | ||
if (first == "create_stream") | ||
c = cmd::Shell::Shell_cmd::create_stream; | ||
if (first == "quit") | ||
c = cmd::Shell::Shell_cmd::quit; | ||
return std::tuple<Shell_cmd, std::vector<std::string>>(c, argv); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef YESS_SHELL_HPP | ||
#define YESS_SHELL_HPP | ||
|
||
#include "../action_handler.hpp" | ||
#include "command.hpp" | ||
|
||
namespace yess::cmd | ||
{ | ||
class Shell | ||
{ | ||
public: | ||
enum class Shell_cmd { | ||
unknown, | ||
help, | ||
create_stream, | ||
quit, | ||
}; | ||
Shell(const Action_handler &handler); | ||
void run(); | ||
|
||
private: | ||
std::tuple<Shell_cmd, std::vector<std::string>> tokens(std::string in); | ||
const Action_handler &handler_; | ||
std::unique_ptr<Command> interpret(std::string in); | ||
void execute(const Command &cmd); | ||
std::string prompt_ = "> "; | ||
}; | ||
} // namespace yess::cmd | ||
|
||
#endif // YESS_SHELL_HPP |
Oops, something went wrong.