Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Logging;
  4. using RackPeek.Commands;
  5. using RackPeek.Commands.Server;
  6. using RackPeek.Commands.Server.Cpus;
  7. using RackPeek.Commands.Server.Drives;
  8. using RackPeek.Commands.Server.Gpu;
  9. using RackPeek.Commands.Server.Nics;
  10. using RackPeek.Commands.Switches;
  11. using RackPeek.Domain.Resources.Hardware;
  12. using RackPeek.Domain.Resources.Hardware.Reports;
  13. using RackPeek.Domain.Resources.Hardware.Server;
  14. using RackPeek.Domain.Resources.Hardware.Server.Cpu;
  15. using RackPeek.Domain.Resources.Hardware.Server.Drive;
  16. using RackPeek.Domain.Resources.Hardware.Server.Gpu;
  17. using RackPeek.Domain.Resources.Hardware.Server.Nic;
  18. using RackPeek.Domain.Resources.Hardware.Switchs;
  19. using RackPeek.Spectre;
  20. using RackPeek.Yaml;
  21. using Spectre.Console.Cli;
  22. namespace RackPeek;
  23. public static class Program
  24. {
  25. public static async Task<int> Main(string[] args)
  26. {
  27. // Configuration
  28. var configuration = new ConfigurationBuilder()
  29. .SetBasePath(Directory.GetCurrentDirectory())
  30. .AddJsonFile("appsettings.json", true)
  31. .Build();
  32. // DI
  33. var services = new ServiceCollection();
  34. var registrar = new TypeRegistrar(services);
  35. var app = new CommandApp(registrar);
  36. CliBootstrap.BuildApp(app, services, configuration);
  37. services.AddLogging(configure =>
  38. configure
  39. .AddSimpleConsole(opts => { opts.TimestampFormat = "yyyy-MM-dd HH:mm:ss "; }));
  40. return await app.RunAsync(args);
  41. }
  42. }
  43. public static class CliBootstrap
  44. {
  45. public static void BuildApp(CommandApp app, IServiceCollection services, IConfiguration configuration)
  46. {
  47. services.AddSingleton<IConfiguration>(configuration);
  48. // Infrastructure
  49. services.AddScoped<IHardwareRepository>(_ =>
  50. {
  51. var collection = new YamlResourceCollection();
  52. var basePath = configuration["HardwarePath"] ?? Directory.GetCurrentDirectory();
  53. collection.LoadFiles(new[]
  54. {
  55. Path.Combine(basePath, "servers.yaml"),
  56. Path.Combine(basePath, "aps.yaml"),
  57. Path.Combine(basePath, "desktops.yaml"),
  58. Path.Combine(basePath, "switches.yaml"),
  59. Path.Combine(basePath, "ups.yaml"),
  60. Path.Combine(basePath, "firewalls.yaml"),
  61. Path.Combine(basePath, "laptops.yaml"),
  62. Path.Combine(basePath, "routers.yaml")
  63. });
  64. return new YamlHardwareRepository(collection);
  65. });
  66. // Application
  67. services.AddScoped<ServerHardwareReportUseCase>();
  68. services.AddScoped<ServerReportCommand>();
  69. services.AddScoped<AccessPointHardwareReportUseCase>();
  70. services.AddScoped<AccessPointReportCommand>();
  71. services.AddScoped<SwitchHardwareReportUseCase>();
  72. services.AddScoped<SwitchReportCommand>();
  73. services.AddScoped<UpsHardwareReportUseCase>();
  74. services.AddScoped<UpsReportCommand>();
  75. services.AddScoped<DesktopHardwareReportUseCase>();
  76. services.AddScoped<DesktopReportCommand>();
  77. services.AddScoped<AddServerUseCase>();
  78. services.AddScoped<ServerAddCommand>();
  79. services.AddScoped<DeleteServerUseCase>();
  80. services.AddScoped<ServerDeleteCommand>();
  81. services.AddScoped<DescribeServerUseCase>();
  82. services.AddScoped<ServerDescribeCommand>();
  83. services.AddScoped<GetServerUseCase>();
  84. services.AddScoped<ServerGetByNameCommand>();
  85. services.AddScoped<UpdateServerUseCase>();
  86. services.AddScoped<ServerSetCommand>();
  87. // CPU use cases
  88. services.AddScoped<AddCpuUseCase>();
  89. services.AddScoped<UpdateCpuUseCase>();
  90. services.AddScoped<RemoveCpuUseCase>();
  91. // Drive use cases
  92. services.AddScoped<AddDrivesUseCase>();
  93. services.AddScoped<UpdateDriveUseCase>();
  94. services.AddScoped<RemoveDriveUseCase>();
  95. // GPU use cases
  96. services.AddScoped<AddGpuUseCase>();
  97. services.AddScoped<UpdateGpuUseCase>();
  98. services.AddScoped<RemoveGpuUseCase>();
  99. // CPU commands
  100. services.AddScoped<ServerCpuAddCommand>();
  101. services.AddScoped<ServerCpuSetCommand>();
  102. services.AddScoped<ServerCpuRemoveCommand>();
  103. // Switch commands
  104. services.AddScoped<SwitchAddCommand>();
  105. services.AddScoped<SwitchDeleteCommand>();
  106. services.AddScoped<SwitchDescribeCommand>();
  107. services.AddScoped<SwitchGetByNameCommand>();
  108. services.AddScoped<SwitchGetCommand>();
  109. services.AddScoped<SwitchSetCommand>();
  110. // Switch Usecases
  111. services.AddScoped<AddSwitchUseCase>();
  112. services.AddScoped<DeleteSwitchUseCase>();
  113. services.AddScoped<GetSwitchUseCase>();
  114. services.AddScoped<GetSwitchesUseCase>();
  115. services.AddScoped<UpdateSwitchUseCase>();
  116. // NIC use cases
  117. services.AddScoped<AddNicUseCase>();
  118. services.AddScoped<UpdateNicUseCase>();
  119. services.AddScoped<RemoveNicUseCase>();
  120. // NIC commands
  121. services.AddScoped<ServerNicAddCommand>();
  122. services.AddScoped<ServerNicUpdateCommand>();
  123. services.AddScoped<ServerNicRemoveCommand>();
  124. // Drive commands
  125. services.AddScoped<ServerDriveAddCommand>();
  126. services.AddScoped<ServerDriveUpdateCommand>();
  127. services.AddScoped<ServerDriveRemoveCommand>();
  128. // GPU commands
  129. services.AddScoped<ServerGpuAddCommand>();
  130. services.AddScoped<ServerGpuUpdateCommand>();
  131. services.AddScoped<ServerGpuRemoveCommand>();
  132. // Spectre bootstrap
  133. app.Configure(config =>
  134. {
  135. config.SetApplicationName("rackpeek");
  136. // ----------------------------
  137. // Server commands (CRUD-style)
  138. // ----------------------------
  139. config.AddBranch("servers", server =>
  140. {
  141. server.SetDescription("Manage servers");
  142. server.AddCommand<ServerReportCommand>("summary")
  143. .WithDescription("Show server hardware report");
  144. server.AddCommand<ServerAddCommand>("add")
  145. .WithDescription("Add a new server");
  146. server.AddCommand<ServerGetByNameCommand>("get")
  147. .WithDescription("List servers or get a server by name");
  148. server.AddCommand<ServerDescribeCommand>("describe")
  149. .WithDescription("Show detailed information about a server");
  150. server.AddCommand<ServerSetCommand>("set")
  151. .WithDescription("Update server properties");
  152. server.AddCommand<ServerDeleteCommand>("del")
  153. .WithDescription("Delete a server");
  154. server.AddBranch("cpu", cpu =>
  155. {
  156. cpu.SetDescription("Manage server CPUs");
  157. cpu.AddCommand<ServerCpuAddCommand>("add")
  158. .WithDescription("Add a CPU to a server");
  159. cpu.AddCommand<ServerCpuSetCommand>("set")
  160. .WithDescription("Update a CPU on a server");
  161. cpu.AddCommand<ServerCpuRemoveCommand>("del")
  162. .WithDescription("Remove a CPU from a server");
  163. });
  164. server.AddBranch("nic", nic =>
  165. {
  166. nic.SetDescription("Manage server NICs");
  167. nic.AddCommand<ServerNicAddCommand>("add")
  168. .WithDescription("Add a NIC to a server");
  169. nic.AddCommand<ServerNicUpdateCommand>("set")
  170. .WithDescription("Update a NIC on a server");
  171. nic.AddCommand<ServerNicRemoveCommand>("del")
  172. .WithDescription("Remove a NIC from a server");
  173. server.AddBranch("drive", drive =>
  174. {
  175. drive.SetDescription("Manage server drives");
  176. drive.AddCommand<ServerDriveAddCommand>("add")
  177. .WithDescription("Add a drive to a server");
  178. drive.AddCommand<ServerDriveUpdateCommand>("set")
  179. .WithDescription("Update a drive on a server");
  180. drive.AddCommand<ServerDriveRemoveCommand>("del")
  181. .WithDescription("Remove a drive from a server");
  182. });
  183. server.AddBranch("gpu", gpu =>
  184. {
  185. gpu.SetDescription("Manage server GPUs");
  186. gpu.AddCommand<ServerGpuAddCommand>("add")
  187. .WithDescription("Add a GPU to a server");
  188. gpu.AddCommand<ServerGpuUpdateCommand>("set")
  189. .WithDescription("Update a GPU on a server");
  190. gpu.AddCommand<ServerGpuRemoveCommand>("del")
  191. .WithDescription("Remove a GPU from a server");
  192. });
  193. });
  194. config.AddBranch("switches", server =>
  195. {
  196. server.SetDescription("Manage switches");
  197. server.AddCommand<SwitchReportCommand>("summary")
  198. .WithDescription("Show switch hardware report");
  199. server.AddCommand<SwitchAddCommand>("add")
  200. .WithDescription("Add a new switch");
  201. server.AddCommand<SwitchGetByNameCommand>("get")
  202. .WithDescription("List switches or get a switches by name");
  203. server.AddCommand<SwitchDescribeCommand>("describe")
  204. .WithDescription("Show detailed information about a switch");
  205. server.AddCommand<SwitchSetCommand>("set")
  206. .WithDescription("Update switch properties");
  207. server.AddCommand<SwitchDeleteCommand>("del")
  208. .WithDescription("Delete a switch");
  209. });
  210. // ----------------------------
  211. // Reports (read-only summaries)
  212. // ----------------------------
  213. config.AddCommand<AccessPointReportCommand>("ap")
  214. .WithDescription("Show access point hardware report");
  215. config.AddCommand<DesktopReportCommand>("desktops")
  216. .WithDescription("Show desktop hardware report");
  217. config.AddCommand<UpsReportCommand>("ups")
  218. .WithDescription("Show UPS hardware report");
  219. config.ValidateExamples();
  220. });
  221. });
  222. }
  223. }