# Code coverage report for lib/config_reader.js

## Statements: 100% (31 / 31) Branches: 100% (12 / 12) Functions: 100% (6 / 6) Lines: 100% (28 / 28) Ignored: 6 statements, 5 branches

All files » lib/ » config_reader.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
    601

    1

    1

    1
    2
    1
    1

    2
    1

    2
    2

    2
    2

    1

    2

    1
    1
    1

    1
    1

    1
    1
    1

    1
    1
    1

    1

    1

     var consul = require('consul')();

    var _ = require('underscore');

    var logger = require('./logging.js');

    /* istanbul ignore next */
    /**
     * Grab the meta config to bootstrap git2consul.
     */
    exports.read = function(params, cb) {
      if (!cb) {
        cb = params;
        params = {'key': 'git2consul/config'};
      }

      Iif (process.env.TOKEN) {
        params = _.extend(params, {'token': process.env.TOKEN})
      }

      consul.kv.get(params, function(err, item) {
        Iif (err) return cb(err);

        try {
          var config = JSON.parse(item.Value);
        } catch(e) {
          return cb('Config value is not valid JSON: ' + require('util').inspect(item));
        }
        cb(null, config, item);
      });
    };

    /* istanbul ignore next */
    /**
     * Wait for the consul config KV to change
     */
    exports.wait = function(cb) {
      exports.read(function(err, config, item) {
        Iif (err) return cb(err);

        var modify_index = item.ModifyIndex;
        logger.info("Current config index is %s, waiting for changes.", modify_index);

        var wait_for_change = function() {
          exports.read({'key': 'git2consul/config', index: item.ModifyIndex}, function(err, config, item) {
            Iif (err) return cb(err);

            Eif (modify_index !== item.ModifyIndex) {
              logger.warn("Config changed.");
              return cb(null);
            }

            process.nextTick(wait_for_change);
          });
        };

        process.nextTick(wait_for_change);
      });
    };