(ws) add bare bone redis-cli like sub-command, with command line editing powered by libnoise
This commit is contained in:
@ -45,6 +45,7 @@ add_executable(ws
|
||||
ws_transfer.cpp
|
||||
ws_send.cpp
|
||||
ws_receive.cpp
|
||||
ws_redis_cli.cpp
|
||||
ws_redis_publish.cpp
|
||||
ws_redis_subscribe.cpp
|
||||
ws_redis_server.cpp
|
||||
|
10
ws/ws.cpp
10
ws/ws.cpp
@ -281,6 +281,12 @@ int main(int argc, char** argv)
|
||||
httpClientApp->add_option("--transfer-timeout", transferTimeout, "Transfer timeout");
|
||||
addTLSOptions(httpClientApp);
|
||||
|
||||
CLI::App* redisCliApp = app.add_subcommand("redis_cli", "Redis cli");
|
||||
redisCliApp->fallthrough();
|
||||
redisCliApp->add_option("--port", redisPort, "Port");
|
||||
redisCliApp->add_option("--host", hostname, "Hostname");
|
||||
redisCliApp->add_option("--password", password, "Password");
|
||||
|
||||
CLI::App* redisPublishApp = app.add_subcommand("redis_publish", "Redis publisher");
|
||||
redisPublishApp->fallthrough();
|
||||
redisPublishApp->add_option("--port", redisPort, "Port");
|
||||
@ -531,6 +537,10 @@ int main(int argc, char** argv)
|
||||
compress,
|
||||
tlsOptions);
|
||||
}
|
||||
else if (app.got_subcommand("redis_cli"))
|
||||
{
|
||||
ret = ix::ws_redis_cli_main(hostname, redisPort, password);
|
||||
}
|
||||
else if (app.got_subcommand("redis_publish"))
|
||||
{
|
||||
ret = ix::ws_redis_publish_main(hostname, redisPort, password, channel, message, count);
|
||||
|
4
ws/ws.h
4
ws/ws.h
@ -64,6 +64,10 @@ namespace ix
|
||||
bool disablePerMessageDeflate,
|
||||
const ix::SocketTLSOptions& tlsOptions);
|
||||
|
||||
int ws_redis_cli_main(const std::string& hostname,
|
||||
int port,
|
||||
const std::string& password);
|
||||
|
||||
int ws_redis_publish_main(const std::string& hostname,
|
||||
int port,
|
||||
const std::string& password,
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "linenoise.hpp"
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -36,17 +37,50 @@ namespace ix
|
||||
|
||||
while (true)
|
||||
{
|
||||
std::string text;
|
||||
std::cout << "> " << std::flush;
|
||||
std::getline(std::cin, text);
|
||||
// Read line
|
||||
std::string line;
|
||||
std::string prompt;
|
||||
prompt += hostname;
|
||||
prompt += ":";
|
||||
prompt += std::to_string(port);
|
||||
prompt += "> ";
|
||||
auto quit = linenoise::Readline(prompt.c_str(), line);
|
||||
|
||||
#if 0
|
||||
if (!redisClient.send(args, errMsg))
|
||||
if (quit)
|
||||
{
|
||||
spdlog::error("Error", channel, errMsg);
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
std::stringstream ss(line);
|
||||
std::vector<std::string> args;
|
||||
std::string arg;
|
||||
|
||||
while (ss.good())
|
||||
{
|
||||
ss >> arg;
|
||||
args.push_back(arg);
|
||||
}
|
||||
|
||||
std::string errMsg;
|
||||
auto response = redisClient.send(args, errMsg);
|
||||
if (!errMsg.empty())
|
||||
{
|
||||
spdlog::error("(error) {}", errMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (response.first != RespType::String)
|
||||
{
|
||||
std::cout << "("
|
||||
<< redisClient.getRespTypeDescription(response.first)
|
||||
<< ")"
|
||||
<< " ";
|
||||
}
|
||||
|
||||
std::cout << response.second << std::endl;
|
||||
}
|
||||
|
||||
linenoise::AddHistory(line.c_str());
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user