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:
Andrew Stewart 2014-02-19 11:53:49 -08:00
parent e0f4246306
commit d194ca76d3
6 changed files with 77 additions and 7 deletions

32
dist/config.js vendored Normal file
View File

@ -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);

2
dist/cylon.js vendored
View File

@ -13,6 +13,8 @@
__slice = [].slice,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
require('./config');
require('./utils');
require('./logger');

5
dist/robot.js vendored
View File

@ -72,7 +72,6 @@
this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test");
this.testing = process.env['CYLON_TEST'];
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
@ -215,7 +214,7 @@
connection: connection,
extraParams: opts
});
if (this.robot.testing != null) {
if (Config.testing_mode) {
testAdaptor = this.robot.requireAdaptor('test').adaptor({
name: adaptorName,
connection: connection,
@ -251,7 +250,7 @@
device: device,
extraParams: opts
});
if (this.robot.testing != null) {
if (Config.testing_mode) {
testDriver = this.robot.requireDriver('test').driver({
name: driverName,
device: device,

38
src/config.coffee Normal file
View File

@ -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

View File

@ -8,6 +8,7 @@
'use strict'
require './config'
require './utils'
require './logger'
require './driver'

View File

@ -66,8 +66,6 @@ namespace 'Cylon', ->
@registerDriver "./test/ping", "ping"
@registerDriver "./test/test-driver", "test"
@testing = process.env['CYLON_TEST']
@initConnections(opts.connection or opts.connections)
@initDevices(opts.device or opts.devices)
@work = opts.work or -> (Logger.info "No work yet")
@ -187,7 +185,7 @@ namespace 'Cylon', ->
connection: connection,
extraParams: opts
if @robot.testing?
if Config.testing_mode
testAdaptor = @robot.requireAdaptor('test').adaptor
name: adaptorName,
connection: connection,
@ -231,7 +229,7 @@ namespace 'Cylon', ->
device: device,
extraParams: opts
if @robot.testing?
if Config.testing_mode
testDriver = @robot.requireDriver('test').driver
name: driverName,
device: device,