using System.ComponentModel.DataAnnotations; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using RackPeek.Commands; using RackPeek.Commands.AccessPoints; using RackPeek.Commands.Desktops; using RackPeek.Commands.Desktops.Cpus; using RackPeek.Commands.Desktops.Drive; using RackPeek.Commands.Desktops.Gpus; using RackPeek.Commands.Desktops.Nics; using RackPeek.Commands.Firewalls; using RackPeek.Commands.Firewalls.Ports; using RackPeek.Commands.Laptops; using RackPeek.Commands.Laptops.Cpus; using RackPeek.Commands.Laptops.Drive; using RackPeek.Commands.Laptops.Gpus; using RackPeek.Commands.Routers; using RackPeek.Commands.Routers.Ports; using RackPeek.Commands.Servers; using RackPeek.Commands.Servers.Cpus; using RackPeek.Commands.Servers.Drives; using RackPeek.Commands.Servers.Gpus; using RackPeek.Commands.Servers.Nics; using RackPeek.Commands.Services; using RackPeek.Commands.Switches; using RackPeek.Commands.Switches.Ports; using RackPeek.Commands.Systems; using RackPeek.Commands.Ups; using RackPeek.Domain; using RackPeek.Domain.Helpers; using RackPeek.Domain.Persistence; using RackPeek.Domain.Persistence.Yaml; using RackPeek.Domain.Resources; using RackPeek.Domain.Resources.Hardware; using RackPeek.Domain.Resources.Services; using RackPeek.Domain.Resources.SystemResources; using RackPeek.Yaml; using Spectre.Console; using Spectre.Console.Cli; namespace RackPeek; public static class CliBootstrap { public static async Task RegisterInternals(IServiceCollection services, IConfiguration configuration, string yamlDir, string yamlFile) { services.AddSingleton(configuration); var basePath = configuration["HardwarePath"] ?? Directory.GetCurrentDirectory(); // Resolve yamlDir as relative to basePath var yamlPath = Path.IsPathRooted(yamlDir) ? yamlDir : Path.Combine(basePath, yamlDir); if (!Directory.Exists(yamlPath)) throw new DirectoryNotFoundException($"YAML directory not found: {yamlPath}"); var collection = new YamlResourceCollection(Path.Combine(yamlDir, yamlFile), new PhysicalTextFileStore(), new ResourceCollection()); await collection.LoadAsync(); services.AddSingleton(collection); // Infrastructure services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Application services.AddUseCases(); services.AddCommands(); } public static void BuildApp(CommandApp app) { // Spectre bootstrap app.Configure(config => { config.SetApplicationName("rpk"); config.ValidateExamples(); config.SetApplicationVersion("0.0.3"); config.SetExceptionHandler(HandleException); // Global summary config.AddCommand("summary") .WithDescription("Show a summarized report of all resources in the system."); // ---------------------------- // Server commands (CRUD-style) // ---------------------------- config.AddBranch("servers", server => { server.SetDescription("Manage servers and their components."); server.AddCommand("summary") .WithDescription("Show a summarized hardware report for all servers."); server.AddCommand("add").WithDescription("Add a new server to the inventory."); server.AddCommand("get") .WithDescription("List all servers or retrieve a specific server by name."); server.AddCommand("describe") .WithDescription("Display detailed information about a specific server."); server.AddCommand("set").WithDescription("Update properties of an existing server."); server.AddCommand("del").WithDescription("Delete a server from the inventory."); server.AddCommand("tree") .WithDescription("Display the dependency tree of a server."); // Server CPUs server.AddBranch("cpu", cpu => { cpu.SetDescription("Manage CPUs attached to a server."); cpu.AddCommand("add").WithDescription("Add a CPU to a specific server."); cpu.AddCommand("set").WithDescription("Update configuration of a server CPU."); cpu.AddCommand("del").WithDescription("Remove a CPU from a server."); }); // Server Drives server.AddBranch("drive", drive => { drive.SetDescription("Manage drives attached to a server."); drive.AddCommand("add").WithDescription("Add a storage drive to a server."); drive.AddCommand("set") .WithDescription("Update properties of a server drive."); drive.AddCommand("del").WithDescription("Remove a drive from a server."); }); // Server GPUs server.AddBranch("gpu", gpu => { gpu.SetDescription("Manage GPUs attached to a server."); gpu.AddCommand("add").WithDescription("Add a GPU to a server."); gpu.AddCommand("set").WithDescription("Update properties of a server GPU."); gpu.AddCommand("del").WithDescription("Remove a GPU from a server."); }); // Server NICs server.AddBranch("nic", nic => { nic.SetDescription("Manage network interface cards (NICs) for a server."); nic.AddCommand("add").WithDescription("Add a NIC to a server."); nic.AddCommand("set").WithDescription("Update properties of a server NIC."); nic.AddCommand("del").WithDescription("Remove a NIC from a server."); }); }); // ---------------------------- // Switch commands // ---------------------------- config.AddBranch("switches", switches => { switches.SetDescription("Manage network switches."); switches.AddCommand("summary") .WithDescription("Show a hardware report for all switches."); switches.AddCommand("add") .WithDescription("Add a new network switch to the inventory."); switches.AddCommand("list").WithDescription("List all switches in the system."); switches.AddCommand("get") .WithDescription("Retrieve details of a specific switch by name."); switches.AddCommand("describe") .WithDescription("Show detailed information about a switch."); switches.AddCommand("set").WithDescription("Update properties of a switch."); switches.AddCommand("del").WithDescription("Delete a switch from the inventory."); switches.AddBranch("port", port => { port.SetDescription("Manage ports on a network switch."); port.AddCommand("add").WithDescription("Add a port to a switch."); port.AddCommand("set").WithDescription("Update a switch port."); port.AddCommand("del").WithDescription("Remove a port from a switch."); }); }); // ---------------------------- // Routers commands // ---------------------------- config.AddBranch("routers", routers => { routers.SetDescription("Manage network routers."); routers.AddCommand("summary") .WithDescription("Show a hardware report for all routers."); routers.AddCommand("add") .WithDescription("Add a new network router to the inventory."); routers.AddCommand("list").WithDescription("List all routers in the system."); routers.AddCommand("get") .WithDescription("Retrieve details of a specific router by name."); routers.AddCommand("describe") .WithDescription("Show detailed information about a router."); routers.AddCommand("set").WithDescription("Update properties of a router."); routers.AddCommand("del").WithDescription("Delete a router from the inventory."); routers.AddBranch("port", port => { port.SetDescription("Manage ports on a router."); port.AddCommand("add").WithDescription("Add a port to a router."); port.AddCommand("set").WithDescription("Update a router port."); port.AddCommand("del").WithDescription("Remove a port from a router."); }); }); // ---------------------------- // Firewalls commands // ---------------------------- config.AddBranch("firewalls", firewalls => { firewalls.SetDescription("Manage firewalls."); firewalls.AddCommand("summary") .WithDescription("Show a hardware report for all firewalls."); firewalls.AddCommand("add").WithDescription("Add a new firewall to the inventory."); firewalls.AddCommand("list").WithDescription("List all firewalls in the system."); firewalls.AddCommand("get") .WithDescription("Retrieve details of a specific firewall by name."); firewalls.AddCommand("describe") .WithDescription("Show detailed information about a firewall."); firewalls.AddCommand("set").WithDescription("Update properties of a firewall."); firewalls.AddCommand("del") .WithDescription("Delete a firewall from the inventory."); firewalls.AddBranch("port", port => { port.SetDescription("Manage ports on a firewall."); port.AddCommand("add").WithDescription("Add a port to a firewall."); port.AddCommand("set").WithDescription("Update a firewall port."); port.AddCommand("del").WithDescription("Remove a port from a firewall."); }); }); // ---------------------------- // System commands // ---------------------------- config.AddBranch("systems", system => { system.SetDescription("Manage systems and their dependencies."); system.AddCommand("summary") .WithDescription("Show a summary report for all systems."); system.AddCommand("add").WithDescription("Add a new system to the inventory."); system.AddCommand("list").WithDescription("List all systems."); system.AddCommand("get").WithDescription("Retrieve a system by name."); system.AddCommand("describe") .WithDescription("Display detailed information about a system."); system.AddCommand("set").WithDescription("Update properties of a system."); system.AddCommand("del").WithDescription("Delete a system from the inventory."); system.AddCommand("tree") .WithDescription("Display the dependency tree for a system."); }); // ---------------------------- // Access Points // ---------------------------- config.AddBranch("accesspoints", ap => { ap.SetDescription("Manage access points."); ap.AddCommand("summary") .WithDescription("Show a hardware report for all access points."); ap.AddCommand("add").WithDescription("Add a new access point."); ap.AddCommand("list").WithDescription("List all access points."); ap.AddCommand("get").WithDescription("Retrieve an access point by name."); ap.AddCommand("describe") .WithDescription("Show detailed information about an access point."); ap.AddCommand("set").WithDescription("Update properties of an access point."); ap.AddCommand("del").WithDescription("Delete an access point."); }); // ---------------------------- // UPS units // ---------------------------- config.AddBranch("ups", ups => { ups.SetDescription("Manage UPS units."); ups.AddCommand("summary") .WithDescription("Show a hardware report for all UPS units."); ups.AddCommand("add").WithDescription("Add a new UPS unit."); ups.AddCommand("list").WithDescription("List all UPS units."); ups.AddCommand("get").WithDescription("Retrieve a UPS unit by name."); ups.AddCommand("describe") .WithDescription("Show detailed information about a UPS unit."); ups.AddCommand("set").WithDescription("Update properties of a UPS unit."); ups.AddCommand("del").WithDescription("Delete a UPS unit."); }); // ---------------------------- // Desktops // ---------------------------- config.AddBranch("desktops", desktops => { desktops.SetDescription("Manage desktop computers and their components."); // CRUD desktops.AddCommand("add").WithDescription("Add a new desktop."); desktops.AddCommand("list").WithDescription("List all desktops."); desktops.AddCommand("get").WithDescription("Retrieve a desktop by name."); desktops.AddCommand("describe") .WithDescription("Show detailed information about a desktop."); desktops.AddCommand("set").WithDescription("Update properties of a desktop."); desktops.AddCommand("del") .WithDescription("Delete a desktop from the inventory."); desktops.AddCommand("summary") .WithDescription("Show a summarized hardware report for all desktops."); desktops.AddCommand("tree") .WithDescription("Display the dependency tree for a desktop."); // CPU desktops.AddBranch("cpu", cpu => { cpu.SetDescription("Manage CPUs attached to desktops."); cpu.AddCommand("add").WithDescription("Add a CPU to a desktop."); cpu.AddCommand("set").WithDescription("Update a desktop CPU."); cpu.AddCommand("del").WithDescription("Remove a CPU from a desktop."); }); // Drives desktops.AddBranch("drive", drive => { drive.SetDescription("Manage storage drives attached to desktops."); drive.AddCommand("add").WithDescription("Add a drive to a desktop."); drive.AddCommand("set").WithDescription("Update a desktop drive."); drive.AddCommand("del") .WithDescription("Remove a drive from a desktop."); }); // GPUs desktops.AddBranch("gpu", gpu => { gpu.SetDescription("Manage GPUs attached to desktops."); gpu.AddCommand("add").WithDescription("Add a GPU to a desktop."); gpu.AddCommand("set").WithDescription("Update a desktop GPU."); gpu.AddCommand("del").WithDescription("Remove a GPU from a desktop."); }); // NICs desktops.AddBranch("nic", nic => { nic.SetDescription("Manage network interface cards (NICs) for desktops."); nic.AddCommand("add").WithDescription("Add a NIC to a desktop."); nic.AddCommand("set").WithDescription("Update a desktop NIC."); nic.AddCommand("del").WithDescription("Remove a NIC from a desktop."); }); }); // ---------------------------- // Laptops // ---------------------------- config.AddBranch("Laptops", Laptops => { Laptops.SetDescription("Manage Laptop computers and their components."); // CRUD Laptops.AddCommand("add").WithDescription("Add a new Laptop."); Laptops.AddCommand("list").WithDescription("List all Laptops."); Laptops.AddCommand("get").WithDescription("Retrieve a Laptop by name."); Laptops.AddCommand("describe") .WithDescription("Show detailed information about a Laptop."); Laptops.AddCommand("del").WithDescription("Delete a Laptop from the inventory."); Laptops.AddCommand("summary") .WithDescription("Show a summarized hardware report for all Laptops."); Laptops.AddCommand("tree") .WithDescription("Display the dependency tree for a Laptop."); // CPU Laptops.AddBranch("cpu", cpu => { cpu.SetDescription("Manage CPUs attached to Laptops."); cpu.AddCommand("add").WithDescription("Add a CPU to a Laptop."); cpu.AddCommand("set").WithDescription("Update a Laptop CPU."); cpu.AddCommand("del").WithDescription("Remove a CPU from a Laptop."); }); // Drives Laptops.AddBranch("drive", drive => { drive.SetDescription("Manage storage drives attached to Laptops."); drive.AddCommand("add").WithDescription("Add a drive to a Laptop."); drive.AddCommand("set").WithDescription("Update a Laptop drive."); drive.AddCommand("del").WithDescription("Remove a drive from a Laptop."); }); // GPUs Laptops.AddBranch("gpu", gpu => { gpu.SetDescription("Manage GPUs attached to Laptops."); gpu.AddCommand("add").WithDescription("Add a GPU to a Laptop."); gpu.AddCommand("set").WithDescription("Update a Laptop GPU."); gpu.AddCommand("del").WithDescription("Remove a GPU from a Laptop."); }); }); // ---------------------------- // Services // ---------------------------- config.AddBranch("services", service => { service.SetDescription("Manage services and their configurations."); service.AddCommand("summary") .WithDescription("Show a summary report for all services."); service.AddCommand("add").WithDescription("Add a new service."); service.AddCommand("list").WithDescription("List all services."); service.AddCommand("get").WithDescription("Retrieve a service by name."); service.AddCommand("describe") .WithDescription("Show detailed information about a service."); service.AddCommand("set").WithDescription("Update properties of a service."); service.AddCommand("del").WithDescription("Delete a service."); service.AddCommand("subnets") .WithDescription("List subnets associated with a service, optionally filtered by CIDR."); }); }); } private static int HandleException(Exception ex, ITypeResolver? arg2) { switch (ex) { case ValidationException ve: AnsiConsole.MarkupLine($"[yellow]Validation error:[/] {ve.Message}"); return 2; case ConflictException ce: AnsiConsole.MarkupLine($"[red]Conflict:[/] {ce.Message}"); return 3; case NotFoundException ne: AnsiConsole.MarkupLine($"[red]Not found:[/] {ne.Message}"); return 4; default: AnsiConsole.MarkupLine("[red]Unexpected error occurred.[/]"); AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything); return 99; } } }