CliBootstrap.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. using System.ComponentModel.DataAnnotations;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using RackPeek.Domain;
  5. using RackPeek.Domain.Helpers;
  6. using RackPeek.Domain.Persistence;
  7. using RackPeek.Domain.Persistence.Yaml;
  8. using Shared.Rcl.Commands;
  9. using Shared.Rcl.Commands.AccessPoints;
  10. using Shared.Rcl.Commands.AccessPoints.Labels;
  11. using Shared.Rcl.Commands.AccessPoints.Rename;
  12. using Shared.Rcl.Commands.Connections;
  13. using Shared.Rcl.Commands.Desktops;
  14. using Shared.Rcl.Commands.Desktops.Cpus;
  15. using Shared.Rcl.Commands.Desktops.Drive;
  16. using Shared.Rcl.Commands.Desktops.Gpus;
  17. using Shared.Rcl.Commands.Desktops.Labels;
  18. using Shared.Rcl.Commands.Desktops.Nics;
  19. using Shared.Rcl.Commands.Desktops.Rename;
  20. using Shared.Rcl.Commands.Exporters;
  21. using Shared.Rcl.Commands.Firewalls;
  22. using Shared.Rcl.Commands.Firewalls.Labels;
  23. using Shared.Rcl.Commands.Firewalls.Ports;
  24. using Shared.Rcl.Commands.Firewalls.Rename;
  25. using Shared.Rcl.Commands.Laptops;
  26. using Shared.Rcl.Commands.Laptops.Cpus;
  27. using Shared.Rcl.Commands.Laptops.Drive;
  28. using Shared.Rcl.Commands.Laptops.Gpus;
  29. using Shared.Rcl.Commands.Laptops.Labels;
  30. using Shared.Rcl.Commands.Laptops.Rename;
  31. using Shared.Rcl.Commands.Routers;
  32. using Shared.Rcl.Commands.Routers.Labels;
  33. using Shared.Rcl.Commands.Routers.Ports;
  34. using Shared.Rcl.Commands.Routers.Rename;
  35. using Shared.Rcl.Commands.Servers;
  36. using Shared.Rcl.Commands.Servers.Cpus;
  37. using Shared.Rcl.Commands.Servers.Drives;
  38. using Shared.Rcl.Commands.Servers.Gpus;
  39. using Shared.Rcl.Commands.Servers.Labels;
  40. using Shared.Rcl.Commands.Servers.Nics;
  41. using Shared.Rcl.Commands.Servers.Rename;
  42. using Shared.Rcl.Commands.Services;
  43. using Shared.Rcl.Commands.Services.Labels;
  44. using Shared.Rcl.Commands.Services.Rename;
  45. using Shared.Rcl.Commands.Switches;
  46. using Shared.Rcl.Commands.Switches.Labels;
  47. using Shared.Rcl.Commands.Switches.Ports;
  48. using Shared.Rcl.Commands.Switches.Rename;
  49. using Shared.Rcl.Commands.Systems;
  50. using Shared.Rcl.Commands.Systems.Labels;
  51. using Shared.Rcl.Commands.Systems.Rename;
  52. using Shared.Rcl.Commands.Ups;
  53. using Shared.Rcl.Commands.Ups.Labels;
  54. using Shared.Rcl.Commands.Ups.Rename;
  55. using Spectre.Console;
  56. using Spectre.Console.Cli;
  57. namespace Shared.Rcl;
  58. public static class CliBootstrap {
  59. private static string[]? _lastArgs;
  60. private static CommandApp? _app;
  61. private static bool _showingHelp;
  62. public static void SetContext(string[] args, CommandApp app) {
  63. _lastArgs = args;
  64. _app = app;
  65. }
  66. public static async Task RegisterInternals(
  67. IServiceCollection services,
  68. IConfiguration configuration,
  69. string yamlDir,
  70. string yamlFile) {
  71. services.AddSingleton(configuration);
  72. var appBasePath = AppContext.BaseDirectory;
  73. var resolvedYamlDir = Path.IsPathRooted(yamlDir)
  74. ? yamlDir
  75. : Path.Combine(appBasePath, yamlDir);
  76. Directory.CreateDirectory(resolvedYamlDir);
  77. var fullYamlPath = Path.Combine(resolvedYamlDir, yamlFile);
  78. if (!File.Exists(fullYamlPath)) await File.WriteAllTextAsync(fullYamlPath, "");
  79. services.AddLogging();
  80. services.AddScoped<RackPeekConfigMigrationDeserializer>();
  81. services.AddScoped<IResourceYamlMigrationService, ResourceYamlMigrationService>();
  82. ServiceProvider b = services.BuildServiceProvider();
  83. var collection = new YamlResourceCollection(
  84. fullYamlPath,
  85. new PhysicalTextFileStore(),
  86. new ResourceCollection(),
  87. b.GetRequiredService<IResourceYamlMigrationService>());
  88. await collection.LoadAsync();
  89. services.AddSingleton<IResourceCollection>(collection);
  90. // Infrastructure
  91. services.AddYamlRepos();
  92. // Application
  93. services.AddUseCases();
  94. services.AddCommands();
  95. }
  96. public static void BuildApp(CommandApp app) {
  97. // Spectre bootstrap
  98. app.Configure(config => {
  99. config.SetApplicationName("rpk");
  100. config.ValidateExamples();
  101. config.SetApplicationVersion(RpkConstants.Version);
  102. config.SetExceptionHandler(HandleException);
  103. // Global summary
  104. config.AddCommand<GetTotalSummaryCommand>("summary")
  105. .WithDescription("Show a summarized report of all resources in the system.");
  106. // ----------------------------
  107. // Server commands (CRUD-style)
  108. // ----------------------------
  109. config.AddBranch("servers", server => {
  110. server.SetDescription("Manage servers and their components.");
  111. server.AddCommand<ServerReportCommand>("summary")
  112. .WithDescription("Show a summarized hardware report for all servers.");
  113. server.AddCommand<ServerAddCommand>("add").WithDescription("Add a new server to the inventory.");
  114. server.AddCommand<ServerGetByNameCommand>("get")
  115. .WithDescription("List all servers or retrieve a specific server by name.");
  116. server.AddCommand<ServerDescribeCommand>("describe")
  117. .WithDescription("Display detailed information about a specific server.");
  118. server.AddCommand<ServerSetCommand>("set").WithDescription("Update properties of an existing server.");
  119. server.AddCommand<ServerDeleteCommand>("del").WithDescription("Delete a server from the inventory.");
  120. server.AddCommand<ServerRenameCommand>("rename")
  121. .WithDescription("Rename a server to a new name.");
  122. server.AddCommand<ServerTreeCommand>("tree")
  123. .WithDescription("Display the dependency tree of a server.");
  124. // Server CPUs
  125. server.AddBranch("cpu", cpu => {
  126. cpu.SetDescription("Manage CPUs attached to a server.");
  127. cpu.AddCommand<ServerCpuAddCommand>("add").WithDescription("Add a CPU to a specific server.");
  128. cpu.AddCommand<ServerCpuSetCommand>("set").WithDescription("Update configuration of a server CPU.");
  129. cpu.AddCommand<ServerCpuRemoveCommand>("del").WithDescription("Remove a CPU from a server.");
  130. });
  131. // Server Drives
  132. server.AddBranch("drive", drive => {
  133. drive.SetDescription("Manage drives attached to a server.");
  134. drive.AddCommand<ServerDriveAddCommand>("add").WithDescription("Add a storage drive to a server.");
  135. drive.AddCommand<ServerDriveUpdateCommand>("set")
  136. .WithDescription("Update properties of a server drive.");
  137. drive.AddCommand<ServerDriveRemoveCommand>("del").WithDescription("Remove a drive from a server.");
  138. });
  139. // Server GPUs
  140. server.AddBranch("gpu", gpu => {
  141. gpu.SetDescription("Manage GPUs attached to a server.");
  142. gpu.AddCommand<ServerGpuAddCommand>("add").WithDescription("Add a GPU to a server.");
  143. gpu.AddCommand<ServerGpuUpdateCommand>("set").WithDescription("Update properties of a server GPU.");
  144. gpu.AddCommand<ServerGpuRemoveCommand>("del").WithDescription("Remove a GPU from a server.");
  145. });
  146. // Server NICs
  147. server.AddBranch("nic", nic => {
  148. nic.SetDescription("Manage network interface cards (NICs) for a server.");
  149. nic.AddCommand<ServerNicAddCommand>("add").WithDescription("Add a NIC to a server.");
  150. nic.AddCommand<ServerNicUpdateCommand>("set").WithDescription("Update properties of a server NIC.");
  151. nic.AddCommand<ServerNicRemoveCommand>("del").WithDescription("Remove a NIC from a server.");
  152. });
  153. // Server Labels
  154. server.AddBranch("label", label => {
  155. label.SetDescription("Manage labels on a server.");
  156. label.AddCommand<ServerLabelAddCommand>("add").WithDescription("Add a label to a server.");
  157. label.AddCommand<ServerLabelRemoveCommand>("remove")
  158. .WithDescription("Remove a label from a server.");
  159. });
  160. });
  161. // ----------------------------
  162. // Switch commands
  163. // ----------------------------
  164. config.AddBranch("switches", switches => {
  165. switches.SetDescription("Manage network switches.");
  166. switches.AddCommand<SwitchReportCommand>("summary")
  167. .WithDescription("Show a hardware report for all switches.");
  168. switches.AddCommand<SwitchAddCommand>("add")
  169. .WithDescription("Add a new network switch to the inventory.");
  170. switches.AddCommand<SwitchGetCommand>("list").WithDescription("List all switches in the system.");
  171. switches.AddCommand<SwitchGetByNameCommand>("get")
  172. .WithDescription("Retrieve details of a specific switch by name.");
  173. switches.AddCommand<SwitchDescribeCommand>("describe")
  174. .WithDescription("Show detailed information about a switch.");
  175. switches.AddCommand<SwitchSetCommand>("set").WithDescription("Update properties of a switch.");
  176. switches.AddCommand<SwitchDeleteCommand>("del").WithDescription("Delete a switch from the inventory.");
  177. switches.AddCommand<SwitchRenameCommand>("rename")
  178. .WithDescription("Rename a switch to a new name.");
  179. switches.AddBranch("port", port => {
  180. port.SetDescription("Manage ports on a network switch.");
  181. port.AddCommand<SwitchPortAddCommand>("add").WithDescription("Add a port to a switch.");
  182. port.AddCommand<SwitchPortUpdateCommand>("set").WithDescription("Update a switch port.");
  183. port.AddCommand<SwitchPortRemoveCommand>("del").WithDescription("Remove a port from a switch.");
  184. });
  185. switches.AddBranch("label", label => {
  186. label.SetDescription("Manage labels on a switch.");
  187. label.AddCommand<SwitchLabelAddCommand>("add").WithDescription("Add a label to a switch.");
  188. label.AddCommand<SwitchLabelRemoveCommand>("remove")
  189. .WithDescription("Remove a label from a switch.");
  190. });
  191. });
  192. // ----------------------------
  193. // Routers commands
  194. // ----------------------------
  195. config.AddBranch("routers", routers => {
  196. routers.SetDescription("Manage network routers.");
  197. routers.AddCommand<RouterReportCommand>("summary")
  198. .WithDescription("Show a hardware report for all routers.");
  199. routers.AddCommand<RouterAddCommand>("add")
  200. .WithDescription("Add a new network router to the inventory.");
  201. routers.AddCommand<RouterGetCommand>("list").WithDescription("List all routers in the system.");
  202. routers.AddCommand<RouterGetByNameCommand>("get")
  203. .WithDescription("Retrieve details of a specific router by name.");
  204. routers.AddCommand<RouterDescribeCommand>("describe")
  205. .WithDescription("Show detailed information about a router.");
  206. routers.AddCommand<RouterSetCommand>("set").WithDescription("Update properties of a router.");
  207. routers.AddCommand<RouterDeleteCommand>("del").WithDescription("Delete a router from the inventory.");
  208. routers.AddCommand<RouterRenameCommand>("rename")
  209. .WithDescription("Rename a router to a new name.");
  210. routers.AddBranch("port", port => {
  211. port.SetDescription("Manage ports on a router.");
  212. port.AddCommand<RouterPortAddCommand>("add").WithDescription("Add a port to a router.");
  213. port.AddCommand<RouterPortUpdateCommand>("set").WithDescription("Update a router port.");
  214. port.AddCommand<RouterPortRemoveCommand>("del").WithDescription("Remove a port from a router.");
  215. });
  216. routers.AddBranch("label", label => {
  217. label.SetDescription("Manage labels on a router.");
  218. label.AddCommand<RouterLabelAddCommand>("add").WithDescription("Add a label to a router.");
  219. label.AddCommand<RouterLabelRemoveCommand>("remove")
  220. .WithDescription("Remove a label from a router.");
  221. });
  222. });
  223. // ----------------------------
  224. // Firewalls commands
  225. // ----------------------------
  226. config.AddBranch("firewalls", firewalls => {
  227. firewalls.SetDescription("Manage firewalls.");
  228. firewalls.AddCommand<FirewallReportCommand>("summary")
  229. .WithDescription("Show a hardware report for all firewalls.");
  230. firewalls.AddCommand<FirewallAddCommand>("add").WithDescription("Add a new firewall to the inventory.");
  231. firewalls.AddCommand<FirewallGetCommand>("list").WithDescription("List all firewalls in the system.");
  232. firewalls.AddCommand<FirewallGetByNameCommand>("get")
  233. .WithDescription("Retrieve details of a specific firewall by name.");
  234. firewalls.AddCommand<FirewallDescribeCommand>("describe")
  235. .WithDescription("Show detailed information about a firewall.");
  236. firewalls.AddCommand<FirewallSetCommand>("set").WithDescription("Update properties of a firewall.");
  237. firewalls.AddCommand<FirewallDeleteCommand>("del")
  238. .WithDescription("Delete a firewall from the inventory.");
  239. firewalls.AddCommand<FirewallRenameCommand>("rename")
  240. .WithDescription("Rename a firewall to a new name.");
  241. firewalls.AddBranch("port", port => {
  242. port.SetDescription("Manage ports on a firewall.");
  243. port.AddCommand<FirewallPortAddCommand>("add").WithDescription("Add a port to a firewall.");
  244. port.AddCommand<FirewallPortUpdateCommand>("set").WithDescription("Update a firewall port.");
  245. port.AddCommand<FirewallPortRemoveCommand>("del").WithDescription("Remove a port from a firewall.");
  246. });
  247. firewalls.AddBranch("label", label => {
  248. label.SetDescription("Manage labels on a firewall.");
  249. label.AddCommand<FirewallLabelAddCommand>("add").WithDescription("Add a label to a firewall.");
  250. label.AddCommand<FirewallLabelRemoveCommand>("remove")
  251. .WithDescription("Remove a label from a firewall.");
  252. });
  253. });
  254. // ----------------------------
  255. // System commands
  256. // ----------------------------
  257. config.AddBranch("systems", system => {
  258. system.SetDescription("Manage systems and their dependencies.");
  259. system.AddCommand<SystemReportCommand>("summary")
  260. .WithDescription("Show a summary report for all systems.");
  261. system.AddCommand<SystemAddCommand>("add").WithDescription("Add a new system to the inventory.");
  262. system.AddCommand<SystemGetCommand>("list").WithDescription("List all systems.");
  263. system.AddCommand<SystemGetByNameCommand>("get").WithDescription("Retrieve a system by name.");
  264. system.AddCommand<SystemDescribeCommand>("describe")
  265. .WithDescription("Display detailed information about a system.");
  266. system.AddCommand<SystemSetCommand>("set").WithDescription("Update properties of a system.");
  267. system.AddCommand<SystemDeleteCommand>("del").WithDescription("Delete a system from the inventory.");
  268. system.AddCommand<SystemRenameCommand>("rename")
  269. .WithDescription("Rename a system to a new name.");
  270. system.AddCommand<SystemTreeCommand>("tree")
  271. .WithDescription("Display the dependency tree for a system.");
  272. system.AddBranch("label", label => {
  273. label.SetDescription("Manage labels on a system.");
  274. label.AddCommand<SystemLabelAddCommand>("add").WithDescription("Add a label to a system.");
  275. label.AddCommand<SystemLabelRemoveCommand>("remove")
  276. .WithDescription("Remove a label from a system.");
  277. });
  278. });
  279. // ----------------------------
  280. // Access Points
  281. // ----------------------------
  282. config.AddBranch("accesspoints", ap => {
  283. ap.SetDescription("Manage access points.");
  284. ap.AddCommand<AccessPointReportCommand>("summary")
  285. .WithDescription("Show a hardware report for all access points.");
  286. ap.AddCommand<AccessPointAddCommand>("add").WithDescription("Add a new access point.");
  287. ap.AddCommand<AccessPointGetCommand>("list").WithDescription("List all access points.");
  288. ap.AddCommand<AccessPointGetByNameCommand>("get").WithDescription("Retrieve an access point by name.");
  289. ap.AddCommand<AccessPointDescribeCommand>("describe")
  290. .WithDescription("Show detailed information about an access point.");
  291. ap.AddCommand<AccessPointSetCommand>("set").WithDescription("Update properties of an access point.");
  292. ap.AddCommand<AccessPointDeleteCommand>("del").WithDescription("Delete an access point.");
  293. ap.AddCommand<AccessPointRenameCommand>("rename")
  294. .WithDescription("Rename an access point to a new name.");
  295. ap.AddBranch("label", label => {
  296. label.SetDescription("Manage labels on an access point.");
  297. label.AddCommand<AccessPointLabelAddCommand>("add")
  298. .WithDescription("Add a label to an access point.");
  299. label.AddCommand<AccessPointLabelRemoveCommand>("remove")
  300. .WithDescription("Remove a label from an access point.");
  301. });
  302. });
  303. // ----------------------------
  304. // UPS units
  305. // ----------------------------
  306. config.AddBranch("ups", ups => {
  307. ups.SetDescription("Manage UPS units.");
  308. ups.AddCommand<UpsReportCommand>("summary")
  309. .WithDescription("Show a hardware report for all UPS units.");
  310. ups.AddCommand<UpsAddCommand>("add").WithDescription("Add a new UPS unit.");
  311. ups.AddCommand<UpsGetCommand>("list").WithDescription("List all UPS units.");
  312. ups.AddCommand<UpsGetByNameCommand>("get").WithDescription("Retrieve a UPS unit by name.");
  313. ups.AddCommand<UpsDescribeCommand>("describe")
  314. .WithDescription("Show detailed information about a UPS unit.");
  315. ups.AddCommand<UpsSetCommand>("set").WithDescription("Update properties of a UPS unit.");
  316. ups.AddCommand<UpsDeleteCommand>("del").WithDescription("Delete a UPS unit.");
  317. ups.AddCommand<UpsRenameCommand>("rename")
  318. .WithDescription("Rename a UPS unit to a new name.");
  319. ups.AddBranch("label", label => {
  320. label.SetDescription("Manage labels on a UPS unit.");
  321. label.AddCommand<UpsLabelAddCommand>("add").WithDescription("Add a label to a UPS unit.");
  322. label.AddCommand<UpsLabelRemoveCommand>("remove")
  323. .WithDescription("Remove a label from a UPS unit.");
  324. });
  325. });
  326. // ----------------------------
  327. // Desktops
  328. // ----------------------------
  329. config.AddBranch("desktops", desktops => {
  330. desktops.SetDescription("Manage desktop computers and their components.");
  331. // CRUD
  332. desktops.AddCommand<DesktopAddCommand>("add").WithDescription("Add a new desktop.");
  333. desktops.AddCommand<DesktopGetCommand>("list").WithDescription("List all desktops.");
  334. desktops.AddCommand<DesktopGetByNameCommand>("get").WithDescription("Retrieve a desktop by name.");
  335. desktops.AddCommand<DesktopDescribeCommand>("describe")
  336. .WithDescription("Show detailed information about a desktop.");
  337. desktops.AddCommand<DesktopSetCommand>("set").WithDescription("Update properties of a desktop.");
  338. desktops.AddCommand<DesktopDeleteCommand>("del")
  339. .WithDescription("Delete a desktop from the inventory.");
  340. desktops.AddCommand<DesktopRenameCommand>("rename")
  341. .WithDescription("Rename a desktop to a new name.");
  342. desktops.AddCommand<DesktopReportCommand>("summary")
  343. .WithDescription("Show a summarized hardware report for all desktops.");
  344. desktops.AddCommand<DesktopTreeCommand>("tree")
  345. .WithDescription("Display the dependency tree for a desktop.");
  346. // CPU
  347. desktops.AddBranch("cpu", cpu => {
  348. cpu.SetDescription("Manage CPUs attached to desktops.");
  349. cpu.AddCommand<DesktopCpuAddCommand>("add").WithDescription("Add a CPU to a desktop.");
  350. cpu.AddCommand<DesktopCpuSetCommand>("set").WithDescription("Update a desktop CPU.");
  351. cpu.AddCommand<DesktopCpuRemoveCommand>("del").WithDescription("Remove a CPU from a desktop.");
  352. });
  353. // Drives
  354. desktops.AddBranch("drive", drive => {
  355. drive.SetDescription("Manage storage drives attached to desktops.");
  356. drive.AddCommand<DesktopDriveAddCommand>("add").WithDescription("Add a drive to a desktop.");
  357. drive.AddCommand<DesktopDriveSetCommand>("set").WithDescription("Update a desktop drive.");
  358. drive.AddCommand<DesktopDriveRemoveCommand>("del")
  359. .WithDescription("Remove a drive from a desktop.");
  360. });
  361. // GPUs
  362. desktops.AddBranch("gpu", gpu => {
  363. gpu.SetDescription("Manage GPUs attached to desktops.");
  364. gpu.AddCommand<DesktopGpuAddCommand>("add").WithDescription("Add a GPU to a desktop.");
  365. gpu.AddCommand<DesktopGpuSetCommand>("set").WithDescription("Update a desktop GPU.");
  366. gpu.AddCommand<DesktopGpuRemoveCommand>("del").WithDescription("Remove a GPU from a desktop.");
  367. });
  368. // NICs
  369. desktops.AddBranch("nic", nic => {
  370. nic.SetDescription("Manage network interface cards (NICs) for desktops.");
  371. nic.AddCommand<DesktopNicAddCommand>("add").WithDescription("Add a NIC to a desktop.");
  372. nic.AddCommand<DesktopNicSetCommand>("set").WithDescription("Update a desktop NIC.");
  373. nic.AddCommand<DesktopNicRemoveCommand>("del").WithDescription("Remove a NIC from a desktop.");
  374. });
  375. desktops.AddBranch("label", label => {
  376. label.SetDescription("Manage labels on a desktop.");
  377. label.AddCommand<DesktopLabelAddCommand>("add").WithDescription("Add a label to a desktop.");
  378. label.AddCommand<DesktopLabelRemoveCommand>("remove")
  379. .WithDescription("Remove a label from a desktop.");
  380. });
  381. });
  382. // ----------------------------
  383. // Laptops
  384. // ----------------------------
  385. config.AddBranch("laptops", laptops => {
  386. laptops.SetDescription("Manage Laptop computers and their components.");
  387. // CRUD
  388. laptops.AddCommand<LaptopAddCommand>("add").WithDescription("Add a new Laptop.");
  389. laptops.AddCommand<LaptopGetCommand>("list").WithDescription("List all Laptops.");
  390. laptops.AddCommand<LaptopGetByNameCommand>("get").WithDescription("Retrieve a Laptop by name.");
  391. laptops.AddCommand<LaptopDescribeCommand>("describe")
  392. .WithDescription("Show detailed information about a Laptop.");
  393. laptops.AddCommand<LaptopSetCommand>("set").WithDescription("Update properties of a laptop.");
  394. laptops.AddCommand<LaptopDeleteCommand>("del").WithDescription("Delete a Laptop from the inventory.");
  395. laptops.AddCommand<LaptopRenameCommand>("rename")
  396. .WithDescription("Rename a Laptop to a new name.");
  397. laptops.AddCommand<LaptopReportCommand>("summary")
  398. .WithDescription("Show a summarized hardware report for all Laptops.");
  399. laptops.AddCommand<LaptopTreeCommand>("tree")
  400. .WithDescription("Display the dependency tree for a Laptop.");
  401. // CPU
  402. laptops.AddBranch("cpu", cpu => {
  403. cpu.SetDescription("Manage CPUs attached to Laptops.");
  404. cpu.AddCommand<LaptopCpuAddCommand>("add").WithDescription("Add a CPU to a Laptop.");
  405. cpu.AddCommand<LaptopCpuSetCommand>("set").WithDescription("Update a Laptop CPU.");
  406. cpu.AddCommand<LaptopCpuRemoveCommand>("del").WithDescription("Remove a CPU from a Laptop.");
  407. });
  408. // Drives
  409. laptops.AddBranch("drives", drives => {
  410. drives.SetDescription("Manage storage drives attached to Laptops.");
  411. drives.AddCommand<LaptopDriveAddCommand>("add").WithDescription("Add a drive to a Laptop.");
  412. drives.AddCommand<LaptopDriveSetCommand>("set").WithDescription("Update a Laptop drive.");
  413. drives.AddCommand<LaptopDriveRemoveCommand>("del").WithDescription("Remove a drive from a Laptop.");
  414. });
  415. // GPUs
  416. laptops.AddBranch("gpu", gpu => {
  417. gpu.SetDescription("Manage GPUs attached to Laptops.");
  418. gpu.AddCommand<LaptopGpuAddCommand>("add").WithDescription("Add a GPU to a Laptop.");
  419. gpu.AddCommand<LaptopGpuSetCommand>("set").WithDescription("Update a Laptop GPU.");
  420. gpu.AddCommand<LaptopGpuRemoveCommand>("del").WithDescription("Remove a GPU from a Laptop.");
  421. });
  422. laptops.AddBranch("label", label => {
  423. label.SetDescription("Manage labels on a laptop.");
  424. label.AddCommand<LaptopLabelAddCommand>("add").WithDescription("Add a label to a laptop.");
  425. label.AddCommand<LaptopLabelRemoveCommand>("remove")
  426. .WithDescription("Remove a label from a laptop.");
  427. });
  428. });
  429. // ----------------------------
  430. // Services
  431. // ----------------------------
  432. config.AddBranch("services", service => {
  433. service.SetDescription("Manage services and their configurations.");
  434. service.AddCommand<ServiceReportCommand>("summary")
  435. .WithDescription("Show a summary report for all services.");
  436. service.AddCommand<ServiceAddCommand>("add").WithDescription("Add a new service.");
  437. service.AddCommand<ServiceGetCommand>("list").WithDescription("List all services.");
  438. service.AddCommand<ServiceGetByNameCommand>("get").WithDescription("Retrieve a service by name.");
  439. service.AddCommand<ServiceDescribeCommand>("describe")
  440. .WithDescription("Show detailed information about a service.");
  441. service.AddCommand<ServiceSetCommand>("set").WithDescription("Update properties of a service.");
  442. service.AddCommand<ServiceDeleteCommand>("del").WithDescription("Delete a service.");
  443. service.AddCommand<ServiceRenameCommand>("rename")
  444. .WithDescription("Rename a service to a new name.");
  445. service.AddCommand<ServiceSubnetsCommand>("subnets")
  446. .WithDescription("List subnets associated with a service, optionally filtered by CIDR.");
  447. service.AddBranch("label", label => {
  448. label.SetDescription("Manage labels on a service.");
  449. label.AddCommand<ServiceLabelAddCommand>("add").WithDescription("Add a label to a service.");
  450. label.AddCommand<ServiceLabelRemoveCommand>("remove")
  451. .WithDescription("Remove a label from a service.");
  452. });
  453. });
  454. // ----------------------------
  455. // Ansible
  456. // ----------------------------
  457. config.AddBranch("ansible", ansible => {
  458. ansible.SetDescription("Generate and manage Ansible inventory.");
  459. ansible.AddCommand<GenerateAnsibleInventoryCommand>("inventory")
  460. .WithDescription("Generate an Ansible inventory.");
  461. });
  462. config.AddBranch("ssh", ssh => {
  463. ssh.SetDescription("Generate SSH configuration from infrastructure.");
  464. ssh.AddCommand<GenerateSshConfigCommand>("export")
  465. .WithDescription("Generate an SSH config file.");
  466. });
  467. config.AddBranch("hosts", hosts => {
  468. hosts.SetDescription("Generate a hosts file from infrastructure.");
  469. hosts.AddCommand<GenerateHostsFileCommand>("export")
  470. .WithDescription("Generate a /etc/hosts compatible file.");
  471. });
  472. config.AddBranch("connections", connections => {
  473. connections.SetDescription("Manage physical or logical port connections.");
  474. connections.AddCommand<ConnectionAddCommand>("add")
  475. .WithDescription("Create a connection between two ports.");
  476. connections.AddCommand<ConnectionRemoveCommand>("remove")
  477. .WithDescription("Remove the connection from a specific port.");
  478. });
  479. });
  480. }
  481. private static int HandleException(Exception ex, ITypeResolver? arg2) {
  482. switch (ex) {
  483. case ValidationException ve:
  484. AnsiConsole.MarkupLine($"[yellow]Validation error:[/] {ve.Message}");
  485. return 2;
  486. case ConflictException ce:
  487. AnsiConsole.MarkupLine($"[red]Conflict:[/] {ce.Message}");
  488. return 3;
  489. case NotFoundException ne:
  490. AnsiConsole.MarkupLine($"[red]Not found:[/] {ne.Message}");
  491. return 4;
  492. case CommandParseException pe:
  493. if (_showingHelp) return 1; // suppress errors during help lookup
  494. AnsiConsole.MarkupLine($"[red]Invalid command:[/] {pe.Message}");
  495. if (pe.Pretty != null) AnsiConsole.Write(pe.Pretty);
  496. ShowContextualHelp();
  497. return 1;
  498. case CommandRuntimeException re:
  499. if (_showingHelp) return 1;
  500. AnsiConsole.MarkupLine($"[red]Error:[/] {re.Message}");
  501. if (re.Pretty != null) AnsiConsole.Write(re.Pretty);
  502. ShowContextualHelp();
  503. return 1;
  504. default:
  505. AnsiConsole.MarkupLine("[red]Unexpected error occurred.[/]");
  506. AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);
  507. return 99;
  508. }
  509. }
  510. private static void ShowContextualHelp() {
  511. if (_lastArgs == null || _app == null || _showingHelp) return;
  512. _showingHelp = true;
  513. try {
  514. // Extract command path (args before any --flags)
  515. var commandPath = _lastArgs.TakeWhile(a => !a.StartsWith("-")).ToList();
  516. // Try progressively shorter command paths until --help succeeds
  517. while (commandPath.Count > 0) {
  518. var helpArgs = commandPath.Append("--help").ToArray();
  519. AnsiConsole.WriteLine();
  520. var result = _app.Run(helpArgs);
  521. if (result == 0) return;
  522. commandPath.RemoveAt(commandPath.Count - 1);
  523. }
  524. }
  525. finally {
  526. _showingHelp = false;
  527. }
  528. }
  529. }