-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcredentials.ts
More file actions
78 lines (71 loc) · 2.37 KB
/
credentials.ts
File metadata and controls
78 lines (71 loc) · 2.37 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
ExternalCliOptions,
SharedFlagsWithCredentials,
} from '../../commands/shared';
import { getDebugFunction } from '../../utils/logger';
import { readLocalEnvFile } from './env';
const debug = getDebugFunction('twilio-run:config:credentials');
export type Credentials = {
accountSid: string;
authToken: string;
};
/**
* Determines the credentials by the following order of preference:
* 1. value via explicit flags
* 2. value passed in through externalCliOptions if `profile` exists
* 3. value passed in through externalCliOptions if `project` (deprecated and removed in `@twilio/cli-core` v3) exists
* 4. value in .env file
* 5. value passed in through externalCliOptions
* 6. empty string
* @param flags Flags passed into command
* @param externalCliOptions Any external information for example passed by the Twilio CLI
*/
export async function getCredentialsFromFlags<
T extends SharedFlagsWithCredentials
>(flags: T, externalCliOptions?: ExternalCliOptions): Promise<Credentials> {
// default Twilio CLI credentials (4) or empty string (5)
let accountSid =
(externalCliOptions &&
!(externalCliOptions.profile || externalCliOptions.project) &&
externalCliOptions.username) ||
'';
let authToken =
(externalCliOptions &&
!(externalCliOptions.profile || externalCliOptions.project) &&
externalCliOptions.password) ||
'';
if (flags.cwd) {
// env file content (3)
const { localEnv } = await readLocalEnvFile(flags);
if (localEnv.ACCOUNT_SID) {
debug('Override value with .env ACCOUNT_SID value');
accountSid = localEnv.ACCOUNT_SID;
}
if (localEnv.AUTH_TOKEN) {
debug('Override value with .env AUTH_TOKEN value');
authToken = localEnv.AUTH_TOKEN;
}
}
// specific profile specified. override both credentials (2)
if (
externalCliOptions &&
(externalCliOptions.profile || externalCliOptions.project)
) {
debug('Values read from explicit CLI profile');
accountSid = externalCliOptions.username;
authToken = externalCliOptions.password;
}
// specific flag passed. override for that flag (1)
if (flags.accountSid) {
debug('Override accountSid with value from flag');
accountSid = flags.accountSid;
}
if (flags.authToken) {
debug('Override authToken with value from flag');
authToken = flags.authToken;
}
return {
accountSid,
authToken,
};
}