CliBootstrap.cs 45 KB

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