using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace RedisManager
{
///
/// Represents configuration for a single Redis instance, including connection details.
///
public class InstanceConfig
{
///
/// Custom Redis configuration parameters to be applied at startup.
///
public Dictionary CustomConfig { get; set; } = new Dictionary();
///
/// Optional: Path to the redis/valkey server binary for this instance.
/// If not set, the global default will be used.
///
public string ServerBinaryPath { get; set; }
///
/// The name of the Redis instance.
///
public string Name { get; set; }
///
/// The host address of the Redis instance.
///
public string Host { get; set; }
///
/// The port number for the Redis instance (default 6379).
///
public int Port { get; set; } = 6379;
///
/// The password for authenticating with the Redis instance.
///
public string Password { get; set; }
}
///
/// Represents the configuration containing all Redis instances.
///
public class Config
{
///
/// Optional: Default path to the redis/valkey server binary for all instances.
///
public string ServerBinaryPath { get; set; }
///
/// The list of Redis instance configurations.
///
public List Instances { get; set; } = new List();
}
///
/// Provides methods to load and save RedisManager configuration from disk.
///
public static class ConfigManager
{
public static string ConfigPath = "redismanager.json";
///
/// Loads the configuration from disk, or creates a default if not present.
///
/// The loaded or default Config object.
public static Config LoadConfig(string path)
{
if (!File.Exists(path))
{
var defaultConfig = new Config
{
Instances = new List
{
new InstanceConfig
{
Name = "default",
Host = "localhost",
Port = 6379
}
}
};
SaveConfig(defaultConfig, path);
return defaultConfig;
}
var json = File.ReadAllText(path);
return JsonSerializer.Deserialize(json) ?? new Config();
}
public static Config LoadConfig()
{
return LoadConfig(ConfigPath);
}
///
/// Saves the provided configuration to disk as JSON.
///
/// The configuration to save.
public static void SaveConfig(Config config, string path)
{
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(path, json);
}
public static void SaveConfig(Config config)
{
SaveConfig(config, ConfigPath);
}
}
}