list.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.SSHListCommand = void 0;
  27. const utils_terminal_1 = require("@ionic/utils-terminal");
  28. const constants_1 = require("../../constants");
  29. const color_1 = require("../../lib/color");
  30. const base_1 = require("./base");
  31. class SSHListCommand extends base_1.SSHBaseCommand {
  32. async getMetadata() {
  33. return {
  34. name: 'list',
  35. type: 'global',
  36. summary: 'List your SSH public keys on Ionic',
  37. options: [
  38. {
  39. name: 'json',
  40. summary: 'Output SSH keys in JSON',
  41. type: Boolean,
  42. },
  43. ],
  44. groups: ["deprecated" /* MetadataGroup.DEPRECATED */],
  45. };
  46. }
  47. async preRun() {
  48. await this.checkForOpenSSH();
  49. }
  50. async run(inputs, options) {
  51. const { SSHKeyClient } = await Promise.resolve().then(() => __importStar(require('../../lib/ssh')));
  52. const { findHostSection, getConfigPath, isDirective, loadFromPath } = await Promise.resolve().then(() => __importStar(require('../../lib/ssh-config')));
  53. const { json } = options;
  54. const user = this.env.session.getUser();
  55. const token = await this.env.session.getUserToken();
  56. const sshkeyClient = new SSHKeyClient({ client: this.env.client, user, token });
  57. const paginator = sshkeyClient.paginate();
  58. const [r] = paginator;
  59. const res = await r;
  60. if (json) {
  61. process.stdout.write(JSON.stringify(res.data));
  62. }
  63. else {
  64. let activeFingerprint;
  65. let foundActiveKey = false;
  66. const sshConfigPath = getConfigPath();
  67. const conf = await loadFromPath(sshConfigPath);
  68. const section = findHostSection(conf, this.env.config.getGitHost());
  69. if (section && section.config) {
  70. const [identityFile] = section.config.filter(line => {
  71. return isDirective(line) && line.param === 'IdentityFile';
  72. });
  73. if (isDirective(identityFile)) {
  74. const output = await this.env.shell.output('ssh-keygen', ['-E', 'sha256', '-lf', identityFile.value], { fatalOnError: false });
  75. activeFingerprint = output.trim().split(' ')[1];
  76. }
  77. }
  78. if (res.data.length === 0) {
  79. this.env.log.warn(`No SSH keys found. Use ${(0, color_1.input)('ionic ssh add')} to add keys to Ionic.`);
  80. return;
  81. }
  82. const keysMatrix = res.data.map(sshkey => {
  83. const data = [sshkey.fingerprint, sshkey.name, sshkey.annotation];
  84. if (sshkey.fingerprint === activeFingerprint) {
  85. foundActiveKey = true;
  86. return data.map(v => (0, color_1.strong)(v));
  87. }
  88. return data;
  89. });
  90. const table = (0, utils_terminal_1.columnar)(keysMatrix, {
  91. ...constants_1.COLUMNAR_OPTIONS,
  92. headers: ['fingerprint', 'name', 'annotation'].map(h => (0, color_1.strong)(h)),
  93. });
  94. if (foundActiveKey) {
  95. this.env.log.nl();
  96. this.env.log.msg(`The row in ${(0, color_1.strong)('bold')} is the key that this computer is using. To change, use ${(0, color_1.input)('ionic ssh use')}.`);
  97. }
  98. this.env.log.nl();
  99. this.env.log.rawmsg(table);
  100. this.env.log.nl();
  101. this.env.log.ok(`Showing ${(0, color_1.strong)(String(res.data.length))} SSH key${res.data.length === 1 ? '' : 's'}.`);
  102. this.env.log.nl();
  103. }
  104. }
  105. }
  106. exports.SSHListCommand = SSHListCommand;