CliBootstrap.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System.ComponentModel.DataAnnotations;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using RackPeek.Commands;
  5. using RackPeek.Commands.AccessPoints;
  6. using RackPeek.Commands.Desktops;
  7. using RackPeek.Commands.Desktops.Cpus;
  8. using RackPeek.Commands.Desktops.Drive;
  9. using RackPeek.Commands.Desktops.Gpus;
  10. using RackPeek.Commands.Desktops.Nics;
  11. using RackPeek.Commands.Firewalls;
  12. using RackPeek.Commands.Firewalls.Ports;
  13. using RackPeek.Commands.Laptops;
  14. using RackPeek.Commands.Laptops.Cpus;
  15. using RackPeek.Commands.Laptops.Drive;
  16. using RackPeek.Commands.Laptops.Gpus;
  17. using RackPeek.Commands.Routers;
  18. using RackPeek.Commands.Routers.Ports;
  19. using RackPeek.Commands.Servers;
  20. using RackPeek.Commands.Servers.Cpus;
  21. using RackPeek.Commands.Servers.Drives;
  22. using RackPeek.Commands.Servers.Gpus;
  23. using RackPeek.Commands.Servers.Nics;
  24. using RackPeek.Commands.Services;
  25. using RackPeek.Commands.Switches;
  26. using RackPeek.Commands.Switches.Ports;
  27. using RackPeek.Commands.Systems;
  28. using RackPeek.Commands.Ups;
  29. using RackPeek.Domain;
  30. using RackPeek.Domain.Helpers;
  31. using RackPeek.Domain.Persistence;
  32. using RackPeek.Domain.Persistence.Yaml;
  33. using RackPeek.Domain.Resources;
  34. using RackPeek.Domain.Resources.Hardware;
  35. using RackPeek.Domain.Resources.Services;
  36. using RackPeek.Domain.Resources.SystemResources;
  37. using RackPeek.Yaml;
  38. using Spectre.Console;
  39. using Spectre.Console.Cli;
  40. namespace RackPeek;
  41. public static class CliBootstrap
  42. {
  43. public static async Task RegisterInternals(IServiceCollection services, IConfiguration configuration,
  44. string yamlDir, string yamlFile)
  45. {
  46. services.AddSingleton(configuration);
  47. var basePath = configuration["HardwarePath"] ?? Directory.GetCurrentDirectory();
  48. // Resolve yamlDir as relative to basePath
  49. var yamlPath = Path.IsPathRooted(yamlDir) ? yamlDir : Path.Combine(basePath, yamlDir);
  50. if (!Directory.Exists(yamlPath)) throw new DirectoryNotFoundException($"YAML directory not found: {yamlPath}");
  51. var collection = new YamlResourceCollection(Path.Combine(yamlDir, yamlFile), new PhysicalTextFileStore(), new ResourceCollection());
  52. await collection.LoadAsync();
  53. services.AddSingleton<IResourceCollection>(collection);
  54. // Infrastructure
  55. services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
  56. services.AddScoped<ISystemRepository, YamlSystemRepository>();
  57. services.AddScoped<IServiceRepository, YamlServiceRepository>();
  58. services.AddScoped<IResourceRepository, YamlResourceRepository>();
  59. // Application
  60. services.AddUseCases();
  61. services.AddCommands();
  62. }
  63. public static void BuildApp(CommandApp app)
  64. {
  65. // Spectre bootstrap
  66. app.Configure(config =>
  67. {
  68. config.SetApplicationName("rpk");
  69. config.ValidateExamples();
  70. config.SetExceptionHandler(HandleException);
  71. // Global summary
  72. config.AddCommand<GetTotalSummaryCommand>("summary")
  73. .WithDescription("Show a summarized report of all resources in the system.");
  74. // ----------------------------
  75. // Server commands (CRUD-style)
  76. // ----------------------------
  77. config.AddBranch("servers", server =>
  78. {
  79. server.SetDescription("Manage servers and their components.");
  80. server.AddCommand<ServerReportCommand>("summary")
  81. .WithDescription("Show a summarized hardware report for all servers.");
  82. server.AddCommand<ServerAddCommand>("add").WithDescription("Add a new server to the inventory.");
  83. server.AddCommand<ServerGetByNameCommand>("get")
  84. .WithDescription("List all servers or retrieve a specific server by name.");
  85. server.AddCommand<ServerDescribeCommand>("describe")
  86. .WithDescription("Display detailed information about a specific server.");
  87. server.AddCommand<ServerSetCommand>("set").WithDescription("Update properties of an existing server.");
  88. server.AddCommand<ServerDeleteCommand>("del").WithDescription("Delete a server from the inventory.");
  89. server.AddCommand<ServerTreeCommand>("tree")
  90. .WithDescription("Display the dependency tree of a server.");
  91. // Server CPUs
  92. server.AddBranch("cpu", cpu =>
  93. {
  94. cpu.SetDescription("Manage CPUs attached to a server.");
  95. cpu.AddCommand<ServerCpuAddCommand>("add").WithDescription("Add a CPU to a specific server.");
  96. cpu.AddCommand<ServerCpuSetCommand>("set").WithDescription("Update configuration of a server CPU.");
  97. cpu.AddCommand<ServerCpuRemoveCommand>("del").WithDescription("Remove a CPU from a server.");
  98. });
  99. // Server Drives
  100. server.AddBranch("drive", drive =>
  101. {
  102. drive.SetDescription("Manage drives attached to a server.");
  103. drive.AddCommand<ServerDriveAddCommand>("add").WithDescription("Add a storage drive to a server.");
  104. drive.AddCommand<ServerDriveUpdateCommand>("set")
  105. .WithDescription("Update properties of a server drive.");
  106. drive.AddCommand<ServerDriveRemoveCommand>("del").WithDescription("Remove a drive from a server.");
  107. });
  108. // Server GPUs
  109. server.AddBranch("gpu", gpu =>
  110. {
  111. gpu.SetDescription("Manage GPUs attached to a server.");
  112. gpu.AddCommand<ServerGpuAddCommand>("add").WithDescription("Add a GPU to a server.");
  113. gpu.AddCommand<ServerGpuUpdateCommand>("set").WithDescription("Update properties of a server GPU.");
  114. gpu.AddCommand<ServerGpuRemoveCommand>("del").WithDescription("Remove a GPU from a server.");
  115. });
  116. // Server NICs
  117. server.AddBranch("nic", nic =>
  118. {
  119. nic.SetDescription("Manage network interface cards (NICs) for a server.");
  120. nic.AddCommand<ServerNicAddCommand>("add").WithDescription("Add a NIC to a server.");
  121. nic.AddCommand<ServerNicUpdateCommand>("set").WithDescription("Update properties of a server NIC.");
  122. nic.AddCommand<ServerNicRemoveCommand>("del").WithDescription("Remove a NIC from a server.");
  123. });
  124. });
  125. // ----------------------------
  126. // Switch commands
  127. // ----------------------------
  128. config.AddBranch("switches", switches =>
  129. {
  130. switches.SetDescription("Manage network switches.");
  131. switches.AddCommand<SwitchReportCommand>("summary")
  132. .WithDescription("Show a hardware report for all switches.");
  133. switches.AddCommand<SwitchAddCommand>("add")
  134. .WithDescription("Add a new network switch to the inventory.");
  135. switches.AddCommand<SwitchGetCommand>("list").WithDescription("List all switches in the system.");
  136. switches.AddCommand<SwitchGetByNameCommand>("get")
  137. .WithDescription("Retrieve details of a specific switch by name.");
  138. switches.AddCommand<SwitchDescribeCommand>("describe")
  139. .WithDescription("Show detailed information about a switch.");
  140. switches.AddCommand<SwitchSetCommand>("set").WithDescription("Update properties of a switch.");
  141. switches.AddCommand<SwitchDeleteCommand>("del").WithDescription("Delete a switch from the inventory.");
  142. switches.AddBranch("port", port =>
  143. {
  144. port.SetDescription("Manage ports on a network switch.");
  145. port.AddCommand<SwitchPortAddCommand>("add").WithDescription("Add a port to a switch.");
  146. port.AddCommand<SwitchPortUpdateCommand>("set").WithDescription("Update a switch port.");
  147. port.AddCommand<SwitchPortRemoveCommand>("del").WithDescription("Remove a port from a switch.");
  148. });
  149. });
  150. // ----------------------------
  151. // Routers commands
  152. // ----------------------------
  153. config.AddBranch("routers", routers =>
  154. {
  155. routers.SetDescription("Manage network routers.");
  156. routers.AddCommand<RouterReportCommand>("summary")
  157. .WithDescription("Show a hardware report for all routers.");
  158. routers.AddCommand<RouterAddCommand>("add")
  159. .WithDescription("Add a new network router to the inventory.");
  160. routers.AddCommand<RouterGetCommand>("list").WithDescription("List all routers in the system.");
  161. routers.AddCommand<RouterGetByNameCommand>("get")
  162. .WithDescription("Retrieve details of a specific router by name.");
  163. routers.AddCommand<RouterDescribeCommand>("describe")
  164. .WithDescription("Show detailed information about a router.");
  165. routers.AddCommand<RouterSetCommand>("set").WithDescription("Update properties of a router.");
  166. routers.AddCommand<RouterDeleteCommand>("del").WithDescription("Delete a router from the inventory.");
  167. routers.AddBranch("port", port =>
  168. {
  169. port.SetDescription("Manage ports on a router.");
  170. port.AddCommand<RouterPortAddCommand>("add").WithDescription("Add a port to a router.");
  171. port.AddCommand<RouterPortUpdateCommand>("set").WithDescription("Update a router port.");
  172. port.AddCommand<RouterPortRemoveCommand>("del").WithDescription("Remove a port from a router.");
  173. });
  174. });
  175. // ----------------------------
  176. // Firewalls commands
  177. // ----------------------------
  178. config.AddBranch("firewalls", firewalls =>
  179. {
  180. firewalls.SetDescription("Manage firewalls.");
  181. firewalls.AddCommand<FirewallReportCommand>("summary")
  182. .WithDescription("Show a hardware report for all firewalls.");
  183. firewalls.AddCommand<FirewallAddCommand>("add").WithDescription("Add a new firewall to the inventory.");
  184. firewalls.AddCommand<FirewallGetCommand>("list").WithDescription("List all firewalls in the system.");
  185. firewalls.AddCommand<FirewallGetByNameCommand>("get")
  186. .WithDescription("Retrieve details of a specific firewall by name.");
  187. firewalls.AddCommand<FirewallDescribeCommand>("describe")
  188. .WithDescription("Show detailed information about a firewall.");
  189. firewalls.AddCommand<FirewallSetCommand>("set").WithDescription("Update properties of a firewall.");
  190. firewalls.AddCommand<FirewallDeleteCommand>("del")
  191. .WithDescription("Delete a firewall from the inventory.");
  192. firewalls.AddBranch("port", port =>
  193. {
  194. port.SetDescription("Manage ports on a firewall.");
  195. port.AddCommand<FirewallPortAddCommand>("add").WithDescription("Add a port to a firewall.");
  196. port.AddCommand<FirewallPortUpdateCommand>("set").WithDescription("Update a firewall port.");
  197. port.AddCommand<FirewallPortRemoveCommand>("del").WithDescription("Remove a port from a firewall.");
  198. });
  199. });
  200. // ----------------------------
  201. // System commands
  202. // ----------------------------
  203. config.AddBranch("systems", system =>
  204. {
  205. system.SetDescription("Manage systems and their dependencies.");
  206. system.AddCommand<SystemReportCommand>("summary")
  207. .WithDescription("Show a summary report for all systems.");
  208. system.AddCommand<SystemAddCommand>("add").WithDescription("Add a new system to the inventory.");
  209. system.AddCommand<SystemGetCommand>("list").WithDescription("List all systems.");
  210. system.AddCommand<SystemGetByNameCommand>("get").WithDescription("Retrieve a system by name.");
  211. system.AddCommand<SystemDescribeCommand>("describe")
  212. .WithDescription("Display detailed information about a system.");
  213. system.AddCommand<SystemSetCommand>("set").WithDescription("Update properties of a system.");
  214. system.AddCommand<SystemDeleteCommand>("del").WithDescription("Delete a system from the inventory.");
  215. system.AddCommand<SystemTreeCommand>("tree")
  216. .WithDescription("Display the dependency tree for a system.");
  217. });
  218. // ----------------------------
  219. // Access Points
  220. // ----------------------------
  221. config.AddBranch("accesspoints", ap =>
  222. {
  223. ap.SetDescription("Manage access points.");
  224. ap.AddCommand<AccessPointReportCommand>("summary")
  225. .WithDescription("Show a hardware report for all access points.");
  226. ap.AddCommand<AccessPointAddCommand>("add").WithDescription("Add a new access point.");
  227. ap.AddCommand<AccessPointGetCommand>("list").WithDescription("List all access points.");
  228. ap.AddCommand<AccessPointGetByNameCommand>("get").WithDescription("Retrieve an access point by name.");
  229. ap.AddCommand<AccessPointDescribeCommand>("describe")
  230. .WithDescription("Show detailed information about an access point.");
  231. ap.AddCommand<AccessPointSetCommand>("set").WithDescription("Update properties of an access point.");
  232. ap.AddCommand<AccessPointDeleteCommand>("del").WithDescription("Delete an access point.");
  233. });
  234. // ----------------------------
  235. // UPS units
  236. // ----------------------------
  237. config.AddBranch("ups", ups =>
  238. {
  239. ups.SetDescription("Manage UPS units.");
  240. ups.AddCommand<UpsReportCommand>("summary")
  241. .WithDescription("Show a hardware report for all UPS units.");
  242. ups.AddCommand<UpsAddCommand>("add").WithDescription("Add a new UPS unit.");
  243. ups.AddCommand<UpsGetCommand>("list").WithDescription("List all UPS units.");
  244. ups.AddCommand<UpsGetByNameCommand>("get").WithDescription("Retrieve a UPS unit by name.");
  245. ups.AddCommand<UpsDescribeCommand>("describe")
  246. .WithDescription("Show detailed information about a UPS unit.");
  247. ups.AddCommand<UpsSetCommand>("set").WithDescription("Update properties of a UPS unit.");
  248. ups.AddCommand<UpsDeleteCommand>("del").WithDescription("Delete a UPS unit.");
  249. });
  250. // ----------------------------
  251. // Desktops
  252. // ----------------------------
  253. config.AddBranch("desktops", desktops =>
  254. {
  255. desktops.SetDescription("Manage desktop computers and their components.");
  256. // CRUD
  257. desktops.AddCommand<DesktopAddCommand>("add").WithDescription("Add a new desktop.");
  258. desktops.AddCommand<DesktopGetCommand>("list").WithDescription("List all desktops.");
  259. desktops.AddCommand<DesktopGetByNameCommand>("get").WithDescription("Retrieve a desktop by name.");
  260. desktops.AddCommand<DesktopDescribeCommand>("describe")
  261. .WithDescription("Show detailed information about a desktop.");
  262. desktops.AddCommand<DesktopSetCommand>("set").WithDescription("Update properties of a desktop.");
  263. desktops.AddCommand<DesktopDeleteCommand>("del")
  264. .WithDescription("Delete a desktop from the inventory.");
  265. desktops.AddCommand<DesktopReportCommand>("summary")
  266. .WithDescription("Show a summarized hardware report for all desktops.");
  267. desktops.AddCommand<DesktopTreeCommand>("tree")
  268. .WithDescription("Display the dependency tree for a desktop.");
  269. // CPU
  270. desktops.AddBranch("cpu", cpu =>
  271. {
  272. cpu.SetDescription("Manage CPUs attached to desktops.");
  273. cpu.AddCommand<DesktopCpuAddCommand>("add").WithDescription("Add a CPU to a desktop.");
  274. cpu.AddCommand<DesktopCpuSetCommand>("set").WithDescription("Update a desktop CPU.");
  275. cpu.AddCommand<DesktopCpuRemoveCommand>("del").WithDescription("Remove a CPU from a desktop.");
  276. });
  277. // Drives
  278. desktops.AddBranch("drive", drive =>
  279. {
  280. drive.SetDescription("Manage storage drives attached to desktops.");
  281. drive.AddCommand<DesktopDriveAddCommand>("add").WithDescription("Add a drive to a desktop.");
  282. drive.AddCommand<DesktopDriveSetCommand>("set").WithDescription("Update a desktop drive.");
  283. drive.AddCommand<DesktopDriveRemoveCommand>("del")
  284. .WithDescription("Remove a drive from a desktop.");
  285. });
  286. // GPUs
  287. desktops.AddBranch("gpu", gpu =>
  288. {
  289. gpu.SetDescription("Manage GPUs attached to desktops.");
  290. gpu.AddCommand<DesktopGpuAddCommand>("add").WithDescription("Add a GPU to a desktop.");
  291. gpu.AddCommand<DesktopGpuSetCommand>("set").WithDescription("Update a desktop GPU.");
  292. gpu.AddCommand<DesktopGpuRemoveCommand>("del").WithDescription("Remove a GPU from a desktop.");
  293. });
  294. // NICs
  295. desktops.AddBranch("nic", nic =>
  296. {
  297. nic.SetDescription("Manage network interface cards (NICs) for desktops.");
  298. nic.AddCommand<DesktopNicAddCommand>("add").WithDescription("Add a NIC to a desktop.");
  299. nic.AddCommand<DesktopNicSetCommand>("set").WithDescription("Update a desktop NIC.");
  300. nic.AddCommand<DesktopNicRemoveCommand>("del").WithDescription("Remove a NIC from a desktop.");
  301. });
  302. });
  303. // ----------------------------
  304. // Laptops
  305. // ----------------------------
  306. config.AddBranch("Laptops", Laptops =>
  307. {
  308. Laptops.SetDescription("Manage Laptop computers and their components.");
  309. // CRUD
  310. Laptops.AddCommand<LaptopAddCommand>("add").WithDescription("Add a new Laptop.");
  311. Laptops.AddCommand<LaptopGetCommand>("list").WithDescription("List all Laptops.");
  312. Laptops.AddCommand<LaptopGetByNameCommand>("get").WithDescription("Retrieve a Laptop by name.");
  313. Laptops.AddCommand<LaptopDescribeCommand>("describe")
  314. .WithDescription("Show detailed information about a Laptop.");
  315. Laptops.AddCommand<LaptopDeleteCommand>("del").WithDescription("Delete a Laptop from the inventory.");
  316. Laptops.AddCommand<LaptopReportCommand>("summary")
  317. .WithDescription("Show a summarized hardware report for all Laptops.");
  318. Laptops.AddCommand<LaptopTreeCommand>("tree")
  319. .WithDescription("Display the dependency tree for a Laptop.");
  320. // CPU
  321. Laptops.AddBranch("cpu", cpu =>
  322. {
  323. cpu.SetDescription("Manage CPUs attached to Laptops.");
  324. cpu.AddCommand<LaptopCpuAddCommand>("add").WithDescription("Add a CPU to a Laptop.");
  325. cpu.AddCommand<LaptopCpuSetCommand>("set").WithDescription("Update a Laptop CPU.");
  326. cpu.AddCommand<LaptopCpuRemoveCommand>("del").WithDescription("Remove a CPU from a Laptop.");
  327. });
  328. // Drives
  329. Laptops.AddBranch("drive", drive =>
  330. {
  331. drive.SetDescription("Manage storage drives attached to Laptops.");
  332. drive.AddCommand<LaptopDriveAddCommand>("add").WithDescription("Add a drive to a Laptop.");
  333. drive.AddCommand<LaptopDriveSetCommand>("set").WithDescription("Update a Laptop drive.");
  334. drive.AddCommand<LaptopDriveRemoveCommand>("del").WithDescription("Remove a drive from a Laptop.");
  335. });
  336. // GPUs
  337. Laptops.AddBranch("gpu", gpu =>
  338. {
  339. gpu.SetDescription("Manage GPUs attached to Laptops.");
  340. gpu.AddCommand<LaptopGpuAddCommand>("add").WithDescription("Add a GPU to a Laptop.");
  341. gpu.AddCommand<LaptopGpuSetCommand>("set").WithDescription("Update a Laptop GPU.");
  342. gpu.AddCommand<LaptopGpuRemoveCommand>("del").WithDescription("Remove a GPU from a Laptop.");
  343. });
  344. });
  345. // ----------------------------
  346. // Services
  347. // ----------------------------
  348. config.AddBranch("services", service =>
  349. {
  350. service.SetDescription("Manage services and their configurations.");
  351. service.AddCommand<ServiceReportCommand>("summary")
  352. .WithDescription("Show a summary report for all services.");
  353. service.AddCommand<ServiceAddCommand>("add").WithDescription("Add a new service.");
  354. service.AddCommand<ServiceGetCommand>("list").WithDescription("List all services.");
  355. service.AddCommand<ServiceGetByNameCommand>("get").WithDescription("Retrieve a service by name.");
  356. service.AddCommand<ServiceDescribeCommand>("describe")
  357. .WithDescription("Show detailed information about a service.");
  358. service.AddCommand<ServiceSetCommand>("set").WithDescription("Update properties of a service.");
  359. service.AddCommand<ServiceDeleteCommand>("del").WithDescription("Delete a service.");
  360. service.AddCommand<ServiceSubnetsCommand>("subnets")
  361. .WithDescription("List subnets associated with a service, optionally filtered by CIDR.");
  362. });
  363. });
  364. }
  365. private static int HandleException(Exception ex, ITypeResolver? arg2)
  366. {
  367. switch (ex)
  368. {
  369. case ValidationException ve:
  370. AnsiConsole.MarkupLine($"[yellow]Validation error:[/] {ve.Message}");
  371. return 2;
  372. case ConflictException ce:
  373. AnsiConsole.MarkupLine($"[red]Conflict:[/] {ce.Message}");
  374. return 3;
  375. case NotFoundException ne:
  376. AnsiConsole.MarkupLine($"[red]Not found:[/] {ne.Message}");
  377. return 4;
  378. default:
  379. AnsiConsole.MarkupLine("[red]Unexpected error occurred.[/]");
  380. AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);
  381. return 99;
  382. }
  383. }
  384. }