All files / src/commands/ssh-keys ssh-keys.command.js

34% Statements 17/50
100% Branches 1/1
0% Functions 0/1
34% Lines 17/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                   193x 193x  
import dedent from 'dedent';
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { getUserSshKeys } from '../../models/ssh-keys.js';
import { humanJsonOutputFormatOption } from '../global.options.js';
 
export const sshKeysCommand = defineCommand({
  description: 'Manage SSH keys of the current user',
  since: '3.13.0',
  options: {
    format: humanJsonOutputFormatOption,
  },
  args: [],
  async handler(options) {
    const { format } = options;

    const keys = await getUserSshKeys();

    switch (format) {
      case 'json': {
        Logger.printJson(keys);
        break;
      }
      case 'human':
      default: {
        if (keys.length === 0) {
          Logger.println(dedent`
              ${styleText('blue', '🔐 No SSH keys')}
              
              To list the SSH keys on your local system, use the following command:
              ${styleText('grey', 'ssh-add -l -E sha256')}
              
              To create a new key pair, use the following command:
              ${styleText('grey', 'ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_clever -C "An optional comment"')}
              
              Then add the public key to your Clever Cloud account:
              ${styleText('grey', 'clever ssh-keys add myNewKey ~/.ssh/id_ed25519_clever.pub')}
            `);
          return;
        }

        Logger.println(`🔐 ${keys.length} SSH key(s):`);
        keys.forEach((key) => {
          Logger.println(` • ${styleText('blue', key.name)}`, styleText('grey', `(${key.fingerprint})`));
        });
      }
    }
  },
});