use.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.SSHUseCommand = void 0;
  27. const cli_framework_1 = require("@ionic/cli-framework");
  28. const utils_fs_1 = require("@ionic/utils-fs");
  29. const utils_terminal_1 = require("@ionic/utils-terminal");
  30. const color_1 = require("../../lib/color");
  31. const errors_1 = require("../../lib/errors");
  32. const base_1 = require("./base");
  33. class SSHUseCommand extends base_1.SSHBaseCommand {
  34. async getMetadata() {
  35. return {
  36. name: 'use',
  37. type: 'global',
  38. summary: 'Set your active Ionic SSH key',
  39. description: `
  40. This command modifies the SSH configuration file (${(0, color_1.strong)('~/.ssh/config')}) to set an active private key for the ${(0, color_1.strong)('git.ionicjs.com')} host. Read more about SSH configuration by running the ${(0, color_1.input)('man ssh_config')} command or by visiting online man pages[^ssh-config-docs].
  41. Before making changes, ${(0, color_1.input)('ionic ssh use')} will print a diff and ask for permission to write the file.
  42. `,
  43. footnotes: [
  44. {
  45. id: 'ssh-config-docs',
  46. url: 'https://linux.die.net/man/5/ssh_config',
  47. },
  48. ],
  49. inputs: [
  50. {
  51. name: 'key-path',
  52. summary: 'Location of private key file to use',
  53. validators: [cli_framework_1.validators.required],
  54. },
  55. ],
  56. groups: ["deprecated" /* MetadataGroup.DEPRECATED */],
  57. };
  58. }
  59. async run(inputs, options) {
  60. const { ERROR_SSH_INVALID_PRIVKEY, ERROR_SSH_MISSING_PRIVKEY, validatePrivateKey } = await Promise.resolve().then(() => __importStar(require('../../lib/ssh')));
  61. const { ensureHostAndKeyPath, getConfigPath } = await Promise.resolve().then(() => __importStar(require('../../lib/ssh-config')));
  62. const keyPath = (0, utils_terminal_1.expandPath)(inputs[0]);
  63. try {
  64. await validatePrivateKey(keyPath);
  65. }
  66. catch (e) {
  67. if (e === ERROR_SSH_MISSING_PRIVKEY) {
  68. throw new errors_1.FatalException(`${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))} does not appear to exist. Please specify a valid SSH private key.\n` +
  69. `If you are having issues, try using ${(0, color_1.input)('ionic ssh setup')}.`);
  70. }
  71. else if (e === ERROR_SSH_INVALID_PRIVKEY) {
  72. throw new errors_1.FatalException(`${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))} does not appear to be a valid SSH private key. (Missing '-----BEGIN RSA PRIVATE KEY-----' header.)\n` +
  73. `If you are having issues, try using ${(0, color_1.input)('ionic ssh setup')}.`);
  74. }
  75. else {
  76. throw e;
  77. }
  78. }
  79. const { SSHConfig } = await Promise.resolve().then(() => __importStar(require('../../lib/ssh-config')));
  80. const sshConfigPath = getConfigPath();
  81. const text1 = await (0, utils_fs_1.fileToString)(sshConfigPath);
  82. const conf = SSHConfig.parse(text1);
  83. ensureHostAndKeyPath(conf, { host: this.env.config.getGitHost(), port: this.env.config.getGitPort() }, keyPath);
  84. const text2 = SSHConfig.stringify(conf);
  85. if (text1 === text2) {
  86. this.env.log.msg(`${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))} is already your active SSH key.`);
  87. return;
  88. }
  89. else {
  90. const { diffPatch } = await Promise.resolve().then(() => __importStar(require('../../lib/diff')));
  91. const diff = await diffPatch(sshConfigPath, text1, text2);
  92. this.env.log.rawmsg(diff);
  93. const confirm = await this.env.prompt({
  94. type: 'confirm',
  95. name: 'confirm',
  96. message: `May we make the above change(s) to '${(0, utils_terminal_1.prettyPath)(sshConfigPath)}'?`,
  97. });
  98. if (!confirm) {
  99. // TODO: link to docs about manual git setup
  100. throw new errors_1.FatalException();
  101. }
  102. }
  103. await (0, utils_fs_1.writeFile)(sshConfigPath, text2, { encoding: 'utf8', mode: 0o600 });
  104. this.env.log.ok(`Your active Ionic SSH key has been set to ${(0, color_1.strong)(keyPath)}!`);
  105. }
  106. }
  107. exports.SSHUseCommand = SSHUseCommand;