All files / src/commands/published-config published-config.set.command.js

58.82% Statements 20/34
100% Branches 1/1
0% Functions 0/1
58.82% Lines 20/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 193x 193x 193x                             193x 193x  
import { getAllExposedEnvVars, updateAllExposedEnvVars } from '@clevercloud/client/esm/api/v2/application.js';
import { validateName } from '@clevercloud/client/esm/utils/env-vars.js';
import { defineCommand } from '../../lib/define-command.js';
import { Logger } from '../../logger.js';
import * as Application from '../../models/application.js';
import { sendToApi } from '../../models/send-to-api.js';
import { envVariableNameArg, envVariableValueArg } from '../global.args.js';
import { aliasOption, appIdOrNameOption } from '../global.options.js';
 
export const publishedConfigSetCommand = defineCommand({
  description: 'Add or update a published configuration item named <variable-name> with the value <variable-value>',
  since: '0.5.0',
  options: {
    alias: aliasOption,
    app: appIdOrNameOption,
  },
  args: [envVariableNameArg, envVariableValueArg],
  async handler(options, varName, varValue) {
    const { alias, app: appIdOrName } = options;

    const nameIsValid = validateName(varName);
    if (!nameIsValid) {
      throw new Error(`Published config name ${varName} is invalid`);
    }

    const { ownerId, appId } = await Application.resolveId(appIdOrName, alias);

    const publishedConfigs = await getAllExposedEnvVars({ id: ownerId, appId }).then(sendToApi);
    publishedConfigs[varName] = varValue;
    await updateAllExposedEnvVars({ id: ownerId, appId }, publishedConfigs).then(sendToApi);

    Logger.println('Your published config item has been successfully saved');
  },
});