Skip to content

Commit 4701f62

Browse files
author
Ilya Radchenko
committed
Cleanup libconfig
1 parent d70451d commit 4701f62

File tree

1 file changed

+79
-53
lines changed

1 file changed

+79
-53
lines changed

lib/libconfig.js

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@ var everypaas = require('everypaas');
55
var _ = require('lodash');
66

77
var envDefaults = {
8-
host: process.env.HOST || '0.0.0.0',
9-
port: process.env.PORT || 3000
10-
, server_name : 'http://localhost'
8+
host: process.env.HOST || '0.0.0.0',
9+
port: process.env.PORT || 3000,
10+
server_name: 'http://localhost',
1111

12-
, db_uri : everypaas.getMongodbUrl() || 'mongodb://localhost/strider-foss'
12+
db_uri: everypaas.getMongodbUrl() || 'mongodb://localhost/strider-foss',
1313

14-
, smtp_host : ''
15-
, smtp_port : 587
16-
, smtp_user : ''
17-
, smtp_pass : ''
18-
, smtp_from : 'Strider <noreply@stridercd.com>'
14+
smtp_host: '',
15+
smtp_port: 587,
16+
smtp_user: '',
17+
smtp_pass: '',
18+
smtp_from: 'Strider <noreply@stridercd.com>',
1919

20-
, enablePty : false
21-
, extpath : 'node_modules'
22-
, session_secret : '8L8BudMkqBUqrz'
20+
enablePty: false,
21+
extpath: 'node_modules',
22+
session_secret: '8L8BudMkqBUqrz',
2323

24-
, github_app_id : ''
25-
, github_secret : ''
24+
github_app_id: '',
25+
github_secret: '',
2626

27-
, cors : false
28-
, body_parser_limit: false
27+
cors: false,
28+
body_parser_limit: false
2929
};
3030

3131
envDefaults.server_name = envDefaults.server_name + ':' + envDefaults.port;
@@ -54,20 +54,25 @@ module.exports = {
5454
addPlugins: addPlugins,
5555
camel: camel,
5656
getConfig: getConfig
57-
}
57+
};
5858

5959
// main function. Get the config, using rc
6060
function getConfig() {
61-
process.env = filterEnv(deprecated(process.env), envDefaults)
62-
var rc = require('rc')('strider', defaults)
63-
if (!rc.smtp) rc.smtp = smtp(rc)
61+
process.env = filterEnv(deprecated(process.env), envDefaults);
62+
63+
var rc = require('rc')('strider', defaults);
64+
65+
if (!rc.smtp) rc.smtp = smtp(rc);
6466
if (!rc.smtp) rc.stubSmtp = true;
65-
addPlugins(rc, process.env)
67+
68+
addPlugins(rc, process.env);
69+
6670
// BACK COMPAT until we get strider config into plugins...
6771
if (rc.plugins.github) {
68-
rc.plugins.github.hostname = rc.server_name
72+
rc.plugins.github.hostname = rc.server_name;
6973
}
70-
return rc
74+
75+
return rc;
7176
}
7277

