Program.cs 13 KB

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