1 | var API = require('./api')
|
2 |
|
3 | var RESERVED_NAMES = {
|
4 | FAAS_ENV: true
|
5 | , FAAS_API_KEY: true
|
6 | , FAAS_API_SECRET: true
|
7 | }
|
8 |
|
9 | ////
|
10 | // faas.env
|
11 | //
|
12 | // faas.env(...) loads configuration settings from faas.io for the
|
13 | // credentials and environment name specified, and merges the name/value
|
14 | // pairs returned with the local process environment variables. It makes
|
15 | // the results available through faas.env[] and as an argument to the
|
16 | // callback function provided.
|
17 | //
|
18 | // Usage:
|
19 | //
|
20 | // 1. Set external environment variables for FAAS_API_KEY, FAAS_API_SECRET,
|
21 | // and (optionally) FAAS_ENV (the name of the environment to load).
|
22 | // Access settings via faas.env[] (faas.env[] doesn't have to be used
|
23 | // directly in the callback scope, as it would be available anywhere
|
24 | // via require('faas').env[]).
|
25 | //
|
26 | // faas.env(function(){
|
27 | // // Access env settings in here via faas.env.SETTING
|
28 | // })
|
29 | //
|
30 | // 2. Pass options to faas.env() instead of relying on
|
31 | // process.env.FAAS_API_KEY, etc.
|
32 | //
|
33 | // faas.env({ apiKey: XXX, apiSecret: XXX, env: XXX }, function(){...})
|
34 | //
|
35 | // The process.env[] variables FAAS_API_KEY, FAAS_API_SECRET, FAAS_ENV will
|
36 | // be used to fill in missing options.
|
37 | //
|
38 | // 3. With either of the above methods, you can also request the faas.env
|
39 | // object as an argument to your callback. This could theoretically be
|
40 | // useful if you wanted to load multiple different environments
|
41 | // into your app, which would clash in the module namespace.
|
42 | //
|
43 | // faas.env(function(faasEnv){
|
44 | // // Use faasEnv.SETTING instead of faas.env.SETTING
|
45 | // })
|
46 | //
|
47 | // faas.env({ apiKey: ... }, function(faasEnv){ ... })
|
48 | //
|
49 | ////
|
50 | var exportedEnv
|
51 | exports.env = exportedEnv = function(options, cb_env){
|
52 | // Figure out if we have options or a callback.
|
53 | if ((typeof options === 'function') && !cb_env) {
|
54 | // Looks like the first argument was a callback.
|
55 | cb_env = options
|
56 | options = {}
|
57 | }
|
58 |
|
59 | options = options || {} // Provide a default object for ease of use.
|
60 | // Set up the query parameters for the API call.
|
61 | var query = {}
|
62 | if (options.apiKey) { query.apiKey = options.apiKey }
|
63 | if (options.apiSecret) { query.apiSecret = options.apiSecret }
|
64 | var envName = options.env || process.env.FAAS_ENV
|
65 | if (envName) { query.env = envName }
|
66 |
|
67 | var apiOpts = { url: { pathname: '/config/getSettings', query: query } }
|
68 |
|
69 | API.api(apiOpts, function(err, faasEnv){
|
70 | if (err) { error_cb(err); return }
|
71 |
|
72 | mergeProcessEnv(faasEnv)
|
73 | mergeApiCredentials(faasEnv, options)
|
74 | storeAtTopLevel(faasEnv)
|
75 |
|
76 | if (cb_env) { cb_env(faasEnv) }
|
77 | })
|
78 |
|
79 | }
|
80 |
|
81 | var mergeProcessEnv = function(faasEnv){
|
82 | // Apply the process environment to this object.
|
83 | for (var p in process.env) { if (process.env.hasOwnProperty(p)) {
|
84 | faasEnv[p] = process.env[p]
|
85 | // Alias FAAS_XXX to XXX.
|
86 | if (p.slice(0,5) === 'FAAS_' && !RESERVED_NAMES[p]) {
|
87 | faasEnv[p.slice(5)] = process.env[p]
|
88 | }
|
89 | }}
|
90 | }
|
91 |
|
92 | var mergeApiCredentials = function(faasEnv, options){
|
93 | // Add the API_KEY and API_SECRET to the env.
|
94 | if (options.apiKey) { faasEnv.FAAS_API_KEY = options.apiKey }
|
95 | if (options.apiSecret) { faasEnv.FAAS_API_SECRET = options.apiSecret }
|
96 | }
|
97 |
|
98 | var storeAtTopLevel = function(faasEnv){
|
99 | for (var p in faasEnv) { if (faasEnv.hasOwnProperty(p)) {
|
100 | exportedEnv[p] = faasEnv[p]
|
101 | }}
|
102 | }
|
103 |
|
104 | var error_cb = function(err){
|
105 | console.log('ERROR LOADING FAAS ENV:', err)
|
106 | console.log('Exiting due to error.')
|
107 | process.exit(1)
|
108 | }
|
109 |
|
110 |
|
111 | exportedEnv.detailsInfo = function(){
|
112 | var info = {}
|
113 | var dets = exportedEnv.FAAS_DETAILS
|
114 | for (var key in dets) { if (dets.hasOwnProperty(key)) {
|
115 | info[key] = [ exportedEnv[key], dets[key] ]
|
116 | }}
|
117 | return info
|
118 | }
|