UNPKG

@cto.ai/ops

Version:

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

91 lines (90 loc) • 3.7 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 () => { 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 { 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 { user, team, tokens } = this.state.config; this.services.analytics.track({ userId: user.email, teamId: team.id, cliEvent: 'Secrets CLI Unregister', event: 'Secrets CLI Unregister', properties: { email: user.email, username: user.username, hasBeenDeleted: inputs.confirmDelete, team: team.name, }, }, tokens.accessToken); return inputs; } catch (err) { this.debug('%O', err); throw new CustomErrors_1.AnalyticsError(err); } }; } async run() { try { await this.isLoggedIn(); const unregisterPipeline = utils_1.asyncPipe(this.unregisterConfirm, this.unregisterAPI, this.sendAnalytics, this.logMessage); await unregisterPipeline(); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: this.state.config.tokens.accessToken, }); } } } exports.default = UnregisterSecret; UnregisterSecret.description = 'Unregister a secrets provider for a team';