@cto.ai/ops
Version:
💻 CTO.ai - The CLI built for Teams 🚀
80 lines (79 loc) • 3.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const base_1 = tslib_1.__importStar(require("./../../base"));
const utils_1 = require("./../../utils");
const CustomErrors_1 = require("./../../errors/CustomErrors");
class ConfigsDelete extends base_1.default {
constructor() {
super(...arguments);
this.confirmConfigDeletion = async (inputs) => {
const { selectedConfig } = inputs;
const { confirmDelete } = await this.ux.prompt({
type: 'confirm',
name: 'confirmDelete',
message: `Are you sure you want to remove ${this.ux.colors.multiBlue(selectedConfig)} from team ${this.ux.colors.multiBlue(this.state.config.team.name)}?`,
});
return Object.assign(Object.assign({}, inputs), { confirmDelete });
};
this.deleteConfigAPI = async (inputs) => {
try {
const { confirmDelete, selectedConfig, config: { team, tokens }, } = inputs;
if (!confirmDelete)
return inputs;
await this.services.api.remove(`/private/teams/${team.name}/configs`, selectedConfig, {
headers: {
Authorization: tokens.accessToken,
},
});
return inputs;
}
catch (err) {
throw new CustomErrors_1.APIError(err);
}
};
this.logMessage = (inputs) => {
if (!inputs.confirmDelete)
return inputs;
this.log(`\n⚡️ the config ${this.ux.colors.multiBlue(inputs.selectedConfig)} has been ${this.ux.colors.red('deleted')} from the team ${this.ux.colors.multiBlue(inputs.config.team.name)}!`);
return inputs;
};
this.sendAnalytics = async (inputs) => {
try {
const { config, confirmDelete, selectedConfig } = inputs;
this.services.analytics.track('Ops CLI Configs:Delete', {
username: config.user.username,
hasBeenDeleted: confirmDelete,
deletedConfigKey: selectedConfig,
}, config);
return inputs;
}
catch (err) {
this.debug('%O', err);
throw new CustomErrors_1.AnalyticsError(err);
}
};
}
async run() {
let { flags: { key }, } = this.parse(ConfigsDelete);
try {
const config = await this.isLoggedIn();
if (!key) {
const { selectedConfig, } = await this.services.configService.runListPipeline(this.state.config, this.services.api);
({ key } = selectedConfig);
}
const teamConfigsDeletePipeline = (0, utils_1.asyncPipe)(this.confirmConfigDeletion, this.deleteConfigAPI, this.sendAnalytics, this.logMessage);
await teamConfigsDeletePipeline({ config, selectedConfig: key });
}
catch (err) {
this.debug('%O', err);
this.config.runHook('error', { err, accessToken: this.accessToken });
}
}
}
exports.default = ConfigsDelete;
ConfigsDelete.description = 'Delete a config stored for the active team';
ConfigsDelete.flags = {
help: base_1.flags.help({ char: 'h' }),
key: base_1.flags.string({ char: 'k', description: 'Secret Key Name' }),
};