@mondaycom/apps-cli
Version:
A cli tool to manage apps (and monday-code projects) in monday.com
95 lines (94 loc) • 4.02 kB
JavaScript
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 { handleSecretRequest, listAppSecretKeys } from '../../services/manage-app-secret-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 secret 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 listAppSecretKeys(appId, region);
key = await PromptService.promptSelectionWithAutoComplete('Enter key for secret variable', existingKeys, {
includeInputInSelection: true,
});
}
return key;
};
const promptForValueIfNotProvided = async (mode, value) => {
if (!value && isValueRequired(mode)) {
value = await PromptService.promptForHiddenInput('value', 'Enter value for secret variable', 'You must enter a value');
}
return value;
};
const flagsWithModeRelationships = {
type: 'all',
flags: [
{
name: 'mode',
when: async (flags) => isValueRequired(flags.mode),
},
],
};
export default class Secret extends AuthenticatedCommand {
static description = 'Manage secret variables for your app hosted on monday-code.';
static examples = ['<%= config.bin %> <%= command.id %>'];
static flags = Secret.serializeFlags(addRegionToFlags({
appId: Flags.integer({
char: 'i',
aliases: ['a'],
description: 'The id of the app to manage secret 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 = 'secret';
async run() {
try {
const { flags } = await this.parse(Secret);
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 handleSecretRequest(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);
}
}
}