CliBootstrap.cs 43 KB

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