CliBootstrap.cs 30 KB

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