126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
using CommandLine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using RedisManager.Utils;
|
|
|
|
namespace RedisManager.Commands
|
|
{
|
|
[Verb("list-instances", HelpText = "List all configured Redis instances.")]
|
|
public class ListInstancesOptions { }
|
|
|
|
[Verb("add-instance", HelpText = "Add a new Redis instance.")]
|
|
public class AddInstanceOptions
|
|
{
|
|
[Option('n', "name", Required = true, HelpText = "Instance name.")]
|
|
public string Name { get; set; }
|
|
[Option('h', "host", Required = true, HelpText = "Redis host.")]
|
|
public string Host { get; set; }
|
|
[Option('p', "port", Required = false, HelpText = "Redis port.")]
|
|
public int Port { get; set; } = 6379;
|
|
[Option('w', "password", Required = false, HelpText = "Redis password.")]
|
|
public string Password { get; set; }
|
|
}
|
|
|
|
[Verb("update-instance", HelpText = "Update an existing Redis instance.")]
|
|
public class UpdateInstanceOptions
|
|
{
|
|
[Option('n', "name", Required = true, HelpText = "Instance name.")]
|
|
public string Name { get; set; }
|
|
[Option('h', "host", Required = false, HelpText = "Redis host.")]
|
|
public string Host { get; set; }
|
|
[Option('p', "port", Required = false, HelpText = "Redis port.")]
|
|
public int Port { get; set; } = 6379;
|
|
[Option('w', "password", Required = false, HelpText = "Redis password.")]
|
|
public string Password { get; set; }
|
|
}
|
|
|
|
[Verb("delete-instance", HelpText = "Delete a Redis instance.")]
|
|
public class DeleteInstanceOptions
|
|
{
|
|
[Option('n', "name", Required = true, HelpText = "Instance name.")]
|
|
public string Name { get; set; }
|
|
}
|
|
|
|
public static class InstanceCommands
|
|
{
|
|
public static int RunListInstances(Config config)
|
|
{
|
|
if (config.Instances.Count == 0)
|
|
{
|
|
Console.WriteLine(Output.Yellow("No instances configured."));
|
|
return 0;
|
|
}
|
|
|
|
var rows = config.Instances.Select(i => new[] { i.Name, i.Host, i.Port.ToString(), i.Password ?? "" }).ToList();
|
|
RedisUtils.PrintTable(new[] { "Name", "Host", "Port", "Password" }, rows);
|
|
return 0;
|
|
}
|
|
|
|
public static int RunAddInstance(AddInstanceOptions opts, Config config)
|
|
{
|
|
if (config.Instances.Any(i => i.Name == opts.Name))
|
|
{
|
|
Console.WriteLine(Output.Red($"Instance '{opts.Name}' already exists."));
|
|
return 1;
|
|
}
|
|
|
|
var instance = new InstanceConfig
|
|
{
|
|
Name = opts.Name,
|
|
Host = opts.Host,
|
|
Port = opts.Port,
|
|
Password = opts.Password
|
|
};
|
|
|
|
config.Instances.Add(instance);
|
|
SaveConfig(config, "redismanager.json");
|
|
Console.WriteLine(Output.Green($"Instance '{opts.Name}' added successfully."));
|
|
return 0;
|
|
}
|
|
|
|
public static int RunUpdateInstance(UpdateInstanceOptions opts, Config config)
|
|
{
|
|
var instance = config.Instances.Find(i => i.Name == opts.Name);
|
|
if (instance == null)
|
|
{
|
|
Console.WriteLine(Output.Red($"Instance '{opts.Name}' not found."));
|
|
return 1;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(opts.Host))
|
|
instance.Host = opts.Host;
|
|
if (opts.Port != 6379)
|
|
instance.Port = opts.Port;
|
|
if (!string.IsNullOrEmpty(opts.Password))
|
|
instance.Password = opts.Password;
|
|
|
|
SaveConfig(config, "redismanager.json");
|
|
Console.WriteLine(Output.Green($"Instance '{opts.Name}' updated successfully."));
|
|
return 0;
|
|
}
|
|
|
|
public static int RunDeleteInstance(DeleteInstanceOptions opts, Config config)
|
|
{
|
|
var instance = config.Instances.Find(i => i.Name == opts.Name);
|
|
if (instance == null)
|
|
{
|
|
Console.WriteLine(Output.Red($"Instance '{opts.Name}' not found."));
|
|
return 1;
|
|
}
|
|
|
|
config.Instances.Remove(instance);
|
|
SaveConfig(config, "redismanager.json");
|
|
Console.WriteLine(Output.Green($"Instance '{opts.Name}' deleted successfully."));
|
|
return 0;
|
|
}
|
|
|
|
private static void SaveConfig(Config config, string path)
|
|
{
|
|
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(path, json);
|
|
}
|
|
}
|
|
} |