CliBootstrap.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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.SetApplicationVersion("0.0.3");
  71. config.SetExceptionHandler(HandleException);
  72. // Global summary
  73. config.AddCommand<GetTotalSummaryCommand>("summary")
  74. .WithDescription("Show a summarized report of all resources in the system.");
  75. // ----------------------------
  76. // Server commands (CRUD-style)
  77. // ----------------------------
  78. config.AddBranch("servers", server =>
  79. {
  80. server.SetDescription("Manage servers and their components.");
  81. server.AddCommand<ServerReportCommand>("summary")
  82. .WithDescription("Show a summarized hardware report for all servers.");
  83. server.AddCommand<ServerAddCommand>("add").WithDescription("Add a new server to the inventory.");
  84. server.AddCommand<ServerGetByNameCommand>("get")
  85. .WithDescription("List all servers or retrieve a specific server by name.");
  86. server.AddCommand<ServerDescribeCommand>("describe")
  87. .WithDescription("Display detailed information about a specific server.");
  88. server.AddCommand<ServerSetCommand>("set").WithDescription("Update properties of an existing server.");
  89. server.AddCommand<ServerDeleteCommand>("del").WithDescription("Delete a server from the inventory.");
  90. server.AddCommand<ServerTreeCommand>("tree")
  91. .WithDescription("Display the dependency tree of a server.");
  92. // Server CPUs
  93. server.AddBranch("cpu", cpu =>
  94. {
  95. cpu.SetDescription("Manage CPUs attached to a server.");
  96. cpu.AddCommand<ServerCpuAddCommand>("add").WithDescription("Add a CPU to a specific server.");
  97. cpu.AddCommand<ServerCpuSetCommand>("set").WithDescription("Update configuration of a server CPU.");
  98. cpu.AddCommand<ServerCpuRemoveCommand>("del").WithDescription("Remove a CPU from a server.");
  99. });
  100. // Server Drives
  101. server.AddBranch("drive", drive =>
  102. {
  103. drive.SetDescription("Manage drives attached to a server.");
  104. drive.AddCommand<ServerDriveAddCommand>("add").WithDescription("Add a storage drive to a server.");
  105. drive.AddCommand<ServerDriveUpdateCommand>("set")
  106. .WithDescription("Update properties of a server drive.");
  107. drive.AddCommand<ServerDriveRemoveCommand>("del").WithDescription("Remove a drive from a server.");
  108. });
  109. // Server GPUs
  110. server.AddBranch("gpu", gpu =>
  111. {
  112. gpu.SetDescription("Manage GPUs attached to a server.");
  113. gpu.AddCommand<ServerGpuAddCommand>("add").WithDescription("Add a GPU to a server.");
  114. gpu.AddCommand<ServerGpuUpdateCommand>("set").WithDescription("Update properties of a server GPU.");
  115. gpu.AddCommand<ServerGpuRemoveCommand>("del").WithDescription("Remove a GPU from a server.");
  116. });
  117. // Server NICs
  118. server.AddBranch("nic", nic =>
  119. {
  120. nic.SetDescription("Manage network interface cards (NICs) for a server.");
  121. nic.AddCommand<ServerNicAddCommand>("add").WithDescription("Add a NIC to a server.");
  122. nic.AddCommand<ServerNicUpdateCommand>("set").WithDescription("Update properties of a server NIC.");
  123. nic.AddCommand<ServerNicRemoveCommand>("del").WithDescription("Remove a NIC from a server.");
  124. });
  125. });
  126. // ----------------------------
  127. // Switch commands
  128. // ----------------------------
  129. config.AddBranch("switches", switches =>
  130. {
  131. switches.SetDescription("Manage network switches.");
  132. switches.AddCommand<SwitchReportCommand>("summary")
  133. .WithDescription("Show a hardware report for all switches.");
  134. switches.AddCommand<SwitchAddCommand>("add")
  135. .WithDescription("Add a new network switch to the inventory.");
  136. switches.AddCommand<SwitchGetCommand>("list").WithDescription("List all switches in the system.");
  137. switches.AddCommand<SwitchGetByNameCommand>("get")
  138. .WithDescription("Retrieve details of a specific switch by name.");
  139. switches.AddCommand<SwitchDescribeCommand>("describe")
  140. .WithDescription("Show detailed information about a switch.");
  141. switches.AddCommand<SwitchSetCommand>("set").WithDescription("Update properties of a switch.");
  142. switches.AddCommand<SwitchDeleteCommand>("del").WithDescription("Delete a switch from the inventory.");
  143. switches.AddBranch("port", port =>
  144. {
  145. port.SetDescription("Manage ports on a network switch.");
  146. port.AddCommand<SwitchPortAddCommand>("add").WithDescription("Add a port to a switch.");
  147. port.AddCommand<SwitchPortUpdateCommand>("set").WithDescription("Update a switch port.");
  148. port.AddCommand<SwitchPortRemoveCommand>("del").WithDescription("Remove a port from a switch.");
  149. });
  150. });
  151. // ----------------------------
  152. // Routers commands
  153. // ----------------------------
  154. config.AddBranch("routers", routers =>
  155. {
  156. routers.SetDescription("Manage network routers.");
  157. routers.AddCommand<RouterReportCommand>("summary")
  158. .WithDescription("Show a hardware report for all routers.");
  159. routers.AddCommand<RouterAddCommand>("add")
  160. .WithDescription("Add a new network router to the inventory.");
  161. routers.AddCommand<RouterGetCommand>("list").WithDescription("List all routers in the system.");
  162. routers.AddCommand<RouterGetByNameCommand>("get")
  163. .WithDescription("Retrieve details of a specific router by name.");
  164. routers.AddCommand<RouterDescribeCommand>("describe")
  165. .WithDescription("Show detailed information about a router.");
  166. routers.AddCommand<RouterSetCommand>("set").WithDescription("Update properties of a router.");
  167. routers.AddCommand<RouterDeleteCommand>("del").WithDescription("Delete a router from the inventory.");
  168. routers.AddBranch("port", port =>
  169. {
  170. port.SetDescription("Manage ports on a router.");
  171. port.AddCommand<RouterPortAddCommand>("add").WithDescription("Add a port to a router.");
  172. port.AddCommand<RouterPortUpdateCommand>("set").WithDescription("Update a router port.");
  173. port.AddCommand<RouterPortRemoveCommand>("del").WithDescription("Remove a port from a router.");
  174. });
  175. });
  176. // ----------------------------
  177. // Firewalls commands
  178. // ----------------------------
  179. config.AddBranch("firewalls", firewalls =>
  180. {
  181. firewalls.SetDescription("Manage firewalls.");
  182. firewalls.AddCommand<FirewallReportCommand>("summary")
  183. .WithDescription("Show a hardware report for all firewalls.");
  184. firewalls.AddCommand<FirewallAddCommand>("add").WithDescription("Add a new firewall to the inventory.");
  185. firewalls.AddCommand<FirewallGetCommand>("list").WithDescription("List all firewalls in the system.");
  186. firewalls.AddCommand<FirewallGetByNameCommand>("get")
  187. .WithDescription("Retrieve details of a specific firewall by name.");
  188. firewalls.AddCommand<FirewallDescribeCommand>("describe")
  189. .WithDescription("Show detailed information about a firewall.");
  190. firewalls.AddCommand<FirewallSetCommand>("set").WithDescription("Update properties of a firewall.");
  191. firewalls.AddCommand<FirewallDeleteCommand>("del")
  192. .WithDescription("Delete a firewall from the inventory.");
  193. firewalls.AddBranch("port", port =>
  194. {
  195. port.SetDescription("Manage ports on a firewall.");
  196. port.AddCommand<FirewallPortAddCommand>("add").WithDescription("Add a port to a firewall.");
  197. port.AddCommand<FirewallPortUpdateCommand>("set").WithDescription("Update a firewall port.");
  198. port.AddCommand<FirewallPortRemoveCommand>("del").WithDescription("Remove a port from a firewall.");
  199. });
  200. });
  201. // ----------------------------
  202. // System commands
  203. // ----------------------------
  204. config.AddBranch("systems", system =>
  205. {
  206. system.SetDescription("Manage systems and their dependencies.");
  207. system.AddCommand<SystemReportCommand>("summary")
  208. .WithDescription("Show a summary report for all systems.");
  209. system.AddCommand<SystemAddCommand>("add").WithDescription("Add a new system to the inventory.");
  210. system.AddCommand<SystemGetCommand>("list").WithDescription("List all systems.");
  211. system.AddCommand<SystemGetByNameCommand>("get").WithDescription("Retrieve a system by name.");
  212. system.AddCommand<SystemDescribeCommand>("describe")
  213. .WithDescription("Display detailed information about a system.");
  214. system.AddCommand<SystemSetCommand>("set").WithDescription("Update properties of a system.");
  215. system.AddCommand<SystemDeleteCommand>("del").WithDescription("Delete a system from the inventory.");
  216. system.AddCommand<SystemTreeCommand>("tree")
  217. .WithDescription("Display the dependency tree for a system.");
  218. });
  219. // ----------------------------
  220. // Access Points
  221. // ----------------------------
  222. config.AddBranch("accesspoints", ap =>
  223. {
  224. ap.SetDescription("Manage access points.");
  225. ap.AddCommand<AccessPointReportCommand>("summary")
  226. .WithDescription("Show a hardware report for all access points.");
  227. ap.AddCommand<AccessPointAddCommand>("add").WithDescription("Add a new access point.");
  228. ap.AddCommand<AccessPointGetCommand>("list").WithDescription("List all access points.");
  229. ap.AddCommand<AccessPointGetByNameCommand>("get").WithDescription("Retrieve an access point by name.");
  230. ap.AddCommand<AccessPointDescribeCommand>("describe")
  231. .WithDescription("Show detailed information about an access point.");
  232. ap.AddCommand<AccessPointSetCommand>("set").WithDescription("Update properties of an access point.");
  233. ap.AddCommand<AccessPointDeleteCommand>("del").WithDescription("Delete an access point.");
  234. });
  235. // ----------------------------
  236. // UPS units
  237. // ----------------------------
  238. config.AddBranch("ups", ups =>
  239. {
  240. ups.SetDescription("Manage UPS units.");
  241. ups.AddCommand<UpsReportCommand>("summary")
  242. .WithDescription("Show a hardware report for all UPS units.");
  243. ups.AddCommand<UpsAddCommand>("add").WithDescription("Add a new UPS unit.");
  244. ups.AddCommand<UpsGetCommand>("list").WithDescription("List all UPS units.");
  245. ups.AddCommand<UpsGetByNameCommand>("get").WithDescription("Retrieve a UPS unit by name.");
  246. ups.AddCommand<UpsDescribeCommand>("describe")
  247. .WithDescription("Show detailed information about a UPS unit.");
  248. ups.AddCommand<UpsSetCommand>("set").WithDescription("Update properties of a UPS unit.");
  249. ups.AddCommand<UpsDeleteCommand>("del").WithDescription("Delete a UPS unit.");
  250. });
  251. // ----------------------------
  252. // Desktops
  253. // ----------------------------
  254. config.AddBranch("desktops", desktops =>
  255. {
  256. desktops.SetDescription("Manage desktop computers and their components.");
  257. // CRUD
  258. desktops.AddCommand<DesktopAddCommand>("add").WithDescription("Add a new desktop.");
  259. desktops.AddCommand<DesktopGetCommand>("list").WithDescription("List all desktops.");
  260. desktops.AddCommand<DesktopGetByNameCommand>("get").WithDescription("Retrieve a desktop by name.");
  261. desktops.AddCommand<DesktopDescribeCommand>("describe")
  262. .WithDescription("Show detailed information about a desktop.");
  263. desktops.AddCommand<DesktopSetCommand>("set").WithDescription("Update properties of a desktop.");
  264. desktops.AddCommand<DesktopDeleteCommand>("del")
  265. .WithDescription("Delete a desktop from the inventory.");
  266. desktops.AddCommand<DesktopReportCommand>("summary")
  267. .WithDescription("Show a summarized hardware report for all desktops.");
  268. desktops.AddCommand<DesktopTreeCommand>("tree")
  269. .WithDescription("Display the dependency tree for a desktop.");
  270. // CPU
  271. desktops.AddBranch("cpu", cpu =>
  272. {
  273. cpu.SetDescription("Manage CPUs attached to desktops.");
  274. cpu.AddCommand<DesktopCpuAddCommand>("add").WithDescription("Add a CPU to a desktop.");
  275. cpu.AddCommand<DesktopCpuSetCommand>("set").WithDescription("Update a desktop CPU.");
  276. cpu.AddCommand<DesktopCpuRemoveCommand>("del").WithDescription("Remove a CPU from a desktop.");
  277. });
  278. // Drives
  279. desktops.AddBranch("drive", drive =>
  280. {
  281. drive.SetDescription("Manage storage drives attached to desktops.");
  282. drive.AddCommand<DesktopDriveAddCommand>("add").WithDescription("Add a drive to a desktop.");
  283. drive.AddCommand<DesktopDriveSetCommand>("set").WithDescription("Update a desktop drive.");
  284. drive.AddCommand<DesktopDriveRemoveCommand>("del")
  285. .WithDescription("Remove a drive from a desktop.");
  286. });
  287. // GPUs
  288. desktops.AddBranch("gpu", gpu =>
  289. {
  290. gpu.SetDescription("Manage GPUs attached to desktops.");
  291. gpu.AddCommand<DesktopGpuAddCommand>("add").WithDescription("Add a GPU to a desktop.");
  292. gpu.AddCommand<DesktopGpuSetCommand>("set").WithDescription("Update a desktop GPU.");
  293. gpu.AddCommand<DesktopGpuRemoveCommand>("del").WithDescription("Remove a GPU from a desktop.");
  294. });
  295. // NICs
  296. desktops.AddBranch("nic", nic =>
  297. {
  298. nic.SetDescription("Manage network interface cards (NICs) for desktops.");
  299. nic.AddCommand<DesktopNicAddCommand>("add").WithDescription("Add a NIC to a desktop.");
  300. nic.AddCommand<DesktopNicSetCommand>("set").WithDescription("Update a desktop NIC.");
  301. nic.AddCommand<DesktopNicRemoveCommand>("del").WithDescription("Remove a NIC from a desktop.");
  302. });
  303. });
  304. // ----------------------------
  305. // Laptops
  306. // ----------------------------
  307. config.AddBranch("Laptops", Laptops =>
  308. {
  309. Laptops.SetDescription("Manage Laptop computers and their components.");
  310. // CRUD
  311. Laptops.AddCommand<LaptopAddCommand>("add").WithDescription("Add a new Laptop.");
  312. Laptops.AddCommand<LaptopGetCommand>("list").WithDescription("List all Laptops.");
  313. Laptops.AddCommand<LaptopGetByNameCommand>("get").WithDescription("Retrieve a Laptop by name.");
  314. Laptops.AddCommand<LaptopDescribeCommand>("describe")
  315. .WithDescription("Show detailed information about a Laptop.");
  316. Laptops.AddCommand<LaptopDeleteCommand>("del").WithDescription("Delete a Laptop from the inventory.");
  317. Laptops.AddCommand<LaptopReportCommand>("summary")
  318. .WithDescription("Show a summarized hardware report for all Laptops.");
  319. Laptops.AddCommand<LaptopTreeCommand>("tree")
  320. .WithDescription("Display the dependency tree for a Laptop.");
  321. // CPU
  322. Laptops.AddBranch("cpu", cpu =>
  323. {
  324. cpu.SetDescription("Manage CPUs attached to Laptops.");
  325. cpu.AddCommand<LaptopCpuAddCommand>("add").WithDescription("Add a CPU to a Laptop.");
  326. cpu.AddCommand<LaptopCpuSetCommand>("set").WithDescription("Update a Laptop CPU.");
  327. cpu.AddCommand<LaptopCpuRemoveCommand>("del").WithDescription("Remove a CPU from a Laptop.");
  328. });
  329. // Drives
  330. Laptops.AddBranch("drive", drive =>
  331. {
  332. drive.SetDescription("Manage storage drives attached to Laptops.");
  333. drive.AddCommand<LaptopDriveAddCommand>("add").WithDescription("Add a drive to a Laptop.");
  334. drive.AddCommand<LaptopDriveSetCommand>("set").WithDescription("Update a Laptop drive.");
  335. drive.AddCommand<LaptopDriveRemoveCommand>("del").WithDescription("Remove a drive from a Laptop.");
  336. });
  337. // GPUs
  338. Laptops.AddBranch("gpu", gpu =>
  339. {
  340. gpu.SetDescription("Manage GPUs attached to Laptops.");
  341. gpu.AddCommand<LaptopGpuAddCommand>("add").WithDescription("Add a GPU to a Laptop.");
  342. gpu.AddCommand<LaptopGpuSetCommand>("set").WithDescription("Update a Laptop GPU.");
  343. gpu.AddCommand<LaptopGpuRemoveCommand>("del").WithDescription("Remove a GPU from a Laptop.");
  344. });
  345. });
  346. // ----------------------------
  347. // Services
  348. // ----------------------------
  349. config.AddBranch("services", service =>
  350. {
  351. service.SetDescription("Manage services and their configurations.");
  352. service.AddCommand<ServiceReportCommand>("summary")
  353. .WithDescription("Show a summary report for all services.");
  354. service.AddCommand<ServiceAddCommand>("add").WithDescription("Add a new service.");
  355. service.AddCommand<ServiceGetCommand>("list").WithDescription("List all services.");
  356. service.AddCommand<ServiceGetByNameCommand>("get").WithDescription("Retrieve a service by name.");
  357. service.AddCommand<ServiceDescribeCommand>("describe")
  358. .WithDescription("Show detailed information about a service.");
  359. service.AddCommand<ServiceSetCommand>("set").WithDescription("Update properties of a service.");
  360. service.AddCommand<ServiceDeleteCommand>("del").WithDescription("Delete a service.");
  361. service.AddCommand<ServiceSubnetsCommand>("subnets")
  362. .WithDescription("List subnets associated with a service, optionally filtered by CIDR.");
  363. });
  364. });
  365. }
  366. private static int HandleException(Exception ex, ITypeResolver? arg2)
  367. {
  368. switch (ex)
  369. {
  370. case ValidationException ve:
  371. AnsiConsole.MarkupLine($"[yellow]Validation error:[/] {ve.Message}");
  372. return 2;
  373. case ConflictException ce:
  374. AnsiConsole.MarkupLine($"[red]Conflict:[/] {ce.Message}");
  375. return 3;
  376. case NotFoundException ne:
  377. AnsiConsole.MarkupLine($"[red]Not found:[/] {ne.Message}");
  378. return 4;
  379. default:
  380. AnsiConsole.MarkupLine("[red]Unexpected error occurred.[/]");
  381. AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);
  382. return 99;
  383. }
  384. }
  385. }