forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
74 lines (61 loc) · 2.08 KB
/
config.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
var cfreader = require('./configfile');
var path = require('path');
var logger = require('./logger');
var config = exports;
config.get = function(name, type, cb, options) {
var a = this.arrange_args([name, type, cb, options]);
if (!a[1]) a[1] = 'value';
var full_path = path.resolve(cfreader.config_path, a[0]);
var results = cfreader.read_config(full_path, a[1], a[2], a[3]);
// Pass arrays by value to prevent config being modified accidentally.
if (Array.isArray(results)) {
return results.slice();
}
return results;
};
/* ways get() can be called:
config.get('thing');
config.get('thing', type);
config.get('thing', cb);
config.get('thing', cb, options);
config.get('thing', options);
config.get('thing', type, cb);
config.get('thing', type, options);
config.get('thing', type, cb, options);
*/
config.arrange_args = function (args) {
var fs_name = args.shift();
var fs_type = null;
var cb, options;
for (var i=0; i < args.length; i++) {
if (args[i] === undefined) continue;
var what_is_it = args[i];
switch (typeof args[i]) { // what is it?
case 'function':
cb = args[i];
break;
case 'object':
options = args[i];
break;
case 'string':
if (/^(ini|value|list|data|json|yaml|binary)$/.test(args[i])) {
fs_type = args[i];
break;
}
console.log('unknown string:' + args[i]);
break;
}
// console.log('unknown arg:' + args[i] + ', typeof: ' +
// typeof args[i]);
}
if (!fs_type) {
if (/\.json$/.test(fs_name)) fs_type = 'json';
else if (/\.yaml$/.test(fs_name)) fs_type = 'yaml';
else if (/\.ini$/.test(fs_name)) fs_type = 'ini';
else fs_type = 'value';
}
return [fs_name, fs_type, cb, options];
};
// Load smtp.json or smtp.yaml as early as possible
var cfg = config.get('smtp.json');