IXWebSocket/ws/ws_redis_cli.cpp

85 lines
2.1 KiB
C++
Raw Normal View History

2020-06-11 22:51:10 +02:00
/*
* ws_redis_cli.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
2020-07-08 19:39:46 +02:00
#include "linenoise.hpp"
#include <iostream>
2020-06-11 22:51:10 +02:00
#include <ixredis/IXRedisClient.h>
#include <spdlog/spdlog.h>
#include <sstream>
namespace ix
{
2020-07-08 19:39:46 +02:00
int ws_redis_cli_main(const std::string& hostname, int port, const std::string& password)
2020-06-11 22:51:10 +02:00
{
RedisClient redisClient;
if (!redisClient.connect(hostname, port))
{
spdlog::info("Cannot connect to redis host");
return 1;
}
if (!password.empty())
{
std::string authResponse;
if (!redisClient.auth(password, authResponse))
{
std::stringstream ss;
spdlog::info("Cannot authenticated to redis");
return 1;
}
spdlog::info("Auth response: {}", authResponse);
}
while (true)
{
// 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);
2020-06-11 22:51:10 +02:00
if (quit)
2020-06-11 22:51:10 +02:00
{
break;
}
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)
{
2020-07-08 19:39:46 +02:00
std::cout << "(" << redisClient.getRespTypeDescription(response.first) << ")"
<< " ";
}
std::cout << response.second << std::endl;
2020-06-11 22:51:10 +02:00
}
linenoise::AddHistory(line.c_str());
2020-06-11 22:51:10 +02:00
}
return 0;
}
} // namespace ix