CliBootstrap.cs 40 KB

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