add project
This commit is contained in:
121
Commands/DatabaseCommands.cs
Normal file
121
Commands/DatabaseCommands.cs
Normal file
@ -0,0 +1,121 @@
|
||||
using CommandLine;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RedisManager.Utils;
|
||||
|
||||
using RedisManager.Commands;
|
||||
|
||||
namespace RedisManager.Commands
|
||||
{
|
||||
[Verb("select", HelpText = "Change the selected database.")]
|
||||
public class SelectOptions : InstanceOptions
|
||||
{
|
||||
[Value(0, MetaName = "database", Required = true, HelpText = "Database number.")]
|
||||
public int Database { get; set; }
|
||||
[Option("table", Required = false, HelpText = "Output as table.")]
|
||||
public bool Table { get; set; }
|
||||
}
|
||||
|
||||
[Verb("flushdb", HelpText = "Remove all keys from the current database.")]
|
||||
public class FlushDbOptions : InstanceOptions
|
||||
{
|
||||
[Option("yes", Required = false, HelpText = "Skip confirmation prompt.")]
|
||||
public bool Yes { get; set; }
|
||||
[Option("table", Required = false, HelpText = "Output as table.")]
|
||||
public bool Table { get; set; }
|
||||
}
|
||||
|
||||
[Verb("flushall", HelpText = "Remove all keys from all databases.")]
|
||||
public class FlushAllOptions : InstanceOptions
|
||||
{
|
||||
[Option("yes", Required = false, HelpText = "Skip confirmation prompt.")]
|
||||
public bool Yes { get; set; }
|
||||
[Option("table", Required = false, HelpText = "Output as table.")]
|
||||
public bool Table { get; set; }
|
||||
}
|
||||
|
||||
[Verb("dbsize", HelpText = "Return the number of keys in the current database.")]
|
||||
public class DbSizeOptions : InstanceOptions
|
||||
{
|
||||
[Option("table", Required = false, HelpText = "Output as table.")]
|
||||
public bool Table { get; set; }
|
||||
}
|
||||
|
||||
public static class DatabaseCommands
|
||||
{
|
||||
public static int RunSelect(SelectOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase(opts.Database);
|
||||
if (opts.Table)
|
||||
RedisUtils.PrintTable(new[] { "Database", "Status" }, new List<string[]> { new[] { opts.Database.ToString(), "Selected" } });
|
||||
else
|
||||
Console.WriteLine(Output.Green("OK"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int RunFlushDb(FlushDbOptions opts, Config config)
|
||||
{
|
||||
if (!opts.Yes)
|
||||
{
|
||||
Console.WriteLine(Output.Yellow("Are you sure you want to flush the current database? (y/N)"));
|
||||
var response = Console.ReadLine()?.ToLower();
|
||||
if (response != "y" && response != "yes")
|
||||
{
|
||||
Console.WriteLine(Output.Red("Operation cancelled."));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
db.Execute("FLUSHDB");
|
||||
if (opts.Table)
|
||||
RedisUtils.PrintTable(new[] { "Operation", "Status" }, new List<string[]> { new[] { "FLUSHDB", "OK" } });
|
||||
else
|
||||
Console.WriteLine(Output.Green("OK"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int RunFlushAll(FlushAllOptions opts, Config config)
|
||||
{
|
||||
if (!opts.Yes)
|
||||
{
|
||||
Console.WriteLine(Output.Yellow("Are you sure you want to flush ALL databases? (y/N)"));
|
||||
var response = Console.ReadLine()?.ToLower();
|
||||
if (response != "y" && response != "yes")
|
||||
{
|
||||
Console.WriteLine(Output.Red("Operation cancelled."));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
db.Execute("FLUSHALL");
|
||||
if (opts.Table)
|
||||
RedisUtils.PrintTable(new[] { "Operation", "Status" }, new List<string[]> { new[] { "FLUSHALL", "OK" } });
|
||||
else
|
||||
Console.WriteLine(Output.Green("OK"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int RunDbSize(DbSizeOptions opts, Config config)
|
||||
{
|
||||
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
||||
using var redis = RedisUtils.ConnectRedis(instance);
|
||||
var db = redis.GetDatabase();
|
||||
var size = db.Execute("DBSIZE");
|
||||
if (opts.Table)
|
||||
RedisUtils.PrintTable(new[] { "Database", "Size" }, new List<string[]> { new[] { "Current", size.ToString() } });
|
||||
else
|
||||
Console.WriteLine(Output.Green(size.ToString()));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user