UNPKG

@cto.ai/ops

Version:

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

83 lines (82 loc) • 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const base_1 = tslib_1.__importDefault(require("./../../base")); const utils_1 = require("./../../utils"); const CustomErrors_1 = require("./../../errors/CustomErrors"); class UnregisterSecret extends base_1.default { constructor() { super(...arguments); this.unregisterConfirm = async (inputs) => { const { team } = this.state.config; const { confirmDelete } = await this.ux.prompt({ type: 'confirm', name: 'confirmDelete', message: `Are you sure you want to remove the secret provider of the ${this.ux.colors.multiBlue(team.name)} team?`, }); return Object.assign(Object.assign({}, inputs), { confirmDelete }); }; this.unregisterAPI = async (inputs) => { try { if (!inputs.confirmDelete) return inputs; this.log('\nšŸ—‘ Removing secret provider...'); await this.services.api.remove(`/private/teams/${this.state.config.team.name}/secrets`, 'unregister', { headers: { Authorization: this.state.config.tokens.accessToken, }, }); return inputs; } catch (err) { const [{ message, code }] = err.error; if (message === 'team not found') { throw new CustomErrors_1.NoTeamFound(this.state.config.team.name); } if (code === 403 || message === 'team not authorized') { throw new CustomErrors_1.UserUnauthorized(err); } if (code === 404) { throw new CustomErrors_1.NoSecretsProviderFound(err); } throw new CustomErrors_1.APIError(err); } }; this.logMessage = (inputs) => { if (!inputs.confirmDelete) return inputs; this.log(`\nāš”ļø the secret provider has been ${this.ux.colors.red('deleted')} from the team ${this.ux.colors.multiBlue(this.state.config.team.name)}!`); return inputs; }; this.sendAnalytics = async (inputs) => { try { const { config } = inputs; this.services.analytics.track('Ops CLI Secrets:Unregister', { username: config.user.username, hasBeenDeleted: inputs.confirmDelete, }, config); return inputs; } catch (err) { this.debug('%O', err); throw new CustomErrors_1.AnalyticsError(err); } }; } async run() { const config = await this.isLoggedIn(); try { const unregisterPipeline = (0, utils_1.asyncPipe)(this.unregisterConfirm, this.unregisterAPI, this.sendAnalytics, this.logMessage); await unregisterPipeline({ config }); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: config.tokens.accessToken, }); } } } exports.default = UnregisterSecret; UnregisterSecret.description = 'Unregister a secrets provider for a team';