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

54.68% Statements 35/64
100% Branches 1/1
0% Functions 0/1
54.68% Lines 35/64

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 65193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 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 { create } from '@clevercloud/client/esm/api/v2/oauth-consumer.js';
import { z } from 'zod';
import { defineArgument } from '../../lib/define-argument.js';
import { defineCommand } from '../../lib/define-command.js';
import { promptTextOption } from '../../lib/prompts.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { getOwnerIdFromOrgIdOrName } from '../../models/ids-resolver.js';
import { promptRights, rightsFromList } from '../../models/oauth-consumer.js';
import { sendToApi } from '../../models/send-to-api.js';
import { humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
import { baseUrlOption, descriptionOption, pictureOption, rightsOption, urlOption } from './oauth-consumers.options.js';
 
export const oauthConsumersCreateCommand = defineCommand({
  description: 'Create an OAuth consumer',
  since: '4.8.0',
  options: {
    org: orgaIdOrNameOption,
    description: descriptionOption,
    url: urlOption,
    picture: pictureOption,
    baseUrl: baseUrlOption,
    rights: rightsOption,
    format: humanJsonOutputFormatOption,
  },
  args: [
    defineArgument({
      schema: z.string(),
      description: 'Consumer name',
      placeholder: 'name',
    }),
  ],
  async handler(options, name) {
    const { org, description, url, picture, baseUrl, rights, format } = options;

    const ownerId = await getOwnerIdFromOrgIdOrName(org);

    const body = {
      name,
      description: description ?? (await promptTextOption(descriptionOption)),
      url: url ?? (await promptTextOption(urlOption)),
      picture: picture ?? (await promptTextOption(pictureOption)),
      baseUrl: baseUrl ?? (await promptTextOption(baseUrlOption)),
      rights: rights != null ? rightsFromList(rights) : await promptRights(),
    };

    const oauthConsumer = await create({ id: ownerId }, body).then(sendToApi);

    switch (format) {
      case 'json': {
        Logger.printJson(oauthConsumer);
        break;
      }
      case 'human':
      default: {
        Logger.printSuccess(`OAuth consumer ${styleText(['bold', 'green'], oauthConsumer.key)} has been created!`);
        Logger.println();
        Logger.println(
          `Retrieve the secret with ${styleText('blue', `clever oauth-consumers get ${oauthConsumer.key} --with-secret`)}`,
        );
      }
    }
  },
});