-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathadd.ts
169 lines (153 loc) · 6.27 KB
/
add.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Command } from '@contentstack/cli-command';
import {
cliux,
configHandler,
printFlagDeprecation,
flags,
FlagInput,
HttpClient,
messageHandler,
Flags,
formatError,
} from '@contentstack/cli-utilities';
import { askTokenType } from '../../../utils/interactive';
import { BaseCommand } from '../../../base-command';
export default class TokensAddCommand extends BaseCommand<typeof TokensAddCommand> {
static description = 'Adds management/delivery tokens to your session to use it with other CLI commands';
static examples = [
'$ csdx auth:tokens:add',
'$ csdx auth:tokens:add -a <alias>',
'$ csdx auth:tokens:add -k <stack api key>',
'$ csdx auth:tokens:add --delivery',
'$ csdx auth:tokens:add --management',
'$ csdx auth:tokens:add -e <environment>',
'$ csdx auth:tokens:add --token <token>',
'$ csdx auth:tokens:add -a <alias> -k <stack api key> --management --token <management token>',
'$ csdx auth:tokens:add -a <alias> -k <stack api key> --delivery -e <environment> --token <delivery token>',
'$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --management --token <management token>',
'$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --delivery -e <environment> --token <delivery token>',
];
static flags: FlagInput = {
alias: Flags.string({ char: 'a', description: 'Alias (name) you want to assign to the token' }),
delivery: flags.boolean({
char: 'd',
description: 'Set this flag to save delivery token',
exclusive: ['management'],
parse: printFlagDeprecation(['-d'], ['--delivery']),
}),
management: flags.boolean({
char: 'm',
description: 'Set this flag to save management token',
exclusive: ['delivery', 'environment'],
parse: printFlagDeprecation(['-m'], ['--management']),
}),
environment: flags.string({
char: 'e',
description: 'Environment name for delivery token',
exclusive: ['management'],
}),
'stack-api-key': flags.string({ char: 'k', description: 'Stack API Key' }),
yes: flags.boolean({ char: 'y', description: 'Use this flag to skip confirmation' }),
token: flags.string({
char: 't',
description: 'Add the token name',
env: 'TOKEN',
parse: printFlagDeprecation(['-t'], ['--token']),
}),
//To be deprecated
'api-key': flags.string({
description: 'API Key',
hidden: true,
parse: printFlagDeprecation(['api-key'], ['-k', 'stack-api-key']),
}),
force: flags.boolean({
char: 'f',
hidden: true,
description: 'Force adding',
parse: printFlagDeprecation(['-f', '--force'], ['-y', '--yes']),
}),
branch: flags.string({
required: false,
multiple: false,
description: 'Branch name',
hidden: true,
}),
};
static usage =
'auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]';
async run(): Promise<any> {
const { flags: addTokenFlags } = await this.parse(TokensAddCommand);
let isAliasExist = false;
const skipAliasReplaceConfirmation = addTokenFlags.force || addTokenFlags.yes;
let alias = addTokenFlags.alias;
let apiKey = addTokenFlags['api-key'] || addTokenFlags['stack-api-key'];
let token = addTokenFlags.token;
let isDelivery = addTokenFlags.delivery;
let isManagement = addTokenFlags.management;
let environment = addTokenFlags.environment;
const configKeyTokens = 'tokens';
if (!isDelivery && !isManagement && !Boolean(environment)) {
let tokenType = await askTokenType();
isDelivery = tokenType === 'delivery';
isManagement = tokenType === 'management';
}
const type = isDelivery || Boolean(environment) ? 'delivery' : 'management';
this.logger.info(`adding ${type} token`);
try {
if (!alias) {
alias = await cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ASK_TOKEN_ALIAS', name: 'alias' });
}
isAliasExist = Boolean(configHandler.get(`${configKeyTokens}.${alias}`)); // get to Check if alias already present
if (isAliasExist && !skipAliasReplaceConfirmation) {
const shouldAliasReplace = await cliux.inquire({
type: 'confirm',
message: `CLI_AUTH_TOKENS_ADD_CONFIRM_ALIAS_REPLACE`,
name: 'confirm',
});
if (!shouldAliasReplace) {
this.logger.info('Exiting from the process of replacing the token');
cliux.print('CLI_AUTH_EXIT_PROCESS');
return;
}
}
if (!apiKey) {
apiKey = await cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_API_KEY', name: 'apiKey' });
}
if (!token) {
token = await cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_TOKEN', name: 'token' });
}
if (isDelivery && !environment) {
environment = await cliux.inquire({
type: 'input',
message: 'CLI_AUTH_TOKENS_ADD_ENTER_ENVIRONMENT',
name: 'env',
});
}
if (type === 'management') {
// FIXME - Once the SDK refresh token issue is resolved, need to revert this back to SDK call
const httpClient = new HttpClient({ headers: { api_key: apiKey, authorization: token } });
const response = (await httpClient.get(`https://${this.cmaHost}/v3/environments?limit=1`)).data;
if (response?.error_code === 105) {
throw new Error(messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN'));
} else if (response?.error_message) {
throw new Error(response.error_message);
}
}
if (isManagement) {
configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, type });
} else {
configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, environment, type });
}
if (isAliasExist) {
cliux.success('CLI_AUTH_TOKENS_ADD_REPLACE_SUCCESS');
} else {
cliux.success('CLI_AUTH_TOKENS_ADD_SUCCESS');
}
} catch (error) {
let errorMessage = formatError(error) || 'Something went wrong while adding token. Please try again.';
this.logger.error('token add error', errorMessage);
cliux.print('CLI_AUTH_TOKENS_ADD_FAILED', { color: 'yellow' });
cliux.error(errorMessage);
}
}
}