string handleSingleOperation()

in sonic-db-cli/sonic-db-cli.cpp [40:87]


string handleSingleOperation(
    const string& netns,
    const string& db_name,
    const string& operation,
    bool useUnixSocket)
{
    shared_ptr<DBConnector> client;
    auto host = SonicDBConfig::getDbHostname(db_name, netns);
    string message = "Could not connect to Redis at " + host + ":";
    try
    {
        auto db_id =  SonicDBConfig::getDbId(db_name, netns);
        if (useUnixSocket && host != "redis_chassis.server")
        {
            auto db_socket = SonicDBConfig::getDbSock(db_name, netns);
            message += db_name + ": Connection refused";
            client = make_shared<DBConnector>(db_id, db_socket, 0);
        }
        else
        {
            auto port = SonicDBConfig::getDbPort(db_name, netns);
            message += port + ": Connection refused";
            client = make_shared<DBConnector>(db_id, host, port, 0);
        }
    }
    catch (const exception& e)
    {
        return message;
    }

    if (operation == "PING"
        || operation == "SAVE"
        || operation == "FLUSHALL")
    {
        RedisReply reply(client.get(), operation);
        auto response = reply.getContext();
        if (nullptr != response)
        {
            return string();
        }
    }
    else
    {
        throw std::invalid_argument("Operation " + operation +" is not supported");
    }

    return message;
}