1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const format_1 = require("@ionic/cli-framework/utils/format");
|
4 | const color_1 = require("../../lib/color");
|
5 | const base_1 = require("./base");
|
6 | class SSHListCommand extends base_1.SSHBaseCommand {
|
7 | async getMetadata() {
|
8 | return {
|
9 | name: 'list',
|
10 | type: 'global',
|
11 | summary: 'List your SSH public keys on Ionic',
|
12 | options: [
|
13 | {
|
14 | name: 'json',
|
15 | summary: 'Output SSH keys in JSON',
|
16 | type: Boolean,
|
17 | },
|
18 | ],
|
19 | };
|
20 | }
|
21 | async preRun() {
|
22 | await this.checkForOpenSSH();
|
23 | }
|
24 | async run(inputs, options) {
|
25 | const { SSHKeyClient } = await Promise.resolve().then(() => require('../../lib/ssh'));
|
26 | const { findHostSection, getConfigPath, isDirective, loadFromPath } = await Promise.resolve().then(() => require('../../lib/ssh-config'));
|
27 | const { json } = options;
|
28 | const user = this.env.session.getUser();
|
29 | const token = this.env.session.getUserToken();
|
30 | const sshkeyClient = new SSHKeyClient({ client: this.env.client, user, token });
|
31 | const paginator = sshkeyClient.paginate();
|
32 | const [r] = paginator;
|
33 | const res = await r;
|
34 | if (json) {
|
35 | process.stdout.write(JSON.stringify(res.data));
|
36 | }
|
37 | else {
|
38 | let activeFingerprint;
|
39 | let foundActiveKey = false;
|
40 | const sshConfigPath = getConfigPath();
|
41 | const conf = await loadFromPath(sshConfigPath);
|
42 | const section = findHostSection(conf, this.env.config.getGitHost());
|
43 | if (section && section.config) {
|
44 | const [identityFile] = section.config.filter(line => {
|
45 | return isDirective(line) && line.param === 'IdentityFile';
|
46 | });
|
47 | if (isDirective(identityFile)) {
|
48 | const output = await this.env.shell.output('ssh-keygen', ['-E', 'sha256', '-lf', identityFile.value], { fatalOnError: false });
|
49 | activeFingerprint = output.trim().split(' ')[1];
|
50 | }
|
51 | }
|
52 | if (res.data.length === 0) {
|
53 | this.env.log.warn(`No SSH keys found. Use ${color_1.input('ionic ssh add')} to add keys to Ionic.`);
|
54 | return;
|
55 | }
|
56 | const keysMatrix = res.data.map(sshkey => {
|
57 | const data = [sshkey.fingerprint, sshkey.name, sshkey.annotation];
|
58 | if (sshkey.fingerprint === activeFingerprint) {
|
59 | foundActiveKey = true;
|
60 | return data.map(v => color_1.strong(v));
|
61 | }
|
62 | return data;
|
63 | });
|
64 | const table = format_1.columnar(keysMatrix, {
|
65 | headers: ['fingerprint', 'name', 'annotation'],
|
66 | });
|
67 | if (foundActiveKey) {
|
68 | this.env.log.nl();
|
69 | this.env.log.msg(`The row in ${color_1.strong('bold')} is the key that this computer is using. To change, use ${color_1.input('ionic ssh use')}.`);
|
70 | }
|
71 | this.env.log.nl();
|
72 | this.env.log.rawmsg(table);
|
73 | this.env.log.nl();
|
74 | this.env.log.ok(`Showing ${color_1.strong(String(res.data.length))} SSH key${res.data.length === 1 ? '' : 's'}.`);
|
75 | this.env.log.nl();
|
76 | }
|
77 | }
|
78 | }
|
79 | exports.SSHListCommand = SSHListCommand;
|