1 | 'use strict';
|
2 |
|
3 | const fs = require('fs-extra');
|
4 | const path = require('path');
|
5 | const os = require('os');
|
6 |
|
7 | const yaml = require('js-yaml');
|
8 | const debug = require('debug')('fun:profile');
|
9 |
|
10 | const { red } = require('colors');
|
11 |
|
12 | const _ = require('lodash');
|
13 |
|
14 | const dotenv = require('dotenv').config();
|
15 |
|
16 | function filterNotExistParameters(profile) {
|
17 | const propsRequired = ['accountId', 'accessKeyId', 'accessKeySecret', 'defaultRegion'];
|
18 | return propsRequired.filter(paramter => {
|
19 | return !profile.hasOwnProperty(paramter);
|
20 | });
|
21 | }
|
22 |
|
23 | function extract(regex, endpoint) {
|
24 | var matchs = endpoint.match(regex);
|
25 | if (matchs) {
|
26 | return matchs[1];
|
27 | }
|
28 | return null;
|
29 | }
|
30 |
|
31 | function extractAccountId(endpoint) {
|
32 | return extract(/^https?:\/\/([^.]+)\..+$/, endpoint);
|
33 | }
|
34 |
|
35 | function extractRegion(endpoint) {
|
36 | return extract(/^https?:\/\/[^.]+\.([^.]+)\..+$/, endpoint);
|
37 | }
|
38 |
|
39 | function extractProtocol(endpoint) {
|
40 | const array = _.split(endpoint, ':', 1);
|
41 | return array.length !== 0 ? array[0] : '';
|
42 | }
|
43 |
|
44 |
|
45 | async 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 |
|
82 | async 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 |
|
128 | async function getProfileFromDotEnv() {
|
129 | const profile = await getProfileFromEnv();
|
130 |
|
131 | if (dotenv) {
|
132 | if (dotenv.error) {
|
133 | debug('could not found .env file, so ignore');
|
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 |
|
183 | async 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 |
|
195 | function mark(source) {
|
196 | if (!source) { return source; }
|
197 |
|
198 | const subStr = source.slice(-4);
|
199 | return `***********${subStr}`;
|
200 | }
|
201 |
|
202 | function isShortDateStr(str) {
|
203 | var dateFormat = /^\d{4}-\d{2}-\d{2}$/;
|
204 | if (dateFormat.test(str)) {
|
205 | return true;
|
206 | }
|
207 | return false;
|
208 | }
|
209 | module.exports = { getProfile, getProfileFromFile, mark, isShortDateStr }; |
\ | No newline at end of file |