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

37.31% Statements 25/67
100% Branches 1/1
0% Functions 0/1
37.31% Lines 25/67

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                                     193x 193x  
import { getSecret as getOauthConsumerSecret } from '@clevercloud/client/esm/api/v2/oauth-consumer.js';
import { z } from 'zod';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { Logger } from '../../logger.js';
import { OAUTH_RIGHTS, resolveOauthConsumer } from '../../models/oauth-consumer.js';
import { sendToApi } from '../../models/send-to-api.js';
import { humanJsonOutputFormatOption } from '../global.options.js';
import { consumerKeyOrNameArg } from './oauth-consumers.args.js';
 
export const oauthConsumersGetCommand = defineCommand({
  description: 'Get details of an OAuth consumer',
  since: '4.8.0',
  options: {
    format: humanJsonOutputFormatOption,
    withSecret: defineOption({
      name: 'with-secret',
      schema: z.boolean().default(false),
      description: 'Include the consumer secret in the output',
    }),
  },
  args: [consumerKeyOrNameArg],
  async handler(options, keyOrName) {
    const { format, withSecret } = options;

    const oauthConsumer = await resolveOauthConsumer(keyOrName);

    const secret = withSecret
      ? await getOauthConsumerSecret({ id: oauthConsumer.ownerId, key: oauthConsumer.key })
          .then(sendToApi)
          .then((c) => c.secret)
      : null;

    switch (format) {
      case 'json': {
        Logger.printJson({ ...oauthConsumer, ...(secret != null && { secret }) });
        break;
      }
      case 'human':
      default: {
        const dataToPrint = {
          Key: oauthConsumer.key,
          Name: oauthConsumer.name || '(unnamed)',
          Description: oauthConsumer.description || '',
          URL: oauthConsumer.url || '',
          Picture: oauthConsumer.picture || '',
          'Base URL': oauthConsumer.baseUrl || '',
        };

        if (secret != null) {
          dataToPrint.Secret = secret;
        }

        console.table(dataToPrint);

        if (oauthConsumer.rights) {
          Logger.println('');
          const rightsData = {};
          for (const [apiName, cliName] of Object.entries(OAUTH_RIGHTS)) {
            rightsData[cliName] = oauthConsumer.rights[apiName] ?? false;
          }
          console.table(rightsData);
        }
      }
    }
  },
});