UNPKG

@cto.ai/ops

Version:

šŸ’» CTO.ai - The CLI built for Teams šŸš€

177 lines (176 loc) • 6.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const cli_sdk_1 = require("@cto.ai/cli-sdk"); const base_1 = tslib_1.__importStar(require("./../../base")); const utils_1 = require("./../../utils"); const fs = tslib_1.__importStar(require("fs-extra")); const CustomErrors_1 = require("./../../errors/CustomErrors"); const { white, reset } = cli_sdk_1.ux.colors; class ConfigsSet extends base_1.default { constructor() { super(...arguments); this.validateKeyInput = async (input) => { if (!input) { return `šŸ˜ž Sorry, the key cannot be empty`; } if (!utils_1.KEY_REGEX.test(input)) { return `šŸ˜ž Config keys cannot begin with a number and may only contain letters, numbers, underscores, hyphens, and periods`; } return true; }; this.validateValueInput = async (input) => { if (!input) { return `šŸ˜ž Sorry, the value cannot be empty`; } return true; }; this.resolveFileConfig = async (input) => { if (!input.valueFilename) { return input; } try { const value = await fs.readFile(input.valueFilename, 'utf8'); return Object.assign(Object.assign({}, input), { value }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.ValueFileError(err); } }; this.promptForConfig = async (input) => { await cli_sdk_1.ux.print(`\nšŸ”‘ Add a config for team ${input.config.team.name} ${reset.green('→')}`); let key = ''; if (input.key) { const keyValidationResult = await this.validateKeyInput(input.key); if (keyValidationResult === true) { key = input.key; } else { await this.ux.print(keyValidationResult); } } key = key || (await cli_sdk_1.ux.prompt({ type: 'input', name: 'key', message: `Enter the name of the config to be stored ${reset.green('→')}`, afterMessage: `${reset.white('Config name:')}`, validate: this.validateKeyInput, })).key; try { const configListInputs = {}; configListInputs.api = this.services.api; configListInputs.config = input.config; const teamConfigList = await this.services.configService.getApiConfigsList(configListInputs); const foundConfig = teamConfigList.teamConfigs.find(tc => tc.key === key); if (foundConfig) { const { confirmOverride } = await cli_sdk_1.ux.prompt({ message: `The config key: ${key} already exists. Do you want to override it?`, name: 'confirmOverride', suffix: false, type: 'confirm', }); if (!confirmOverride) { process.exit(); } } } catch (err) { this.config.runHook('error', { err, accessToken: this.accessToken }); } let value = ''; if (input.value) { const valueValidationResult = await this.validateValueInput(input.value); if (valueValidationResult === true) { value = input.value; } else { await this.ux.print(valueValidationResult); } } value = value || (await cli_sdk_1.ux.prompt({ type: 'editor', name: 'value', message: `\nNext add the config's value to be stored ${reset.green('→')}`, validate: this.validateValueInput, })).value.trim(); return Object.assign(Object.assign({}, input), { key, value }); }; this.setConfig = async (inputs) => { try { await this.services.api.create(`/private/teams/${inputs.config.team.name}/configs`, { teamConfigs: { [inputs.key]: inputs.value, }, }, { headers: { Authorization: this.accessToken, }, }); return inputs; } catch (err) { this.debug('%O', err); //TODO handle error } }; this.logMessage = (inputs) => { this.log(`\n ${white(`šŸ™Œ Great job! Config ${cli_sdk_1.ux.colors.callOutCyan(inputs.key)} has been added to your team ${cli_sdk_1.ux.colors.blueBright(inputs.config.team.name)}!`)}`); return inputs; }; this.sendAnalytics = async (inputs) => { const { config, key } = inputs; try { this.services.analytics.track('Ops CLI Configs:Set', { username: config.user.username, setConfigKey: key, }, config); return inputs; } catch (err) { this.debug('%O', err); throw new CustomErrors_1.AnalyticsError(err); } }; } async run() { let { flags: { key, value, 'from-file': valueFilename }, } = this.parse(ConfigsSet); try { const config = await this.isLoggedIn(); const configSetPipeline = (0, utils_1.asyncPipe)(this.resolveFileConfig, this.promptForConfig, this.setConfig, this.sendAnalytics, this.logMessage); await configSetPipeline({ config, key, value, valueFilename, }); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: this.accessToken }); } } } exports.default = ConfigsSet; ConfigsSet.description = 'Add a new config key & value'; ConfigsSet.flags = { key: base_1.flags.string({ char: 'k', description: 'the key of the config to set', }), value: base_1.flags.string({ char: 'v', description: 'the value of the config to set', exclusive: ['from-file'], }), 'from-file': base_1.flags.string({ char: 'f', description: 'path to a file containing the value of the config to set', exclusive: ['value'], }), };