Code coverage report for lib/formatters/ansible.js

Statements: 100% (22 / 22)      Branches: 100% (2 / 2)      Functions: 100% (7 / 7)      Lines: 100% (22 / 22)      Ignored: none     

All files » lib/formatters/ » ansible.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 461               1     5 5 10 10 1   9           5 5 9 9 9   5 7       5 5 9 9 9       5     1
var _ = require('lodash');
 
/**
 * Format configuration data into an Ansible inventory
 * http://ansible.cc/docs/patterns.html#hosts-and-groups
 *
 * @param {Object} conf: Breaker configuration data
 */
function format(conf) {
 
  // group hosts by label
  var groups = {};
  conf.forEach(function (item) {
    item.labels.forEach(function (label) {
      if (groups[label]) {
        groups[label].push(item.host);
      } else {
        groups[label] = [ item.host ];
      }
    });
  });
 
  // sort groups by label
  var data = [];
  _.keys(groups).forEach(function (label) {
    var group = {};
    group[label] = groups[label];
    data.push(group);
  });
  data.sort(function (o1, o2) {
    return _.keys(o1)[0] > _.keys(o2)[0];
  });
 
  // format sorted groups with one line per host
  var lines = [];
  data.forEach(function (item) {
    _.keys(item).forEach(function (label) {
      lines.push('[' + label + ']');
      lines.push(item[label].join('\n') + '\n');
    });
  });
 
  return lines.join('\n');
}
 
exports.format = format;