-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (44 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const _ = require('lodash');
module.exports = cfServices;
cfServices.filter = filter;
function cfServices(query, queryDescription) {
let services = parseServices();
if (!query) {
return services;
}
if (typeof query === 'string') {
if (!services[query]) {
throw new Error(`No service instance with name ${query}`);
}
return services[query];
}
let matches = _.filter(services, query);
if (matches.length === 1) {
return matches[0];
}
if (!queryDescription) {
queryDescription = typeof query === 'function' ? 'the filter' : JSON.stringify(query);
}
if (matches.length === 0) {
throw new Error(`No service instance matches ${queryDescription}`);
}
throw new Error(`Multiple service instances match ${queryDescription}: ` +
matches.map(m => m.name).join(', '));
}
function filter(query) {
let services = parseServices();
return _.filter(services, query);
}
function parseServices() {
if (!process.env.VCAP_SERVICES) {
throw new Error('Environment variable VCAP_SERVICES is not defined');
}
try {
var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
} catch (err) {
throw new Error('Could not parse environment variable VCAP_SERVICES: ' +
err.message);
}
return _(vcapServices).flatMap().filter('name').keyBy('name').value();
}