UNPKG

5.43 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4const util = require('util');
5const path = require('path');
6const os = require('os');
7
8const yaml = require('js-yaml');
9const debug = require('debug')('fun:profile');
10
11const { red } = require('colors');
12
13const _ = require('lodash');
14
15const dotenv = require('dotenv').config();
16
17const readFile = util.promisify(fs.readFile);
18const exists = util.promisify(fs.exists);
19
20function isAllRequiredExist(profile) {
21 const props = ['accountId', 'accessKeyId', 'accessKeySecret'];
22 return props.every((item) => {
23 return profile.hasOwnProperty(item);
24 });
25}
26
27function extract(regex, endpoint) {
28 var matchs = endpoint.match(regex);
29 if (matchs) {
30 return matchs[1];
31 }
32 return null;
33}
34
35function extractAccountId(endpoint) {
36 return extract(/^https?:\/\/([^.]+)\..+$/, endpoint);
37}
38
39function extractRegion(endpoint) {
40 return extract(/^https?:\/\/[^.]+\.([^.]+)\..+$/, endpoint);
41}
42
43function extractProtocol(endpoint) {
44 const array = _.split(endpoint, ':', 1);
45 return array.length !== 0 ? array[0] : '';
46}
47
48
49async 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
86async 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
132async function getProfileFromDotEnv() {
133 const profile = await getProfileFromEnv();
134
135 if (dotenv) {
136 if (dotenv.error) {
137 debug('could not found .env file, so ignore'); // dotenv file may not exist.
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
187async function getProfile() {
188 const profile = await getProfileFromDotEnv();
189
190 if (!isAllRequiredExist(profile)) {
191 console.error(red(''));
192 console.error(red(''));
193 throw new Error(red('Fun is not properly configured.\nPlease run `fun config` first.'));
194 }
195
196 return profile;
197}
198
199function mark(source) {
200 if (!source) { return source; }
201
202 const subStr = source.slice(-4);
203 return `***********${subStr}`;
204}
205
206function isShortDateStr(str) { //example:2008-07-22
207 var dateFormat = /^\d{4}-\d{2}-\d{2}$/;
208 if (dateFormat.test(str)) {
209 return true;
210 }
211 return false;
212
213}
214module.exports = { getProfile, getProfileFromFile, mark, isShortDateStr };
\No newline at end of file