UNPKG

@mondaycom/apps-cli

Version:

A cli tool to manage apps (and monday-code projects) in monday.com

95 lines (94 loc) 4.04 kB
import { Flags } from '@oclif/core'; import { AuthenticatedCommand } from '../../commands-base/authenticated-command.js'; import { APP_VARIABLE_MANAGEMENT_MODES } from '../../consts/manage-app-variables.js'; import { DynamicChoicesService } from '../../services/dynamic-choices-service.js'; import { handleEnvironmentRequest, listAppEnvKeys } from '../../services/manage-app-env-service.js'; import { PromptService } from '../../services/prompt-service.js'; import logger from '../../utils/logger.js'; import { addRegionToFlags, chooseRegionIfNeeded, getRegionFromString } from '../../utils/region.js'; const MODES_WITH_KEYS = [ APP_VARIABLE_MANAGEMENT_MODES.SET, APP_VARIABLE_MANAGEMENT_MODES.DELETE, ]; const isKeyRequired = (mode) => MODES_WITH_KEYS.includes(mode); const isValueRequired = (mode) => mode === APP_VARIABLE_MANAGEMENT_MODES.SET; const promptForModeIfNotProvided = async (mode) => { if (!mode) { mode = await PromptService.promptSelectionWithAutoComplete('Select app environment variables management mode', Object.values(APP_VARIABLE_MANAGEMENT_MODES)); } return mode; }; const promptForKeyIfNotProvided = async (mode, appId, key, region) => { if (!key && isKeyRequired(mode)) { const existingKeys = await listAppEnvKeys(appId, region); key = await PromptService.promptSelectionWithAutoComplete('Enter key for environment variable', existingKeys, { includeInputInSelection: true, }); } return key; }; const promptForValueIfNotProvided = async (mode, value) => { if (!value && isValueRequired(mode)) { value = await PromptService.promptForHiddenInput('value', 'Enter value for environment variable', 'You must enter a value'); } return value; }; const flagsWithModeRelationships = { type: 'all', flags: [ { name: 'mode', when: async (flags) => isValueRequired(flags.mode), }, ], }; export default class Env extends AuthenticatedCommand { static description = 'Manage environment variables for your app hosted on monday-code.'; static examples = ['<%= config.bin %> <%= command.id %>']; static flags = Env.serializeFlags(addRegionToFlags({ appId: Flags.integer({ char: 'i', aliases: ['a'], description: 'The id of the app to manage environment variables for', }), mode: Flags.string({ char: 'm', description: 'management mode', options: Object.values(APP_VARIABLE_MANAGEMENT_MODES), }), key: Flags.string({ char: 'k', description: 'variable key [required for set and delete]]', relationships: [flagsWithModeRelationships], }), value: Flags.string({ char: 'v', description: 'variable value [required for set]', relationships: [flagsWithModeRelationships], }), })); static args = {}; DEBUG_TAG = 'env'; async run() { try { const { flags } = await this.parse(Env); const { region: strRegion } = flags; const region = getRegionFromString(strRegion); let { mode, key, value, appId } = flags; if (!appId) { appId = Number(await DynamicChoicesService.chooseApp()); } const selectedRegion = await chooseRegionIfNeeded(region, { appId }); mode = await promptForModeIfNotProvided(mode); key = await promptForKeyIfNotProvided(mode, appId, key, selectedRegion); value = await promptForValueIfNotProvided(mode, value); this.preparePrintCommand(this, { appId, mode, key, value, region: selectedRegion }); await handleEnvironmentRequest(appId, mode, key, value, selectedRegion); } catch (error) { logger.debug(error, this.DEBUG_TAG); // need to signal to the parent process that the command failed process.exit(1); } } }