CliBootstrap.cs 30 KB

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