All files / src/commands/config-provider config-provider.import.command.js

72.5% Statements 29/40
100% Branches 1/1
0% Functions 0/1
72.5% Lines 29/40

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 41193x 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 { updateConfigProviderEnv } from '@clevercloud/client/esm/api/v4/addon.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 { resolveConfigProviderId } from '../../models/config-provider.js';
import { sendToApi } from '../../models/send-to-api.js';
import * as variables from '../../models/variables.js';
import { configProviderIdOrNameArg } from './config-provider.args.js';
 
const importFormatOption = defineOption({
  name: 'format',
  schema: z.enum(['name-equals-value', 'json']).default('name-equals-value'),
  description: 'Input format',
  aliases: ['F'],
  placeholder: 'format',
});
 
export const configProviderImportCommand = defineCommand({
  description:
    'Load environment variables from STDIN\n(WARNING: this deletes all current variables and replaces them with the new list loaded from STDIN)',
  since: '4.6.0',
  options: {
    format: importFormatOption,
  },
  args: [configProviderIdOrNameArg],
  async handler(options, addonIdOrRealIdOrName) {
    const { format } = options;
    const { realId } = await resolveConfigProviderId(addonIdOrRealIdOrName);

    // readVariablesFromStdin returns { NAME: "value" } format
    // but the API expects [{ name, value }] format
    const envVarsObject = await variables.readVariablesFromStdin(format);
    const envVarsArray = Object.entries(envVarsObject).map(([name, value]) => ({ name, value }));

    await updateConfigProviderEnv({ configurationProviderId: realId }, envVarsArray).then(sendToApi);

    Logger.println('Environment variables have been set');
  },
});