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