UNPKG

7.16 kBJavaScriptView Raw
1'use strict';
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10const fs = require('fs-extra');
11const path = require('path');
12const os = require('os');
13const yaml = require('js-yaml');
14const debug = require('debug')('fun:profile');
15const { red } = require('colors');
16const _ = require('lodash');
17const dotenv = require('dotenv').config();
18function filterNotExistParameters(profile) {
19 const propsRequired = ['accountId', 'accessKeyId', 'accessKeySecret', 'defaultRegion'];
20 return propsRequired.filter(paramter => {
21 return !profile.hasOwnProperty(paramter);
22 });
23}
24function extract(regex, endpoint) {
25 var matchs = endpoint.match(regex);
26 if (matchs) {
27 return matchs[1];
28 }
29 return null;
30}
31function extractAccountId(endpoint) {
32 return extract(/^https?:\/\/([^.]+)\..+$/, endpoint);
33}
34function extractRegion(endpoint) {
35 return extract(/^https?:\/\/[^.]+\.([^.]+)\..+$/, endpoint);
36}
37function extractProtocol(endpoint) {
38 const array = _.split(endpoint, ':', 1);
39 return array.length !== 0 ? array[0] : '';
40}
41function getProfileFromFile() {
42 return __awaiter(this, void 0, void 0, function* () {
43 const profPath = path.join(process.env.HOME || os.homedir(), '.fcli/config.yaml');
44 const isExists = yield fs.pathExists(profPath);
45 var profile = {};
46 if (!isExists) {
47 return profile;
48 }
49 const profContent = yield fs.readFile(profPath, 'utf8');
50 const profYml = yaml.safeLoad(profContent);
51 if (profYml.endpoint) {
52 profile.accountId = extractAccountId(profYml.endpoint);
53 profile.defaultRegion = extractRegion(profYml.endpoint);
54 profile.protocol = extractProtocol(profYml.endpoint);
55 }
56 if (profYml.access_key_id) {
57 profile.accessKeyId = profYml.access_key_id;
58 }
59 if (profYml.access_key_secret) {
60 profile.accessKeySecret = profYml.access_key_secret;
61 }
62 if (profYml.report !== undefined) {
63 profile.report = profYml.report;
64 }
65 profile.timeout = profYml.timeout || 10;
66 profile.retries = profYml.retries || 3;
67 return profile;
68 });
69}
70function getProfileFromEnv() {
71 return __awaiter(this, void 0, void 0, function* () {
72 const profile = yield getProfileFromFile();
73 if (process.env.ACCOUNT_ID) {
74 debug('try to get ACCOUNT_ID from environment variable');
75 profile.accountId = process.env.ACCOUNT_ID;
76 }
77 if (process.env.DEFAULT_REGION) {
78 debug('try to get DEFAULT_REGION from environment variable');
79 profile.defaultRegion = process.env.DEFAULT_REGION;
80 }
81 if (process.env.REGION) {
82 debug('try to get REGION from environment variable');
83 profile.defaultRegion = process.env.REGION;
84 }
85 if (process.env.ACCESS_KEY_ID) {
86 debug('try to get ACCESS_KEY_ID from environment variable');
87 profile.accessKeyId = process.env.ACCESS_KEY_ID;
88 }
89 if (process.env.ACCESS_KEY_SECRET) {
90 debug('try to get ACCESS_KEY_SECRET from environment variable');
91 profile.accessKeySecret = process.env.ACCESS_KEY_SECRET;
92 }
93 if (process.env.TIMEOUT) {
94 debug('try to get TIMEOUT from environment variable');
95 profile.timeout = process.env.TIMEOUT;
96 }
97 if (process.env.RETRIES) {
98 debug('try to get RETRIES from environment variable');
99 profile.retries = process.env.RETRIES;
100 }
101 if (process.env.FC_ENDPOINT) {
102 debug('try to get ENDPOINT from environment variable');
103 profile.fcEndpoint = process.env.FC_ENDPOINT;
104 }
105 return profile;
106 });
107}
108function getProfileFromDotEnv() {
109 return __awaiter(this, void 0, void 0, function* () {
110 const profile = yield getProfileFromEnv();
111 if (dotenv) {
112 if (dotenv.error) {
113 debug('could not found .env file, so ignore'); // dotenv file may not exist.
114 return profile;
115 }
116 const parsed = dotenv.parsed;
117 if (parsed['ACCOUNT_ID']) {
118 debug('try to get ACCOUNT_ID from dotenv variable');
119 profile.accountId = parsed['ACCOUNT_ID'];
120 }
121 if (parsed['DEFAULT_REGION']) {
122 debug('try to get DEFAULT_REGION from dotenv variable');
123 profile.defaultRegion = parsed['DEFAULT_REGION'];
124 }
125 if (parsed['REGION']) {
126 debug('try to get REGION from dotenv variable');
127 profile.defaultRegion = parsed['REGION'];
128 }
129 if (parsed['ACCESS_KEY_ID']) {
130 debug('try to get ACCESS_KEY_ID from dotenv variable');
131 profile.accessKeyId = parsed['ACCESS_KEY_ID'];
132 }
133 if (parsed['ACCESS_KEY_SECRET']) {
134 debug('try to get ACCESS_KEY_SECRET from dotenv variable');
135 profile.accessKeySecret = parsed['ACCESS_KEY_SECRET'];
136 }
137 if (parsed['TIMEOUT']) {
138 debug('try to get TIMEOUT from dotenv variable');
139 profile.timeout = parsed['TIMEOUT'];
140 }
141 if (parsed['RETRIES']) {
142 debug('try to get RETRIES from dotenv variable');
143 profile.retries = parsed['RETRIES'];
144 }
145 if (parsed['FC_ENDPOINT']) {
146 debug('try to get FC_ENDPOINT from dotenv variable');
147 profile.fcEndpoint = parsed['FC_ENDPOINT'];
148 }
149 }
150 return profile;
151 });
152}
153function getProfile() {
154 return __awaiter(this, void 0, void 0, function* () {
155 const profile = yield getProfileFromDotEnv();
156 const notExistParams = filterNotExistParameters(profile);
157 if (!_.isEmpty(notExistParams)) {
158 console.error(red(''));
159 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.`));
160 }
161 return profile;
162 });
163}
164function mark(source) {
165 if (!source) {
166 return source;
167 }
168 const subStr = source.slice(-4);
169 return `***********${subStr}`;
170}
171function isShortDateStr(str) {
172 var dateFormat = /^\d{4}-\d{2}-\d{2}$/;
173 return dateFormat.test(str);
174}
175module.exports = { getProfile, getProfileFromFile, mark, isShortDateStr };