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

61.53% Statements 16/26
100% Branches 1/1
0% Functions 0/1
61.53% Lines 16/26

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 27193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                     193x 193x  
import { todo_removeSshKey as removeSshKey } from '@clevercloud/client/esm/api/v2/user.js';
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { sendToApi } from '../../models/send-to-api.js';
import { getUserSshKeys } from '../../models/ssh-keys.js';
import { sshKeyNameArg } from './ssh-keys.args.js';
 
export const sshKeysRemoveCommand = defineCommand({
  description: 'Remove a SSH key from the current user',
  since: '3.13.0',
  options: {},
  args: [sshKeyNameArg],
  async handler(_options, keyName) {
    const keys = await getUserSshKeys();

    if (keys.find((key) => key.name === keyName) == null) {
      throw new Error(`SSH key ${styleText('red', keyName)} not found`);
    }

    const keyNameEncoded = encodeURIComponent(keyName);
    await removeSshKey({ key: keyNameEncoded }).then(sendToApi);

    Logger.printSuccess(`SSH key ${keyName} removed successfully`);
  },
});