mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-04 00:56:19 +02:00
Reorganize project structure
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
namespace RemSox.Kernel.UI.CLI;
|
||||
|
||||
public static class CommandManager
|
||||
{
|
||||
private static readonly Dictionary<string, ICommand> commands = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private static readonly List<string> commandHistory = [];
|
||||
|
||||
public static void RegisterCommand(ICommand command)
|
||||
{
|
||||
commands[command.Name] = command;
|
||||
}
|
||||
|
||||
public static void RegisterCommands(IEnumerable<ICommand> commandsToRegister)
|
||||
{
|
||||
foreach (ICommand command in commandsToRegister)
|
||||
{
|
||||
RegisterCommand(command);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<ICommand> GetCommands()
|
||||
{
|
||||
return commands.Values.OrderBy(command => command.Name);
|
||||
}
|
||||
|
||||
public static IList<string> GetCommandHistory()
|
||||
{
|
||||
return commandHistory;
|
||||
}
|
||||
|
||||
public static async Task<bool> TryExecuteAsync(string input, Action<string> printLine, CancellationToken cancellationToken = default)
|
||||
{
|
||||
string trimmedInput = input.Trim();
|
||||
|
||||
if (trimmedInput.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (commandHistory.Count == 0 || commandHistory[^1] != trimmedInput)
|
||||
{
|
||||
commandHistory.Add(trimmedInput);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, ICommand> entry in commands.OrderByDescending(entry => entry.Key.Length))
|
||||
{
|
||||
string commandName = entry.Key;
|
||||
|
||||
if (!trimmedInput.Equals(commandName, StringComparison.OrdinalIgnoreCase) &&
|
||||
!trimmedInput.StartsWith(commandName + " ", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string? arguments = null;
|
||||
|
||||
if (trimmedInput.Length > commandName.Length)
|
||||
{
|
||||
arguments = trimmedInput[commandName.Length..].TrimStart();
|
||||
}
|
||||
|
||||
CancellationTokenRegistration unused = cancellationToken.Register(async () => await entry.Value.StopAsync());
|
||||
|
||||
await entry.Value.ExecuteAsync(arguments, printLine);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public sealed class ClearCommand : ICommand
|
||||
{
|
||||
public string Name => "clear";
|
||||
|
||||
public string Description => "Clear the screen";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
Console.Clear();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public sealed class ShutdownCommand : ICommand
|
||||
{
|
||||
public string Name => "shutdown";
|
||||
|
||||
public string Description => "Shutdown the system";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Shutting down system...");
|
||||
Sys.Power.Shutdown();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public sealed class HelpCommand : ICommand
|
||||
{
|
||||
public string Name => "help";
|
||||
|
||||
public string Description => "Show this help message";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Available commands:");
|
||||
|
||||
foreach (ICommand command in CommandManager.GetCommands())
|
||||
{
|
||||
printLine($" {command.Name,-12} - {command.Description}");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using RemSox.Kernel.Processes;
|
||||
using RemSox.Kernel.Processing;
|
||||
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public sealed class SpawnProcessCommand : ICommand
|
||||
{
|
||||
public string Name => "spawn";
|
||||
|
||||
public string Description => "Spawn a new process";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
arguments = arguments?.Trim();
|
||||
|
||||
int? processId = arguments switch
|
||||
{
|
||||
"test" => ProcessManager.SpawnProcess<TestProcess>(),
|
||||
"terminal" => ProcessManager.SpawnProcess<TerminalProcess>(),
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (processId is not null)
|
||||
{
|
||||
printLine($"Spawned process with ID {processId}");
|
||||
}
|
||||
else
|
||||
{
|
||||
printLine("Usage: spawn <process-name>");
|
||||
printLine("Available processes: test, terminal");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ListProcessesCommand : ICommand
|
||||
{
|
||||
public string Name => "ps";
|
||||
|
||||
public string Description => "List running processes";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
IEnumerable<Process> processes = ProcessManager.GetAllProcesses();
|
||||
|
||||
printLine("Running processes:");
|
||||
|
||||
foreach (Process process in processes)
|
||||
{
|
||||
printLine($" ID: {process.Id}, Name: {process.Name}");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StopProcessCommand : ICommand
|
||||
{
|
||||
public string Name => "stop";
|
||||
|
||||
public string Description => "Stop a process by ID";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
string? idText = arguments;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(idText))
|
||||
{
|
||||
printLine("Usage: stop <process-id>");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (int.TryParse(idText, out int processId))
|
||||
{
|
||||
ProcessManager.StopProcess(processId);
|
||||
printLine($"Stopped process with ID {processId}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
printLine("Invalid process ID");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public class RebootCommand : ICommand
|
||||
{
|
||||
public string Name => "reboot";
|
||||
|
||||
public string Description => "Reboot the system";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Rebooting system...");
|
||||
Sys.Power.Reboot();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using RemSox.Kernel.Processes;
|
||||
using RemSox.Kernel.Processing;
|
||||
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public class StartGuiCommand : ICommand
|
||||
{
|
||||
public string Name => "start-gui";
|
||||
public string Description => "Starts the Graphical User Interface (Desktop Process)";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Starting Desktop Process...");
|
||||
|
||||
if (ProcessManager.IsProcessRunning<DesktopProcess>())
|
||||
{
|
||||
printLine("Desktop Process is already running.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_ = ProcessManager.SpawnProcess<DesktopProcess>();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using RemSox.Kernel.Processes;
|
||||
using RemSox.Kernel.Processing;
|
||||
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public class StartRemoteDesktopCommand : ICommand
|
||||
{
|
||||
public string Name => "rdp-start";
|
||||
public string Description => "Starts the remote desktop server on the specified port (rdp-start <port>)";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(arguments) || !int.TryParse(arguments.Trim(), out int port) || port <= 0 || port > 65535)
|
||||
{
|
||||
printLine("Usage: rdp-start <port> (1-65535)");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (ProcessManager.IsProcessRunning<RemoteDesktopProcess>())
|
||||
{
|
||||
printLine("Remote desktop server is already running.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_ = ProcessManager.SpawnProcess<RemoteDesktopProcess>([port.ToString()]);
|
||||
printLine($"Remote desktop server started on port {port}.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using RemSox.Kernel.Processes;
|
||||
using RemSox.Kernel.Processing;
|
||||
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public class StopGuiCommand : ICommand
|
||||
{
|
||||
public string Name => "stop-gui";
|
||||
public string Description => "Stops the Graphical User Interface (Desktop Process)";
|
||||
|
||||
public async Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Stopping Desktop Process...");
|
||||
|
||||
if (!ProcessManager.IsProcessRunning<DesktopProcess>())
|
||||
{
|
||||
printLine("Desktop Process is not running.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (Process process in ProcessManager.GetProcessesOfType<DesktopProcess>())
|
||||
{
|
||||
await ProcessManager.StopProcessAndWaitAsync(process.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using RemSox.Kernel.Processes;
|
||||
using RemSox.Kernel.Processing;
|
||||
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public class StopRemoteDesktopCommand : ICommand
|
||||
{
|
||||
public string Name => "rdp-stop";
|
||||
public string Description => "Stops the remote desktop server";
|
||||
|
||||
public async Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
if (!ProcessManager.IsProcessRunning<RemoteDesktopProcess>())
|
||||
{
|
||||
printLine("Remote desktop server is not running.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (RemoteDesktopProcess process in ProcessManager.GetProcessesOfType<RemoteDesktopProcess>())
|
||||
{
|
||||
await ProcessManager.StopProcessAndWaitAsync(process.Id);
|
||||
}
|
||||
|
||||
printLine("Remote desktop server stopped.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using RemSox.Kernel.Logging;
|
||||
using RemSox.Kernel.Processing;
|
||||
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public class ViewProcessLogs : ICommand
|
||||
{
|
||||
public string Name => "logs";
|
||||
|
||||
public string Description => "View logs for a process";
|
||||
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
if (int.TryParse(arguments, out int processId))
|
||||
{
|
||||
IEnumerable<LogEntry> logs = ProcessManager.GetProcessLogs(processId);
|
||||
PrintLogs(logs, printLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
IEnumerable<LogEntry> logs = ProcessManager.GetLogs();
|
||||
PrintLogs(logs, printLine);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static void PrintLogs(IEnumerable<LogEntry> logs, Action<string> printLine)
|
||||
{
|
||||
if (!logs.Any())
|
||||
{
|
||||
printLine("No logs found!");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (LogEntry log in logs)
|
||||
{
|
||||
printLine($"[{log.Timestamp:HH:mm:ss}] [{log.Severity}] {log.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using RemSox.Kernel.Processing;
|
||||
|
||||
namespace RemSox.Kernel.UI.CLI.Commands;
|
||||
|
||||
public class YesNtCommand : ICommand
|
||||
{
|
||||
public string Name => "yesnt";
|
||||
|
||||
public string Description => "Start the YesNt interpreter";
|
||||
|
||||
private int processId;
|
||||
|
||||
public async Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
processId = ProcessManager.SpawnProcess<Processes.YesNtInterpreterProcess>(arguments?.Split(',') ?? []);
|
||||
await ProcessManager.WaitForProcessExitAsync(processId);
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
ProcessManager.StopProcess(processId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace RemSox.Kernel.UI.CLI;
|
||||
/// <summary>
|
||||
/// Defines a command executable via the CLI or GUI terminal.
|
||||
/// </summary>
|
||||
public interface ICommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name of the command used to invoke it.
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a brief description of the command's functionality.
|
||||
/// </summary>
|
||||
string Description { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Executes the command.
|
||||
/// </summary>
|
||||
/// <param name="arguments">The arguments provided to the command.</param>
|
||||
/// <param name="printLine">A delegate to stream output lines to the current console or terminal.</param>
|
||||
Task ExecuteAsync(string? arguments, Action<string> printLine);
|
||||
|
||||
/// <summary>
|
||||
/// Interrupts the command if it's currently running. This is called when the user presses Ctrl+C in the CLI.
|
||||
/// </summary>
|
||||
Task StopAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user