7378
function camel(words) {
@@ -77,84 +82,105 @@ function camel(words) {
7782
}
7883

7984
function addPlugins(rc, env) {
80-
var parts
81-
if (!rc.plugins) rc.plugins = {}
85+
var parts;
86+
87+
if (!rc.plugins) rc.plugins = {};
88+
8289
for (var key in env) {
8390
if (!key.match(/^plugin_/i)) continue;
91+
8492
parts = key.toLowerCase().split('_')
93+
8594
if (parts.length === 2) {
8695
try {
87-
rc.plugins[parts[1]] = JSON.parse(env[key])
96+
rc.plugins[parts[1]] = JSON.parse(env[key]);
8897
} catch (e) {
89-
console.warn("Ignoring config because it's not valid JSON: ", key, env[key])
98+
console.warn('Ignoring config because it\'s not valid JSON: ', key, env[key]);
9099
}
100+
91101
continue;
92102
}
93-
if (!rc.plugins[parts[1]]) rc.plugins[parts[1]] = {}
94-
rc.plugins[parts[1]][camel(parts.slice(2))] = env[key]
103+
if (!rc.plugins[parts[1]]) {
104+
rc.plugins[parts[1]] = {};
105+
}
106+
107+
rc.plugins[parts[1]][camel(parts.slice(2))] = env[key];
95108
}
96109
}
97110

98111
// Filter process.env.FOO to process.env.strider_foo for rc's benefit
99112
function filterEnv(env, defaults) {
100-
var res = {}
113+
var res = {};
114+
101115
for (var k in env) {
102116
if (defaults[k.toLowerCase()] !== undefined) {
103-
res["strider_" + k.toLowerCase()] = env[k]
117+
res['strider_' + k.toLowerCase()] = env[k];
104118
} else {
105-
res[k] = env[k]
119+
res[k] = env[k];
106120
}
107121
}
108-
return res
122+
123+
return res;
109124
}
110125

111126
function deprecated(env) {
112-
var nenv = _.extend({}, env)
127+
var nenv = _.extend({}, env);
128+
113129
if (env.APP_ID) {
114-
console.warn("WARNING: You are using APP_ID to configure Github OAuth application id.")
115-
console.warn("This name has been deprecated. Please use PLUGIN_GITHUB_APP_ID instead.\n")
116-
nenv.PLUGIN_GITHUB_APP_ID = env.APP_ID
130+
console.warn('WARNING: You are using APP_ID to configure Github OAuth application id.');
131+
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_ID instead.\n');
132+
133+
nenv.PLUGIN_GITHUB_APP_ID = env.APP_ID;
117134
}
135+
118136
if (env.APP_SECRET) {
119-
console.warn("WARNING: You are using APP_SECRET to configure Github OAuth application secret.")
120-
console.warn("This name has been deprecated. Please use PLUGIN_GITHUB_APP_SECRET instead.\n")
121-
nenv.PLUGIN_GITHUB_APP_SECRET = env.APP_SECRET
137+
console.warn('WARNING: You are using APP_SECRET to configure Github OAuth application secret.');
138+
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_SECRET instead.\n');
139+
140+
nenv.PLUGIN_GITHUB_APP_SECRET = env.APP_SECRET;
122141
}
142+
123143
if (env.GITHUB_APP_ID) {
124-
console.warn("WARNING: You are using GITHUB_APP_ID to configure Github OAuth application id.")
125-
console.warn("This name has been deprecated. Please use PLUGIN_GITHUB_APP_ID instead.\n")
126-
nenv.PLUGIN_GITHUB_APP_ID = env.GITHUB_APP_ID
144+
console.warn('WARNING: You are using GITHUB_APP_ID to configure Github OAuth application id.');
145+
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_ID instead.\n');
146+
147+
nenv.PLUGIN_GITHUB_APP_ID = env.GITHUB_APP_ID;
127148
}
149+
128150
if (env.GITHUB_SECRET) {
129-
console.warn("WARNING: You are using GITHUB_SECRET to configure Github OAuth application secret.")
130-
console.warn("This name has been deprecated. Please use PLUGIN_GITHUB_APP_SECRET instead.\n")
131-
nenv.PLUGIN_GITHUB_APP_SECRET = env.GITHUB_SECRET
151+
console.warn('WARNING: You are using GITHUB_SECRET to configure Github OAuth application secret.');
152+
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_SECRET instead.\n');
153+
154+
nenv.PLUGIN_GITHUB_APP_SECRET = env.GITHUB_SECRET;
132155
}
156+
133157
if (env.GITHUB_API_ENDPOINT) {
134-
console.warn("WARNING: You are using GITHUB_API_ENDPOINT to configure Github API base URL.")
135-
console.warn("This name has been deprecated. Please use PLUGIN_GITHUB_API_BASE instead.\n")
136-
nenv.PLUGIN_GITHUB_API_BASE = env.GITHUB_API_ENDPOINT
158+
console.warn('WARNING: You are using GITHUB_API_ENDPOINT to configure Github API base URL.');
159+
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_API_BASE instead.\n');
160+
161+
nenv.PLUGIN_GITHUB_API_BASE = env.GITHUB_API_ENDPOINT;
137162
}
138-
return nenv
163+
164+
return nenv;
139165
}
140166

141167
function smtp(rc) {
142168
if (!rc.smtp_host) {
143169
return;
144170
}
145-
171+
146172
var options = {
147173
host: rc.smtp_host,
148174
port: rc.smtp_port,
149175
from: rc.smtp_from
150176
};
151-
177+
152178
if (rc.smtp_user) {
153179
options.auth = {
154180
user: rc.smtp_user,
155181
pass: rc.smtp_pass
156182
}
157183
}
158-
184+
159185
return options;
160186
}

0 commit comments

Comments
 (0)