-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (51 loc) · 1.65 KB
/
index.js
File metadata and controls
63 lines (51 loc) · 1.65 KB
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
var _ = require('lodash')
var argv = require('optimist').argv
var mongodb = require('mongoskin')
var rc = require('rc')
module.exports = function(opts, defaultConfig, cb) {
var defaultConfig = defaultConfig || {}
if (!opts.name)
return cb(new Error("must specify a name in options object"), null)
if (typeof cb !== 'function')
return cb(new Error("must supply a callback function"), null)
// Collection to read config from.
var configCollection = argv.configCollection || opts.configCollection || "config"
// Database URL to read config from.
var configUrl = argv.configUrl || opts.configUrl
// Query to findOne the config with. Defaults to empty.
var configQuery
try {
configQuery = JSON.parse(argv.configQuery)
} catch(e) {
configQuery = opts.configQuery || {}
}
var db = null
if (!configUrl) {
// No mongodb URL - passthru
return cb(null, rc(opts.name, defaultConfig))
}
var gotConfig = function(err, config) {
if (err) {
return cb(new Error("error loading config from MongoDB: " + err, null))
}
db.close()
var config = _.extend(defaultConfig, config)
return cb(null, rc(opts.name, config))
}
var getConfig = function(url, collection, query, cb) {
try {
db = mongodb.db(url, {safe:true})
db.collection(collection).findOne(query, cb)
} catch(e) {
return cb(e, null)
}
}
getConfig(configUrl, configCollection, configQuery, gotConfig)
}
// If run from CLI by tests
if (require.main === module) {
module.exports({name:"mongorctest"}, {override:123}, function(err, config) {
if (err) throw err
console.log(JSON.stringify(config, null, '\t'))
})
}