UNPKG

@contentstack/cli-auth

Version:

Contentstack CLI plugin for authentication activities

75 lines (74 loc) 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cli_utilities_1 = require("@contentstack/cli-utilities"); const base_command_1 = require("../../../base-command"); class TokensListCommand extends base_command_1.BaseCommand { async run() { cli_utilities_1.log.debug('TokensListCommand run method started', this.contextDetails); this.contextDetails.module = 'tokens-list'; try { cli_utilities_1.log.debug('Retrieving tokens from configuration', this.contextDetails); const managementTokens = cli_utilities_1.configHandler.get('tokens'); cli_utilities_1.log.debug('Tokens retrieved from configuration', Object.assign(Object.assign({}, this.contextDetails), { tokenCount: managementTokens ? Object.keys(managementTokens).length : 0 })); const tokens = []; if (managementTokens && Object.keys(managementTokens).length > 0) { cli_utilities_1.log.debug('Processing tokens for display', this.contextDetails); Object.keys(managementTokens).forEach(function (item) { tokens.push({ alias: item, token: managementTokens[item].token, apiKey: managementTokens[item].apiKey, environment: managementTokens[item].environment ? managementTokens[item].environment : '-', type: managementTokens[item].type, }); cli_utilities_1.log.debug(`Token processed: ${item}`, { tokenType: managementTokens[item].type }); }); const { flags } = await this.parse(TokensListCommand); cli_utilities_1.log.debug('Tokens list flags parsed', Object.assign(Object.assign({}, this.contextDetails), { flags })); const headers = [ { value: 'alias', }, { value: 'token', }, { value: 'apiKey', }, { value: 'environment', }, { value: 'type', }, ]; cli_utilities_1.log.debug('Displaying tokens table', Object.assign(Object.assign({}, this.contextDetails), { tokenCount: tokens.length })); cli_utilities_1.cliux.table(headers, tokens, flags); cli_utilities_1.log.debug('Tokens table displayed successfully', this.contextDetails); } else { cli_utilities_1.log.debug('No tokens found in configuration', this.contextDetails); cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS'); } cli_utilities_1.log.debug('Tokens list command completed successfully', this.contextDetails); } catch (error) { cli_utilities_1.log.debug('Tokens list command failed', Object.assign(Object.assign({}, this.contextDetails), { error })); cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' }); (0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails)); } } } exports.default = TokensListCommand; TokensListCommand.aliases = ['tokens']; TokensListCommand.examples = ['$ csdx auth:tokens']; TokensListCommand.description = 'Lists all existing tokens added to the session'; TokensListCommand.flags = cli_utilities_1.CLITable.getTableFlags([ 'columns', 'sort', 'filter', 'csv', 'no-truncate', 'no-header', 'output', ]); // use the cli table flags as it displays tokens in table