@contentstack/cli-auth
Version:
Contentstack CLI plugin for authentication activities
79 lines (78 loc) • 5.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cli_utilities_1 = require("@contentstack/cli-utilities");
const base_command_1 = require("../../../base-command");
class TokensRemoveCommand extends base_command_1.BaseCommand {
async run() {
cli_utilities_1.log.debug('TokensRemoveCommand run method started', this.contextDetails);
this.contextDetails.module = 'tokens-remove';
const { flags: removeTokenFlags } = await this.parse(TokensRemoveCommand);
cli_utilities_1.log.debug('Token remove flags parsed', Object.assign(Object.assign({}, this.contextDetails), { flags: removeTokenFlags }));
const alias = removeTokenFlags.alias;
const ignore = removeTokenFlags.ignore;
cli_utilities_1.log.debug('Token removal parameters', Object.assign(Object.assign({}, this.contextDetails), { alias, ignore }));
try {
cli_utilities_1.log.debug('Retrieving token from configuration', Object.assign(Object.assign({}, this.contextDetails), { alias }));
const token = cli_utilities_1.configHandler.get(`tokens.${alias}`);
cli_utilities_1.log.debug('Token retrieved from configuration', Object.assign(Object.assign({}, this.contextDetails), { hasToken: !!token, tokenType: token === null || token === void 0 ? void 0 : token.type }));
const tokens = cli_utilities_1.configHandler.get('tokens');
cli_utilities_1.log.debug('All tokens retrieved from configuration', Object.assign(Object.assign({}, this.contextDetails), { tokenCount: tokens ? Object.keys(tokens).length : 0 }));
const tokenOptions = [];
if (token || ignore) {
cli_utilities_1.log.debug('Token found or ignore flag set, proceeding with removal', Object.assign(Object.assign({}, this.contextDetails), { hasToken: !!token, ignore }));
cli_utilities_1.configHandler.delete(`tokens.${alias}`);
cli_utilities_1.log.debug('Token removed from configuration', Object.assign(Object.assign({}, this.contextDetails), { alias }));
return cli_utilities_1.cliux.success(`CLI_AUTH_TOKENS_REMOVE_SUCCESS`);
}
if (tokens && Object.keys(tokens).length > 0) {
cli_utilities_1.log.debug('Building token options for user selection', this.contextDetails);
Object.keys(tokens).forEach(function (item) {
const tokenOption = `${item}: ${tokens[item].token} : ${tokens[item].apiKey}${tokens[item].environment ? ' : ' + tokens[item].environment + ' ' : ''}: ${tokens[item].type}`;
tokenOptions.push(tokenOption);
cli_utilities_1.log.debug(`Token option added: ${item}`, { tokenType: tokens[item].type });
});
cli_utilities_1.log.debug(`Token options built: ${tokenOptions.length} options`, this.contextDetails);
}
else {
cli_utilities_1.log.debug('No tokens found in configuration', this.contextDetails);
return cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_NOT_FOUND');
}
cli_utilities_1.log.debug('Requesting user to select tokens for removal', this.contextDetails);
const selectedTokens = await cli_utilities_1.cliux.inquire({
name: 'selectedTokens',
message: 'CLI_AUTH_TOKENS_REMOVE_SELECT_TOKEN',
type: 'checkbox',
choices: tokenOptions,
});
cli_utilities_1.log.debug(`User selected ${selectedTokens.length} tokens for removal`, Object.assign(Object.assign({}, this.contextDetails), { selectedTokens }));
if (selectedTokens.length === 0) {
cli_utilities_1.log.debug('No tokens selected for removal, exiting', this.contextDetails);
return;
}
selectedTokens.forEach((ele) => {
cli_utilities_1.log.info(`Selected token: ${ele}`, this.contextDetails);
});
cli_utilities_1.log.debug('Removing selected tokens from configuration', this.contextDetails);
selectedTokens.forEach((element) => {
const selectedToken = element.split(':')[0];
cli_utilities_1.log.debug(`Removing token: ${selectedToken}`, this.contextDetails);
cli_utilities_1.configHandler.delete(`tokens.${selectedToken}`);
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_REMOVE_SUCCESS');
cli_utilities_1.log.info(`Token removed: ${selectedToken}`, this.contextDetails);
});
cli_utilities_1.log.debug('Token removal process completed successfully', this.contextDetails);
}
catch (error) {
cli_utilities_1.log.debug('Token removal process failed', Object.assign(Object.assign({}, this.contextDetails), { error }));
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_REMOVE_FAILED', { color: 'yellow' });
(0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails));
}
}
}
exports.default = TokensRemoveCommand;
TokensRemoveCommand.description = 'Removes selected tokens';
TokensRemoveCommand.examples = ['$ csdx auth:tokens:remove', '$ csdx auth:tokens:remove -a <alias>'];
TokensRemoveCommand.flags = {
alias: cli_utilities_1.flags.string({ char: 'a', description: 'Alias (name) of the token to delete.' }),
ignore: cli_utilities_1.flags.boolean({ char: 'i', description: 'Ignores if the token is not present.' }),
};