CliBootstrap.cs 31 KB

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