add project

This commit is contained in:
GuilhermeStrice
2025-07-09 19:31:34 +01:00
parent 8d2e88edf4
commit f37078157d
44 changed files with 7680 additions and 0 deletions

87
Program.cs Normal file
View File

@ -0,0 +1,87 @@
using CommandLine;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using RedisManager.Commands;
using Crayon;
namespace RedisManager
{
/// <summary>
/// Main entry point for the RedisManager application.
/// Supports service (daemon) mode and client mode only.
/// </summary>
class Program
{
/// <summary>
/// Main entry point that determines the operational mode based on command line arguments.
/// </summary>
/// <param name="args">Command line arguments</param>
/// <returns>Exit code (0 for success, non-zero for errors)</returns>
static async Task<int> Main(string[] args)
{
// No arguments: run as a daemon (service mode)
// Service mode: allow optional --config <path>
if (args.Length == 0 || (args.Length >= 2 && args[0] == "--config"))
{
string configPath = null;
if (args.Length >= 2 && args[0] == "--config")
{
configPath = args[1];
// Remove --config and path from args for service mode
args = args.Skip(2).ToArray();
}
return await RunServiceMode();
} // Standalone mode removed: all commands require daemon/service.
// With arguments: try to connect to the daemon (client mode)
var client = new RedisManagerClient();
var clientResult = await client.ExecuteCommandAsync(args);
// A specific exit code (2) from the client indicates the service is not running
if (clientResult == 2)
{
// Allow help output even if no daemon and no --instance
bool wantsHelp = args.Any(arg => arg == "help" || arg == "--help" || arg == "-h");
if (!wantsHelp)
{
Console.WriteLine(Output.Red("Error: RedisManager daemon/service is not running. Please start the service (no arguments) and retry your command."));
}
}
// Return the result from the client (0 for success, 1 for other errors)
return clientResult;
}
/// <summary>
/// Runs RedisManager in service mode, starting a TCP server that accepts client connections.
/// The service runs indefinitely until interrupted by Ctrl+C or other termination signals.
/// </summary>
/// <returns>Exit code (0 for successful shutdown)</returns>
static async Task<int> RunServiceMode()
{
Console.WriteLine("Starting RedisManager Service...");
var service = new RedisManagerService();
await service.StartAsync();
// Block the main thread until Ctrl+C is pressed
var cts = new System.Threading.CancellationTokenSource();
Console.CancelKeyPress += (sender, e) => {
e.Cancel = true;
cts.Cancel();
};
Console.WriteLine("RedisManager daemon is running. Press Ctrl+C to exit.");
try
{
await Task.Delay(-1, cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Shutting down RedisManager Service...");
}
await service.StopAsync();
return 0;
}
}
}