Skip to content

Commit ced638c

Browse files
Merge pull request #2615 from contentstack/enh/dx-3812-tokens-enh
feat: add auth:tokens namespace help and auth:tokens:list command
2 parents 7dce357 + b0d1455 commit ced638c

3 files changed

Lines changed: 117 additions & 100 deletions

File tree

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import {
2-
cliux,
3-
configHandler,
4-
CLITable,
5-
TableFlags,
6-
FlagInput,
7-
handleAndLogError,
8-
log,
9-
} from '@contentstack/cli-utilities';
1+
import { Help } from '@oclif/core';
102
import { BaseCommand } from '../../../base-command';
11-
export default class TokensListCommand extends BaseCommand<typeof TokensListCommand> {
12-
static aliases = ['tokens'];
13-
static examples = ['$ csdx auth:tokens'];
14-
static description = 'Lists all existing tokens added to the session';
3+
import { CLITable, FlagInput } from '@contentstack/cli-utilities';
4+
5+
export default class AuthTokensCommand extends BaseCommand<typeof AuthTokensCommand> {
6+
static description = 'Manage authentication tokens for API access';
7+
static strict = false;
8+
9+
static examples = [
10+
'$ csdx auth:tokens:list',
11+
'$ csdx auth:tokens:add --alias mytoken',
12+
'$ csdx auth:tokens:remove --alias mytoken',
13+
];
14+
1515
static flags: FlagInput = CLITable.getTableFlags([
1616
'columns',
1717
'sort',
@@ -20,65 +20,11 @@ export default class TokensListCommand extends BaseCommand<typeof TokensListComm
2020
'no-truncate',
2121
'no-header',
2222
'output',
23-
]); // use the cli table flags as it displays tokens in table
23+
]);
2424

2525
async run(): Promise<any> {
26-
log.debug('TokensListCommand run method started.', this.contextDetails);
27-
this.contextDetails.module = 'tokens-list';
28-
29-
try {
30-
log.debug('Retrieving tokens from configuration.', this.contextDetails);
31-
const managementTokens = configHandler.get('tokens');
32-
log.debug('Tokens retrieved from configuration.', {...this.contextDetails, tokenCount: managementTokens ? Object.keys(managementTokens).length : 0 });
33-
34-
const tokens: Record<string, unknown>[] = [];
35-
if (managementTokens && Object.keys(managementTokens).length > 0) {
36-
log.debug('Processing tokens for display.', this.contextDetails);
37-
Object.keys(managementTokens).forEach(function (item) {
38-
tokens.push({
39-
alias: item,
40-
token: managementTokens[item].token,
41-
apiKey: managementTokens[item].apiKey,
42-
environment: managementTokens[item].environment ? managementTokens[item].environment : '-',
43-
type: managementTokens[item].type,
44-
});
45-
log.debug(`Token processed: ${item}`, {tokenType: managementTokens[item].type });
46-
});
47-
48-
const { flags } = await this.parse(TokensListCommand);
49-
log.debug('Token list flags parsed.', {...this.contextDetails, flags });
50-
51-
const headers = [
52-
{
53-
value: 'alias',
54-
},
55-
{
56-
value: 'token',
57-
},
58-
{
59-
value: 'apiKey',
60-
},
61-
{
62-
value: 'environment',
63-
},
64-
{
65-
value: 'type',
66-
},
67-
];
68-
69-
log.debug('Displaying token table.', {...this.contextDetails, tokenCount: tokens.length });
70-
cliux.table(headers, tokens, flags as TableFlags);
71-
log.debug('Token table displayed successfully.', this.contextDetails);
72-
} else {
73-
log.debug('No tokens found in configuration.', this.contextDetails);
74-
cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
75-
}
76-
77-
log.debug('Token list command completed successfully.', this.contextDetails);
78-
} catch (error) {
79-
log.debug('Token list command failed.', {...this.contextDetails, error });
80-
cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
81-
handleAndLogError(error, { ...this.contextDetails });
82-
}
26+
await this.parse(AuthTokensCommand, this.argv.filter((a) => a !== '-help' && a !== '-h'));
27+
const cmd = this.config.findCommand('auth:tokens');
28+
if (cmd) await new Help(this.config).showCommandHelp(cmd);
8329
}
8430
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import {
2+
cliux,
3+
configHandler,
4+
CLITable,
5+
TableFlags,
6+
FlagInput,
7+
handleAndLogError,
8+
log,
9+
} from '@contentstack/cli-utilities';
10+
import { BaseCommand } from '../../../base-command';
11+
12+
export default class AuthTokensListCommand extends BaseCommand<typeof AuthTokensListCommand> {
13+
static description = 'Lists all existing tokens added to the session';
14+
static examples = ['$ csdx auth:tokens:list'];
15+
static flags: FlagInput = CLITable.getTableFlags([
16+
'columns',
17+
'sort',
18+
'filter',
19+
'csv',
20+
'no-truncate',
21+
'no-header',
22+
'output',
23+
]);
24+
25+
async run(): Promise<any> {
26+
log.debug('AuthTokensListCommand run method started.', this.contextDetails);
27+
this.contextDetails.module = 'auth-tokens-list';
28+
29+
try {
30+
log.debug('Retrieving tokens from configuration.', this.contextDetails);
31+
const managementTokens = configHandler.get('tokens');
32+
log.debug('Tokens retrieved from configuration.', { ...this.contextDetails, tokenCount: managementTokens ? Object.keys(managementTokens).length : 0 });
33+
34+
const tokens: Record<string, unknown>[] = [];
35+
if (managementTokens && Object.keys(managementTokens).length > 0) {
36+
log.debug('Processing tokens for display.', this.contextDetails);
37+
Object.keys(managementTokens).forEach(function (item) {
38+
tokens.push({
39+
alias: item,
40+
token: managementTokens[item].token,
41+
apiKey: managementTokens[item].apiKey,
42+
environment: managementTokens[item].environment ? managementTokens[item].environment : '-',
43+
type: managementTokens[item].type,
44+
});
45+
log.debug(`Token processed: ${item}`, { tokenType: managementTokens[item].type });
46+
});
47+
48+
const { flags } = await this.parse(AuthTokensListCommand);
49+
log.debug('Token list flags parsed.', { ...this.contextDetails, flags });
50+
51+
const headers = [
52+
{ value: 'alias' },
53+
{ value: 'token' },
54+
{ value: 'apiKey' },
55+
{ value: 'environment' },
56+
{ value: 'type' },
57+
];
58+
59+
log.debug('Displaying token table.', { ...this.contextDetails, tokenCount: tokens.length });
60+
cliux.table(headers, tokens, flags as TableFlags);
61+
log.debug('Token table displayed successfully.', this.contextDetails);
62+
} else {
63+
log.debug('No tokens found in configuration.', this.contextDetails);
64+
cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
65+
}
66+
67+
log.debug('Token list command completed successfully.', this.contextDetails);
68+
} catch (error) {
69+
log.debug('Token list command failed.', { ...this.contextDetails, error });
70+
cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
71+
handleAndLogError(error, { ...this.contextDetails });
72+
}
73+
}
74+
}

pnpm-lock.yaml

Lines changed: 26 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)