add project
This commit is contained in:
160
Commands/ConnectionCommands.cs
Normal file
160
Commands/ConnectionCommands.cs
Normal file
@ -0,0 +1,160 @@
|
||||
using CommandLine;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RedisManager.Utils;
|
||||
|
||||
namespace RedisManager.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains command line options and implementations for Redis connection management operations.
|
||||
/// Provides functionality for AUTH, QUIT, CLIENT LIST, CLIENT KILL, CLIENT GETNAME, and CLIENT SETNAME commands.
|
||||
/// </summary>
|
||||
[Verb("auth", HelpText = "Authenticate to the server.")]
|
||||
public class AuthOptions
|
||||
{
|
||||
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
||||
public string Instance { get; set; }
|
||||
[Value(0, MetaName = "password", Required = true, HelpText = "Password.")]
|
||||
public string Password { get; set; }
|
||||
}
|
||||
|
||||
[Verb("quit", HelpText = "Close the connection.")]
|
||||
public class QuitOptions
|
||||
{
|
||||
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
||||
public string Instance { get; set; }
|
||||
}
|
||||
|
||||
[Verb("clientlist", HelpText = "Get the list of client connections.")]
|
||||
public class ClientListOptions
|
||||
{
|
||||
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
||||
public string Instance { get; set; }
|
||||
}
|
||||
|
||||
[Verb("clientkill", HelpText = "Kill a client connection.")]
|
||||
public class ClientKillOptions
|
||||
{
|
||||
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
||||
public string Instance { get; set; }
|
||||
[Value(0, MetaName = "client_id", Required = true, HelpText = "Client ID.")]
|
||||
public string ClientId { get; set; }
|
||||
}
|
||||
|
||||
[Verb("clientgetname", HelpText = "Get the current connection name.")]
|
||||
public class ClientGetNameOptions
|
||||
{
|
||||
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
||||
public string Instance { get; set; }
|
||||
}
|
||||
|
||||
[Verb("clientsetname", HelpText = "Set the current connection name.")]
|
||||
public class ClientSetNameOptions
|
||||
{
|
||||
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
||||
public string Instance { get; set; }
|
||||
[Value(0, MetaName = "name", Required = true, HelpText = "Connection name.")]
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public static class ConnectionCommands
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the AUTH command to authenticate to the server.
|
||||
/// </summary>
|
||||
/// <param name="opts">The AuthOptions containing instance and password.</param>
|
||||
/// <param name="config">The RedisManager configuration.</param>
|
||||
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
||||
public static int RunAuth(AuthOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
db.Execute("AUTH", opts.Password);
|
||||
Console.WriteLine(Output.Green("OK"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the QUIT command to close the connection.
|
||||
/// </summary>
|
||||
/// <param name="opts">The QuitOptions containing instance.</param>
|
||||
/// <param name="config">The RedisManager configuration.</param>
|
||||
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
||||
public static int RunQuit(QuitOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
db.Execute("QUIT");
|
||||
Console.WriteLine(Output.Green("OK"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the CLIENT LIST command to get the list of client connections.
|
||||
/// </summary>
|
||||
/// <param name="opts">The ClientListOptions containing instance.</param>
|
||||
/// <param name="config">The RedisManager configuration.</param>
|
||||
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
||||
public static int RunClientList(ClientListOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
var result = db.Execute("CLIENT", "LIST");
|
||||
Console.WriteLine(Output.Green(result.ToString()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the CLIENT KILL command to kill a client connection.
|
||||
/// </summary>
|
||||
/// <param name="opts">The ClientKillOptions containing instance and client ID.</param>
|
||||
/// <param name="config">The RedisManager configuration.</param>
|
||||
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
||||
public static int RunClientKill(ClientKillOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
db.Execute("CLIENT", "KILL", opts.ClientId);
|
||||
Console.WriteLine(Output.Green("OK"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the CLIENT GETNAME command to get the current connection name.
|
||||
/// </summary>
|
||||
/// <param name="opts">The ClientGetNameOptions containing instance.</param>
|
||||
/// <param name="config">The RedisManager configuration.</param>
|
||||
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
||||
public static int RunClientGetName(ClientGetNameOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
var result = db.Execute("CLIENT", "GETNAME");
|
||||
Console.WriteLine(Output.Green(result.ToString()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the CLIENT SETNAME command to set the current connection name.
|
||||
/// </summary>
|
||||
/// <param name="opts">The ClientSetNameOptions containing instance and name.</param>
|
||||
/// <param name="config">The RedisManager configuration.</param>
|
||||
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
||||
public static int RunClientSetName(ClientSetNameOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
db.Execute("CLIENT", "SETNAME", opts.Name);
|
||||
Console.WriteLine(Output.Green("OK"));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user