1 | 'use strict';
|
2 |
|
3 |
|
4 | const fs = require('fs');
|
5 | const util = require('util');
|
6 | const path = require('path');
|
7 |
|
8 | const yaml = require('js-yaml');
|
9 | const debug = require('debug')('fun:conf');
|
10 | const getProfile = require('./profile').getProfile;
|
11 |
|
12 | const readFile = util.promisify(fs.readFile);
|
13 | const exists = util.promisify(fs.exists);
|
14 |
|
15 | async function getConf(rootDir) {
|
16 | const profile = await getProfile();
|
17 |
|
18 | var confPath = path.join(rootDir, 'faas.yml');
|
19 | var isexists = await exists(confPath);
|
20 |
|
21 | if (!isexists) {
|
22 |
|
23 | confPath = path.join(rootDir, 'faas.yaml');
|
24 | isexists = await exists(confPath);
|
25 | }
|
26 |
|
27 | if (!isexists) {
|
28 | throw new Error('Current folder not a Faas project\nThe folder must contains faas.yml or faas.yaml');
|
29 | }
|
30 |
|
31 | const confContent = await readFile(confPath, 'utf8');
|
32 | const conf = yaml.safeLoad(confContent);
|
33 |
|
34 | if (!conf.accountid) {
|
35 | debug('try to get accountId from profile');
|
36 | conf.accountid = profile.accountId;
|
37 | }
|
38 |
|
39 | if (!conf.accessKeyId) {
|
40 | debug('try to get accessKeyId from profile');
|
41 | conf.accessKeyId = profile.accessKeyId;
|
42 | }
|
43 |
|
44 | if (!conf.accessKeySecret) {
|
45 | debug('try to get accessKeySecret from profile');
|
46 | conf.accessKeySecret = profile.accessKeySecret;
|
47 | }
|
48 |
|
49 | debug('exitst config: %j', conf);
|
50 | return conf;
|
51 | }
|
52 |
|
53 | module.exports = getConf;
|