1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const cli_framework_1 = require("@ionic/cli-framework");
|
4 | const format_1 = require("@ionic/cli-framework/utils/format");
|
5 | const utils_fs_1 = require("@ionic/utils-fs");
|
6 | const color_1 = require("../../lib/color");
|
7 | const errors_1 = require("../../lib/errors");
|
8 | const base_1 = require("./base");
|
9 | class SSHUseCommand extends base_1.SSHBaseCommand {
|
10 | async getMetadata() {
|
11 | return {
|
12 | name: 'use',
|
13 | type: 'global',
|
14 | summary: 'Set your active Ionic SSH key',
|
15 | description: `
|
16 | This command modifies the SSH configuration file (${color_1.strong('~/.ssh/config')}) to set an active private key for the ${color_1.strong('git.ionicjs.com')} host. Read more about SSH configuration by running the ${color_1.input('man ssh_config')} command or by visiting online man pages[^ssh-config-docs].
|
17 |
|
18 | Before making changes, ${color_1.input('ionic ssh use')} will print a diff and ask for permission to write the file.
|
19 | `,
|
20 | footnotes: [
|
21 | {
|
22 | id: 'ssh-config-docs',
|
23 | url: 'https://linux.die.net/man/5/ssh_config',
|
24 | },
|
25 | ],
|
26 | inputs: [
|
27 | {
|
28 | name: 'key-path',
|
29 | summary: 'Location of private key file to use',
|
30 | validators: [cli_framework_1.validators.required],
|
31 | },
|
32 | ],
|
33 | };
|
34 | }
|
35 | async run(inputs, options) {
|
36 | const { ERROR_SSH_INVALID_PRIVKEY, ERROR_SSH_MISSING_PRIVKEY, validatePrivateKey } = await Promise.resolve().then(() => require('../../lib/ssh'));
|
37 | const { ensureHostAndKeyPath, getConfigPath } = await Promise.resolve().then(() => require('../../lib/ssh-config'));
|
38 | const keyPath = format_1.expandPath(inputs[0]);
|
39 | try {
|
40 | await validatePrivateKey(keyPath);
|
41 | }
|
42 | catch (e) {
|
43 | if (e === ERROR_SSH_MISSING_PRIVKEY) {
|
44 | throw new errors_1.FatalException(`${color_1.strong(format_1.prettyPath(keyPath))} does not appear to exist. Please specify a valid SSH private key.\n` +
|
45 | `If you are having issues, try using ${color_1.input('ionic ssh setup')}.`);
|
46 | }
|
47 | else if (e === ERROR_SSH_INVALID_PRIVKEY) {
|
48 | throw new errors_1.FatalException(`${color_1.strong(format_1.prettyPath(keyPath))} does not appear to be a valid SSH private key. (Missing '-----BEGIN RSA PRIVATE KEY-----' header.)\n` +
|
49 | `If you are having issues, try using ${color_1.input('ionic ssh setup')}.`);
|
50 | }
|
51 | else {
|
52 | throw e;
|
53 | }
|
54 | }
|
55 | const { SSHConfig } = await Promise.resolve().then(() => require('../../lib/ssh-config'));
|
56 | const sshConfigPath = getConfigPath();
|
57 | const text1 = await utils_fs_1.fileToString(sshConfigPath);
|
58 | const conf = SSHConfig.parse(text1);
|
59 | ensureHostAndKeyPath(conf, { host: this.env.config.getGitHost(), port: this.env.config.getGitPort() }, keyPath);
|
60 | const text2 = SSHConfig.stringify(conf);
|
61 | if (text1 === text2) {
|
62 | this.env.log.msg(`${color_1.strong(format_1.prettyPath(keyPath))} is already your active SSH key.`);
|
63 | return;
|
64 | }
|
65 | else {
|
66 | const { diffPatch } = await Promise.resolve().then(() => require('../../lib/diff'));
|
67 | const diff = await diffPatch(sshConfigPath, text1, text2);
|
68 | this.env.log.rawmsg(diff);
|
69 | const confirm = await this.env.prompt({
|
70 | type: 'confirm',
|
71 | name: 'confirm',
|
72 | message: `May we make the above change(s) to '${format_1.prettyPath(sshConfigPath)}'?`,
|
73 | });
|
74 | if (!confirm) {
|
75 |
|
76 | throw new errors_1.FatalException();
|
77 | }
|
78 | }
|
79 | await utils_fs_1.writeFile(sshConfigPath, text2, { encoding: 'utf8', mode: 0o600 });
|
80 | this.env.log.ok(`Your active Ionic SSH key has been set to ${color_1.strong(keyPath)}!`);
|
81 | }
|
82 | }
|
83 | exports.SSHUseCommand = SSHUseCommand;
|