UNPKG

5.55 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs-extra');
4const path = require('path');
5const os = require('os');
6
7const yaml = require('js-yaml');
8const debug = require('debug')('fun:profile');
9
10const { red } = require('colors');
11
12const _ = require('lodash');
13
14const dotenv = require('dotenv').config();
15
16function filterNotExistParameters(profile) {
17 const propsRequired = ['accountId', 'accessKeyId', 'accessKeySecret', 'defaultRegion'];
18 return propsRequired.filter(paramter => {
19 return !profile.hasOwnProperty(paramter);
20 });
21}
22
23function extract(regex, endpoint) {
24 var matchs = endpoint.match(regex);
25 if (matchs) {
26 return matchs[1];
27 }
28 return null;
29}
30
31function extractAccountId(endpoint) {
32 return extract(/^https?:\/\/([^.]+)\..+$/, endpoint);
33}
34
35function extractRegion(endpoint) {
36 return extract(/^https?:\/\/[^.]+\.([^.]+)\..+$/, endpoint);
37}
38
39function extractProtocol(endpoint) {
40 const array = _.split(endpoint, ':', 1);
41 return array.length !== 0 ? array[0] : '';
42}
43
44
45async function getProfileFromFile() {
46 const profPath = path.join(os.homedir(), '.fcli/config.yaml');
47 const isExists = await fs.pathExists(profPath);
48
49 var profile = {};
50
51 if (!isExists) {
52 return profile;
53 }
54
55 const profContent = await fs.readFile(profPath, 'utf8');
56 const profYml = yaml.safeLoad(profContent);
57
58 if (profYml.endpoint) {
59 profile.accountId = extractAccountId(profYml.endpoint);
60 profile.defaultRegion = extractRegion(profYml.endpoint);
61 profile.protocol = extractProtocol(profYml.endpoint);
62 }
63
64 if (profYml.access_key_id) {
65 profile.accessKeyId = profYml.access_key_id;
66 }
67
68 if (profYml.access_key_secret) {
69 profile.accessKeySecret = profYml.access_key_secret;
70 }
71
72 if (profYml.report !== undefined) {
73 profile.report = profYml.report;
74 }
75
76 profile.timeout = profYml.timeout || 10;
77 profile.retries = profYml.retries || 3;
78
79 return profile;
80}
81
82async function getProfileFromEnv() {
83 const profile = await getProfileFromFile();
84
85 if (process.env.ACCOUNT_ID) {
86 debug('try to get ACCOUNT_ID from environment variable');
87 profile.accountId = process.env.ACCOUNT_ID;
88 }
89
90 if (process.env.DEFAULT_REGION) {
91 debug('try to get DEFAULT_REGION from environment variable');
92 profile.defaultRegion = process.env.DEFAULT_REGION;
93 }
94
95 if (process.env.REGION) {
96 debug('try to get REGION from environment variable');
97 profile.defaultRegion = process.env.REGION;
98 }
99
100 if (process.env.ACCESS_KEY_ID) {
101 debug('try to get ACCESS_KEY_ID from environment variable');
102 profile.accessKeyId = process.env.ACCESS_KEY_ID;
103 }
104
105 if (process.env.ACCESS_KEY_SECRET) {
106 debug('try to get ACCESS_KEY_SECRET from environment variable');
107 profile.accessKeySecret = process.env.ACCESS_KEY_SECRET;
108 }
109
110 if (process.env.TIMEOUT) {
111 debug('try to get TIMEOUT from environment variable');
112 profile.timeout = process.env.TIMEOUT;
113 }
114
115 if (process.env.RETRIES) {
116 debug('try to get RETRIES from environment variable');
117 profile.retries = process.env.RETRIES;
118 }
119
120 if (process.env.FC_ENDPOINT) {
121 debug('try to get ENDPOINT from environment variable');
122 profile.fcEndpoint = process.env.FC_ENDPOINT;
123 }
124
125 return profile;
126}
127
128async function getProfileFromDotEnv() {
129 const profile = await getProfileFromEnv();
130
131 if (dotenv) {
132 if (dotenv.error) {
133 debug('could not found .env file, so ignore'); // dotenv file may not exist.
134 return profile;
135 }
136
137 const parsed = dotenv.parsed;
138
139 if (parsed['ACCOUNT_ID']) {
140 debug('try to get ACCOUNT_ID from dotenv variable');
141 profile.accountId = parsed['ACCOUNT_ID'];
142 }
143
144 if (parsed['DEFAULT_REGION']) {
145 debug('try to get DEFAULT_REGION from dotenv variable');
146 profile.defaultRegion = parsed['DEFAULT_REGION'];
147 }
148
149 if (parsed['REGION']) {
150 debug('try to get REGION from dotenv variable');
151 profile.defaultRegion = parsed['REGION'];
152 }
153
154 if (parsed['ACCESS_KEY_ID']) {
155 debug('try to get ACCESS_KEY_ID from dotenv variable');
156 profile.accessKeyId = parsed['ACCESS_KEY_ID'];
157 }
158
159 if (parsed['ACCESS_KEY_SECRET']) {
160 debug('try to get ACCESS_KEY_SECRET from dotenv variable');
161 profile.accessKeySecret = parsed['ACCESS_KEY_SECRET'];
162 }
163
164 if (parsed['TIMEOUT']) {
165 debug('try to get TIMEOUT from dotenv variable');
166 profile.timeout = parsed['TIMEOUT'];
167 }
168
169 if (parsed['RETRIES']) {
170 debug('try to get RETRIES from dotenv variable');
171 profile.retries = parsed['RETRIES'];
172 }
173
174 if (parsed['FC_ENDPOINT']) {
175 debug('try to get FC_ENDPOINT from dotenv variable');
176 profile.fcEndpoint = parsed['FC_ENDPOINT'];
177 }
178 }
179
180 return profile;
181}
182
183async function getProfile() {
184 const profile = await getProfileFromDotEnv();
185 const notExistParams = filterNotExistParameters(profile);
186
187 if (!_.isEmpty(notExistParams)) {
188 console.error(red(''));
189 throw new Error(red(`Fun is not properly configured. Missing '${notExistParams.join(', ')}' configuration. Please run 'fun config' first.\nRefer to https://github.com/alibaba/funcraft/blob/master/docs/usage/getting_started-zh.md#配置 for more help.`));
190 }
191
192 return profile;
193}
194
195function mark(source) {
196 if (!source) { return source; }
197
198 const subStr = source.slice(-4);
199 return `***********${subStr}`;
200}
201
202function isShortDateStr(str) { //example:2008-07-22
203 var dateFormat = /^\d{4}-\d{2}-\d{2}$/;
204 if (dateFormat.test(str)) {
205 return true;
206 }
207 return false;
208}
209module.exports = { getProfile, getProfileFromFile, mark, isShortDateStr };
\No newline at end of file