-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.ts
61 lines (58 loc) · 2.09 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Command } from '@contentstack/cli-command';
import { cliux, configHandler, formatError } from '@contentstack/cli-utilities';
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';
static flags: Record<string, any> = cliux.uxTable.flags(); // use the cli table flags as it displays tokens in table
async run(): Promise<any> {
try {
const managementTokens = configHandler.get('tokens');
const tokenOptions: Record<string, unknown>[] = [];
if (managementTokens && Object.keys(managementTokens).length > 0) {
Object.keys(managementTokens).forEach(function (item) {
tokenOptions.push({
alias: item,
token: managementTokens[item].token,
apiKey: managementTokens[item].apiKey,
environment: managementTokens[item].environment ? managementTokens[item].environment : '-',
type: managementTokens[item].type,
});
});
const { flags } = await this.parse(TokensListCommand);
cliux.table(
tokenOptions,
{
alias: {
minWidth: 7,
},
token: {
minWidth: 7,
},
apiKey: {
minWidth: 7,
},
environment: {
minWidth: 7,
},
type: {
minWidth: 7,
},
},
{
printLine: cliux.print,
...flags, // parsed flags
},
);
} else {
cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
}
} catch (error) {
let errorMessage = formatError(error) || 'Something went wrong while fetching tokens. Please try again.';
this.logger.error('Token list error', errorMessage);
cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
cliux.print(errorMessage, { color: 'red' });
}
}
}