@cto.ai/ops
Version:
š» CTO.ai Ops - The CLI built for Teams š
104 lines (103 loc) ⢠4.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const base_1 = tslib_1.__importStar(require("../../base"));
const asyncPipe_1 = require("../../utils/asyncPipe");
const CustomErrors_1 = require("../../errors/CustomErrors");
class SecretsDelete extends base_1.default {
constructor() {
super(...arguments);
this.confirmSecretDeletion = async (inputs) => {
const { selectedSecret } = inputs;
if (typeof selectedSecret === 'undefined') {
throw new CustomErrors_1.NoSecretFound();
}
const { confirmDelete } = await this.ux.prompt({
type: 'confirm',
name: 'confirmDelete',
message: `Are you sure you want to remove ${this.ux.colors.multiBlue(selectedSecret)} from team ${this.ux.colors.multiBlue(this.state.config.team.name)}?`,
});
return { selectedSecret, confirmDelete };
};
this.deleteSecretAPI = async (inputs) => {
try {
if (!inputs.confirmDelete)
return inputs;
this.log('\n š Removing secret...');
await this.services.api.remove(`/private/teams/${this.state.config.team.name}/secret`, inputs.selectedSecret, {
headers: {
Authorization: this.state.config.tokens.accessToken,
},
});
return inputs;
}
catch (err) {
switch (err.error[0].code) {
case 400:
throw new CustomErrors_1.InvalidSecretVault(err);
case 401:
throw new CustomErrors_1.UserUnauthorized(err);
case 403:
if (err.error[0].message.includes('invalid secret token')) {
throw new CustomErrors_1.InvalidSecretToken(err);
}
else {
throw new CustomErrors_1.NoSecretsProviderFound(err);
}
case 404:
throw new CustomErrors_1.SecretNotFound(err);
default:
throw new CustomErrors_1.NoSecretsProviderFound(err);
}
}
};
this.logMessage = (inputs) => {
if (!inputs.confirmDelete)
return inputs;
this.log(`\nā”ļø the secret ${this.ux.colors.multiBlue(inputs.selectedSecret)} 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 {
this.services.analytics.track({
userId: this.state.config.user.email,
teamId: this.state.config.team.id,
cliEvent: 'Secrets CLI Delete',
event: 'Secrets CLI Delete',
properties: {
email: this.state.config.user.email,
username: this.state.config.user.username,
hasBeenDeleted: inputs.confirmDelete,
deletedSecretKey: inputs.selectedSecret,
},
}, this.state.config.tokens.accessToken);
return inputs;
}
catch (err) {
this.debug('%O', err);
throw new CustomErrors_1.AnalyticsError(err);
}
};
}
async run() {
let { flags: { key }, } = this.parse(SecretsDelete);
try {
await this.isLoggedIn();
const inputs = key
? { selectedSecret: key }
: await this.services.secretService.runListPipeline(this.state.config, this.services.api);
const secretDeletePipeline = asyncPipe_1.asyncPipe(this.confirmSecretDeletion, this.deleteSecretAPI, this.sendAnalytics, this.logMessage);
await secretDeletePipeline(inputs);
}
catch (err) {
this.debug('%O', err);
this.config.runHook('error', { err });
}
}
}
exports.default = SecretsDelete;
SecretsDelete.description = 'Delete a secret stored for the active team';
SecretsDelete.flags = {
help: base_1.flags.help({ char: 'h' }),
key: base_1.flags.string({ char: 'k', description: 'Secret Key Name' }),
};