Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 17 additions & 71 deletions packages/contentstack-auth/src/commands/auth/tokens/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
cliux,
configHandler,
CLITable,
TableFlags,
FlagInput,
handleAndLogError,
log,
} from '@contentstack/cli-utilities';
import { Help } from '@oclif/core';
import { BaseCommand } from '../../../base-command';
export default class TokensListCommand extends BaseCommand<typeof TokensListCommand> {
static aliases = ['tokens'];
static examples = ['$ csdx auth:tokens'];
static description = 'Lists all existing tokens added to the session';
import { CLITable, FlagInput } from '@contentstack/cli-utilities';

export default class AuthTokensCommand extends BaseCommand<typeof AuthTokensCommand> {
static description = 'Manage authentication tokens for API access';
static strict = false;

static examples = [
'$ csdx auth:tokens:list',
'$ csdx auth:tokens:add --alias mytoken',
'$ csdx auth:tokens:remove --alias mytoken',
];

static flags: FlagInput = CLITable.getTableFlags([
'columns',
'sort',
Expand All @@ -20,65 +20,11 @@ export default class TokensListCommand extends BaseCommand<typeof TokensListComm
'no-truncate',
'no-header',
'output',
]); // use the cli table flags as it displays tokens in table
]);

async run(): Promise<any> {
log.debug('TokensListCommand run method started.', this.contextDetails);
this.contextDetails.module = 'tokens-list';

try {
log.debug('Retrieving tokens from configuration.', this.contextDetails);
const managementTokens = configHandler.get('tokens');
log.debug('Tokens retrieved from configuration.', {...this.contextDetails, tokenCount: managementTokens ? Object.keys(managementTokens).length : 0 });

const tokens: Record<string, unknown>[] = [];
if (managementTokens && Object.keys(managementTokens).length > 0) {
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,
});
log.debug(`Token processed: ${item}`, {tokenType: managementTokens[item].type });
});

const { flags } = await this.parse(TokensListCommand);
log.debug('Token list flags parsed.', {...this.contextDetails, flags });

const headers = [
{
value: 'alias',
},
{
value: 'token',
},
{
value: 'apiKey',
},
{
value: 'environment',
},
{
value: 'type',
},
];

log.debug('Displaying token table.', {...this.contextDetails, tokenCount: tokens.length });
cliux.table(headers, tokens, flags as TableFlags);
log.debug('Token table displayed successfully.', this.contextDetails);
} else {
log.debug('No tokens found in configuration.', this.contextDetails);
cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
}

log.debug('Token list command completed successfully.', this.contextDetails);
} catch (error) {
log.debug('Token list command failed.', {...this.contextDetails, error });
cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
handleAndLogError(error, { ...this.contextDetails });
}
await this.parse(AuthTokensCommand, this.argv.filter((a) => a !== '-help' && a !== '-h'));
const cmd = this.config.findCommand('auth:tokens');
if (cmd) await new Help(this.config).showCommandHelp(cmd);
}
}
74 changes: 74 additions & 0 deletions packages/contentstack-auth/src/commands/auth/tokens/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
cliux,
configHandler,
CLITable,
TableFlags,
FlagInput,
handleAndLogError,
log,
} from '@contentstack/cli-utilities';
import { BaseCommand } from '../../../base-command';

export default class AuthTokensListCommand extends BaseCommand<typeof AuthTokensListCommand> {
static description = 'Lists all existing tokens added to the session';
static examples = ['$ csdx auth:tokens:list'];
static flags: FlagInput = CLITable.getTableFlags([
'columns',
'sort',
'filter',
'csv',
'no-truncate',
'no-header',
'output',
]);

async run(): Promise<any> {
log.debug('AuthTokensListCommand run method started.', this.contextDetails);
this.contextDetails.module = 'auth-tokens-list';

try {
log.debug('Retrieving tokens from configuration.', this.contextDetails);
const managementTokens = configHandler.get('tokens');
log.debug('Tokens retrieved from configuration.', { ...this.contextDetails, tokenCount: managementTokens ? Object.keys(managementTokens).length : 0 });

const tokens: Record<string, unknown>[] = [];
if (managementTokens && Object.keys(managementTokens).length > 0) {
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,
});
log.debug(`Token processed: ${item}`, { tokenType: managementTokens[item].type });
});

const { flags } = await this.parse(AuthTokensListCommand);
log.debug('Token list flags parsed.', { ...this.contextDetails, flags });

const headers = [
{ value: 'alias' },
{ value: 'token' },
{ value: 'apiKey' },
{ value: 'environment' },
{ value: 'type' },
];

log.debug('Displaying token table.', { ...this.contextDetails, tokenCount: tokens.length });
cliux.table(headers, tokens, flags as TableFlags);
log.debug('Token table displayed successfully.', this.contextDetails);
} else {
log.debug('No tokens found in configuration.', this.contextDetails);
cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
}

log.debug('Token list command completed successfully.', this.contextDetails);
} catch (error) {
log.debug('Token list command failed.', { ...this.contextDetails, error });
cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
handleAndLogError(error, { ...this.contextDetails });
}
}
}
55 changes: 26 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading