CliBootstrap.cs 22 KB

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