using CommandLine; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using RedisManager.Utils; namespace RedisManager.Commands { [Verb("module-load", HelpText = "Load a Redis module.")] public class ModuleLoadOptions { [Option('i', "instance", Required = true, HelpText = "Instance name.")] public string Instance { get; set; } [Value(0, MetaName = "path", Required = true, HelpText = "Module path.")] public string Path { get; set; } [Value(1, MetaName = "args", HelpText = "Module arguments.")] public IEnumerable Args { get; set; } } [Verb("module-unload", HelpText = "Unload a Redis module.")] public class ModuleUnloadOptions { [Option('i', "instance", Required = true, HelpText = "Instance name.")] public string Instance { get; set; } [Value(0, MetaName = "name", Required = true, HelpText = "Module name.")] public string Name { get; set; } } [Verb("module-list", HelpText = "List loaded Redis modules.")] public class ModuleListOptions { [Option('i', "instance", Required = true, HelpText = "Instance name.")] public string Instance { get; set; } } public static class ModuleCommands { public static int RunModuleLoad(ModuleLoadOptions opts, Config config) { var instance = RedisUtils.GetInstance(config, opts.Instance); using var redis = RedisUtils.ConnectRedis(instance); var db = redis.GetDatabase(); try { var args = new List { "LOAD", opts.Path }; if (opts.Args != null) args.AddRange(opts.Args); var result = db.Execute("MODULE", args.ToArray()); Console.WriteLine(Output.Green($"Module loaded: {result}")); return 0; } catch (Exception ex) { Console.WriteLine(Output.Red($"Failed to load module: {ex.Message}")); return 1; } } public static int RunModuleUnload(ModuleUnloadOptions opts, Config config) { var instance = RedisUtils.GetInstance(config, opts.Instance); using var redis = RedisUtils.ConnectRedis(instance); var db = redis.GetDatabase(); try { var result = db.Execute("MODULE", new object[] { "UNLOAD", opts.Name }); Console.WriteLine(Output.Green($"Module unloaded: {result}")); return 0; } catch (Exception ex) { Console.WriteLine(Output.Red($"Failed to unload module: {ex.Message}")); return 1; } } public static int RunModuleList(ModuleListOptions opts, Config config) { var instance = RedisUtils.GetInstance(config, opts.Instance); using var redis = RedisUtils.ConnectRedis(instance); var db = redis.GetDatabase(); try { var result = db.Execute("MODULE", new object[] { "LIST" }); Console.WriteLine(Output.Green($"Module list: {result}")); return 0; } catch (Exception ex) { Console.WriteLine(Output.Red($"Failed to list modules: {ex.Message}")); return 1; } } } }