All files / src/commands/tokens tokens.command.js

44.18% Statements 19/43
100% Branches 1/1
0% Functions 0/1
44.18% Lines 19/43

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 44193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                 193x 193x  
import { listApiTokens } from '../../clever-client/auth-bridge.js';
import { config } from '../../config/config.js';
import { formatDate } from '../../lib/date-utils.js';
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { sendToAuthBridge } from '../../models/send-to-api.js';
import { humanJsonOutputFormatOption } from '../global.options.js';
 
export const tokensCommand = defineCommand({
  description: `Manage API tokens to query Clever Cloud API from ${config.AUTH_BRIDGE_HOST}`,
  since: '3.12.0',
  options: {
    format: humanJsonOutputFormatOption,
  },
  args: [],
  async handler(options) {
    const { format } = options;

    const tokens = await listApiTokens().then(sendToAuthBridge);

    if (format === 'json') {
      Logger.printJson(tokens);
    } else {
      if (tokens.length === 0) {
        Logger.println(`ℹ️  No API token found, create one with ${styleText('blue', 'clever tokens create')} command`);
      } else {
        console.table(
          tokens.map((token) => {
            return {
              'API token ID': token.apiTokenId,
              Name: token.name,
              'Creation IP address': token.ip,
              Creation: formatDate(token.creationDate),
              Expiration: formatDate(token.expirationDate),
              State: token.state,
            };
          }),
        );
      }
    }
  },
});