Code coverage report for lib/formatters/sshconfig.js

Statements: 100% (25 / 25)      Branches: 100% (12 / 12)      Functions: 100% (8 / 8)      Lines: 100% (25 / 25)      Ignored: none     

All files » lib/formatters/ » sshconfig.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 591                 1     6   6 9   6 9 7         6 6 9 9 7 7 7         6 3       6 6 7 7 7 6                   6     1
var _ = require('lodash'),
  util = require('util');
 
/**
 * Format configuration data into an SSH config
 * http://nerderati.com/2011/03/simplify-your-life-with-an-ssh-config-file/
 *
 * @param {Object} conf: Breaker configuration data
 */
function format(conf) {
 
  // get unique labels
  var labels = [],
    uniques = [];
  conf.forEach(function (item) {
    labels = labels.concat(item.labels);
  });
  labels.forEach(function (label) {
    if (labels.indexOf(label) === labels.lastIndexOf(label)) {
      uniques.push(label);
    }
  });
 
  // map each unique label to a host, then sort by label
  var data = [];
  conf.forEach(function (item) {
    item.labels.forEach(function (label) {
      if (uniques.indexOf(label) >= 0) {
        var map = {};
        map[label] = item;
        data.push(map);
      }
    });
  });
 
  data.sort(function (o1, o2) {
    return _.keys(o1)[0] > _.keys(o2)[0];
  });
 
  // format them as paragraphs
  var paragraphs = [];
  data.forEach(function (item) {
    var key = _.keys(item)[0];
    item[key].ssh = item[key].ssh || [];
    item[key].ssh.forEach(function (sshItem) {
      paragraphs.push(util.format('Host %s\n    HostName %s\n%s%s%s\n',
        key,
        item[key].host,
        ((sshItem.key) ? '    IdentityFile ' + sshItem.key + '\n' : ''),
        ((sshItem.user) ? '    User ' + sshItem.user + '\n' : ''),
        ((sshItem.port) ? '    Port ' + sshItem.port + '\n' : '')
        ));
    });
  });
 
  return paragraphs.join('\n');
}
 
exports.format = format;