-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathremove.ts
More file actions
63 lines (56 loc) · 2.38 KB
/
remove.ts
File metadata and controls
63 lines (56 loc) · 2.38 KB
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
62
63
import { cliux, configHandler, flags, FlagInput, log, handleAndLogError } from '@contentstack/cli-utilities';
import { BaseCommand } from '../../../base-command';
export default class TokensRemoveCommand extends BaseCommand<typeof TokensRemoveCommand> {
static description = 'Removes selected tokens';
static examples = ['$ csdx auth:tokens:remove', '$ csdx auth:tokens:remove -a <alias>'];
static flags: FlagInput = {
alias: flags.string({ char: 'a', description: 'Alias (name) of the token to delete.' }),
ignore: flags.boolean({ char: 'i', description: 'Ignores if the token is not present.' }),
};
async run(): Promise<any> {
const { flags: removeTokenFlags } = await this.parse(TokensRemoveCommand);
const alias = removeTokenFlags.alias;
const ignore = removeTokenFlags.ignore;
try {
const token = configHandler.get(`tokens.${alias}`);
const tokens = configHandler.get('tokens');
const tokenOptions: Array<string> = [];
if (token || ignore) {
configHandler.delete(`tokens.${alias}`);
return cliux.success(`CLI_AUTH_TOKENS_REMOVE_SUCCESS`);
}
if (tokens && Object.keys(tokens).length > 0) {
Object.keys(tokens).forEach(function (item) {
tokenOptions.push(
`${item}: ${tokens[item].token} : ${tokens[item].apiKey}${
tokens[item].environment ? ' : ' + tokens[item].environment + ' ' : ''
}: ${tokens[item].type}`,
);
});
} else {
return cliux.print('CLI_AUTH_TOKENS_NOT_FOUND');
}
const selectedTokens: Array<any> = await cliux.inquire({
name: 'selectedTokens',
message: 'CLI_AUTH_TOKENS_REMOVE_SELECT_TOKEN',
type: 'checkbox',
choices: tokenOptions,
});
if (selectedTokens.length === 0) {
return;
}
selectedTokens.forEach((ele)=>{
log.info(`Selected token: ${ele}`, this.contextDetails);
})
selectedTokens.forEach((element) => {
const selectedToken = element.split(':')[0];
configHandler.delete(`tokens.${selectedToken}`);
cliux.success('CLI_AUTH_TOKENS_REMOVE_SUCCESS');
log.info(`Token removed: ${selectedToken}`, this.contextDetails);
});
} catch (error) {
cliux.print('CLI_AUTH_TOKENS_REMOVE_FAILED', { color: 'yellow' });
handleAndLogError(error, {...this.contextDetails} )
}
}
}