All files / src/commands/oauth-consumers oauth-consumers.list.command.js

44.11% Statements 15/34
100% Branches 1/1
0% Functions 0/1
44.11% Lines 15/34

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 35193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                       193x 193x  
import { defineCommand } from '../../lib/define-command.js';
import { printItemsByOwner } from '../../lib/print-items-by-owner.js';
import { Logger } from '../../logger.js';
import { getAllConsumers } from '../../models/oauth-consumer.js';
import { humanJsonOutputFormatOption } from '../global.options.js';
 
export const oauthConsumersListCommand = defineCommand({
  description: 'List OAuth consumers',
  since: '4.8.0',
  options: {
    format: humanJsonOutputFormatOption,
  },
  async handler(options) {
    const { format } = options;
    const oauthConsumers = await getAllConsumers();

    switch (format) {
      case 'json': {
        const consumersPerOwner = Object.groupBy(oauthConsumers, (c) => c.ownerId);
        Logger.printJson(consumersPerOwner);
        break;
      }
      case 'human':
      default: {
        printItemsByOwner(oauthConsumers, {
          itemName: 'OAuth consumer',
          emptyCommand: 'clever oauth-consumers create',
          getItemId: (c) => c.key,
          getExtraLines: (c) => (c.url ? `URL: ${c.url}` : null),
        });
      }
    }
  },
});