
var path = require('path');
var yaml_config = require('node-yaml-config');
var _ = require('lodash');

function ymlHerokuConfigModule() {

  var HEROKU_VARS_SUPPORT = [
    'user', 'password', 'url', 'pipeline', 'jobs', 'key', 'secret', 'account'
  ];

  var create = function (configKey) {

    var config;
    var id = configKey;

    init();

    var get = function () {
      return config[id];
    };

    function init() {

      try {
        config = yaml_config.load('config.yml');
      } catch (err) {
        console.log('could not read yml, trying Heroku vars', err);

        config = {};
        config[id] = {};
        _.each(HEROKU_VARS_SUPPORT, function(varName) {
          config[id][varName] = process.env[id.toUpperCase() + '_' + varName.toUpperCase()];
        });

        if(config[id].jobs) {
          config[id].jobs = config[id].jobs.split(',');
        }

        if (!config[id].user || !config[id].password || !config[id].url) {
          console.log('ERROR: Not enough values in ' + id + ' config, cannot get data');
        }

      }

      config[id] = config[id] || { sample: true };

    }

    return {
      get: get
    };
  };

  return {
    create: create
  };
}

exports.create = ymlHerokuConfigModule().create;

var path = require('path');
var yaml_config = require('node-yaml-config');
var _ = require('lodash');

function ymlHerokuConfigModule() {

  var HEROKU_VARS_SUPPORT = [
    'user', 'password', 'url', 'pipeline', 'jobs', 'key', 'secret', 'account', 'debug'
  ];

  var create = function (configKey) {

    var config;
    var id = configKey;

    init();

    var get = function () {
      return config[id];
    };

    function init() {

      try {
        config = yaml_config.load('config.yml');
      } catch (err) {
        console.log('could not read yml, trying Heroku vars', err);

        config = {};
        config[id] = {};
        _.each(HEROKU_VARS_SUPPORT, function(varName) {
          config[id][varName] = process.env[id.toUpperCase() + '_' + varName.toUpperCase()];
        });

        if(config[id].jobs) {
          config[id].jobs = config[id].jobs.split(',');
        }

        if (!config[id].user || !config[id].password || !config[id].url) {
          console.log('ERROR: Not enough values in ' + id + ' config, cannot get data');
        }

      }

      config[id] = config[id] || { sample: true };

    }

    return {
      get: get
    };
  };

  return {
    create: create
  };
}

exports.create = ymlHerokuConfigModule().create;
Support debug logging for gocd
Support debug logging for gocd
