Add Config module to load config from env
Better to keep configuration loading in one place than spread throughout Cylon.
This commit is contained in:
parent
e0f4246306
commit
d194ca76d3
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* cylon configuration loader
|
||||||
|
* cylonjs.com
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013 The Hybrid Group
|
||||||
|
* Licensed under the Apache 2.0 license.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var fetch, namespace;
|
||||||
|
|
||||||
|
namespace = require('node-namespace');
|
||||||
|
|
||||||
|
fetch = function(variable, defaultValue) {
|
||||||
|
if (defaultValue == null) {
|
||||||
|
defaultValue = false;
|
||||||
|
}
|
||||||
|
if (process.env[variable] != null) {
|
||||||
|
return process.env[variable];
|
||||||
|
} else {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace('Config', function() {
|
||||||
|
return this.testing_mode = fetch("CYLON_TEST", false);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = Config;
|
||||||
|
|
||||||
|
}).call(this);
|
|
@ -13,6 +13,8 @@
|
||||||
__slice = [].slice,
|
__slice = [].slice,
|
||||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||||
|
|
||||||
|
require('./config');
|
||||||
|
|
||||||
require('./utils');
|
require('./utils');
|
||||||
|
|
||||||
require('./logger');
|
require('./logger');
|
||||||
|
|
|
@ -72,7 +72,6 @@
|
||||||
this.registerAdaptor("./test/test-adaptor", "test");
|
this.registerAdaptor("./test/test-adaptor", "test");
|
||||||
this.registerDriver("./test/ping", "ping");
|
this.registerDriver("./test/ping", "ping");
|
||||||
this.registerDriver("./test/test-driver", "test");
|
this.registerDriver("./test/test-driver", "test");
|
||||||
this.testing = process.env['CYLON_TEST'];
|
|
||||||
this.initConnections(opts.connection || opts.connections);
|
this.initConnections(opts.connection || opts.connections);
|
||||||
this.initDevices(opts.device || opts.devices);
|
this.initDevices(opts.device || opts.devices);
|
||||||
this.work = opts.work || function() {
|
this.work = opts.work || function() {
|
||||||
|
@ -215,7 +214,7 @@
|
||||||
connection: connection,
|
connection: connection,
|
||||||
extraParams: opts
|
extraParams: opts
|
||||||
});
|
});
|
||||||
if (this.robot.testing != null) {
|
if (Config.testing_mode) {
|
||||||
testAdaptor = this.robot.requireAdaptor('test').adaptor({
|
testAdaptor = this.robot.requireAdaptor('test').adaptor({
|
||||||
name: adaptorName,
|
name: adaptorName,
|
||||||
connection: connection,
|
connection: connection,
|
||||||
|
@ -251,7 +250,7 @@
|
||||||
device: device,
|
device: device,
|
||||||
extraParams: opts
|
extraParams: opts
|
||||||
});
|
});
|
||||||
if (this.robot.testing != null) {
|
if (Config.testing_mode) {
|
||||||
testDriver = this.robot.requireDriver('test').driver({
|
testDriver = this.robot.requireDriver('test').driver({
|
||||||
name: driverName,
|
name: driverName,
|
||||||
device: device,
|
device: device,
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
###
|
||||||
|
* cylon configuration loader
|
||||||
|
* cylonjs.com
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013 The Hybrid Group
|
||||||
|
* Licensed under the Apache 2.0 license.
|
||||||
|
###
|
||||||
|
|
||||||
|
namespace = require 'node-namespace'
|
||||||
|
|
||||||
|
# Public: Fetches a variable from the environment, returning a provided value if
|
||||||
|
# it's not set.
|
||||||
|
#
|
||||||
|
# variable - variable to fetch from the environment
|
||||||
|
# defaultValue - value to return if the ENV variable isn't set
|
||||||
|
#
|
||||||
|
# Examples
|
||||||
|
#
|
||||||
|
# process.env["CYLON_TEST"] #=> undefined
|
||||||
|
# fetch("CYLON_TEST", "not set")
|
||||||
|
# #=> "not set"
|
||||||
|
#
|
||||||
|
# process.env["CYLON_TEST"] #=> false
|
||||||
|
# fetch("CYLON_TEST", true)
|
||||||
|
# #=> false
|
||||||
|
#
|
||||||
|
# process.env["CYLON_TEST"] #=> true
|
||||||
|
# fetch("CYLON_TEST", false)
|
||||||
|
# #=> true
|
||||||
|
#
|
||||||
|
# Returns the env var or default value
|
||||||
|
fetch = (variable, defaultValue = false) ->
|
||||||
|
if process.env[variable]? then process.env[variable] else defaultValue
|
||||||
|
|
||||||
|
namespace 'Config', ->
|
||||||
|
@testing_mode = fetch("CYLON_TEST", false)
|
||||||
|
|
||||||
|
module.exports = Config
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
require './config'
|
||||||
require './utils'
|
require './utils'
|
||||||
require './logger'
|
require './logger'
|
||||||
require './driver'
|
require './driver'
|
||||||
|
|
|
@ -66,8 +66,6 @@ namespace 'Cylon', ->
|
||||||
@registerDriver "./test/ping", "ping"
|
@registerDriver "./test/ping", "ping"
|
||||||
@registerDriver "./test/test-driver", "test"
|
@registerDriver "./test/test-driver", "test"
|
||||||
|
|
||||||
@testing = process.env['CYLON_TEST']
|
|
||||||
|
|
||||||
@initConnections(opts.connection or opts.connections)
|
@initConnections(opts.connection or opts.connections)
|
||||||
@initDevices(opts.device or opts.devices)
|
@initDevices(opts.device or opts.devices)
|
||||||
@work = opts.work or -> (Logger.info "No work yet")
|
@work = opts.work or -> (Logger.info "No work yet")
|
||||||
|
@ -187,7 +185,7 @@ namespace 'Cylon', ->
|
||||||
connection: connection,
|
connection: connection,
|
||||||
extraParams: opts
|
extraParams: opts
|
||||||
|
|
||||||
if @robot.testing?
|
if Config.testing_mode
|
||||||
testAdaptor = @robot.requireAdaptor('test').adaptor
|
testAdaptor = @robot.requireAdaptor('test').adaptor
|
||||||
name: adaptorName,
|
name: adaptorName,
|
||||||
connection: connection,
|
connection: connection,
|
||||||
|
@ -231,7 +229,7 @@ namespace 'Cylon', ->
|
||||||
device: device,
|
device: device,
|
||||||
extraParams: opts
|
extraParams: opts
|
||||||
|
|
||||||
if @robot.testing?
|
if Config.testing_mode
|
||||||
testDriver = @robot.requireDriver('test').driver
|
testDriver = @robot.requireDriver('test').driver
|
||||||
name: driverName,
|
name: driverName,
|
||||||
device: device,
|
device: device,
|
||||||
|
|
Loading…
Reference in New Issue