Skip to content

Instantly share code, notes, and snippets.

@cliftonc
Created December 3, 2014 06:49
Show Gist options
  • Save cliftonc/fa5a8416bd4156922fce to your computer and use it in GitHub Desktop.
Save cliftonc/fa5a8416bd4156922fce to your computer and use it in GitHub Desktop.
Example project specific bosco command (ports @ TES)
'use strict';
var _ = require('lodash');
var async = require('async');
var fs = require('fs');
var path = require('path');
var Table = require('cli-table');
var repoList = [], repoTable = [];
module.exports = {
name:'ports',
description:'Lists the ports that all Node services run on (by inspecting configuration)',
example:'bosco ports -r <repoPattern>',
cmd:cmd
};
function cmd(bosco, args) {
var repoPattern = bosco.options.repo;
var repoRegex = new RegExp(repoPattern);
var repos = bosco.config.get('github:repos');
var getRepoList = function(next) {
repos.forEach(function(repo) {
if (repo.match(repoRegex)) {
repoList.push(repo);
}
});
next();
};
var getPort = function(config) {
if(!bosco.exists(config)) return;
var cfg = require(config);
if(cfg.server && cfg.server.port) return cfg.server.port;
};
var checkDupe = function(port) {
var isDupe = false;
repoTable.forEach(function(item) {
if(item[1] == port) {
isDupe = true;
}
});
return isDupe ? "Yes" : "";
};
var getPorts = function(next) {
repoList.forEach(function(repo) {
var repoPath = bosco.getRepoPath(repo),
defaultConfig = [repoPath, "config", "default.json"].join("/"),
envConfig = [repoPath, "config", bosco.options.environment + ".json"].join("/");
var defaultPort = getPort(defaultConfig);
var envPort = getPort(envConfig);
var isDupe = checkDupe(envPort || defaultPort);
if(defaultPort || envPort) repoTable.push([repo, defaultPort, envPort || "", isDupe, "http://localhost:" + (envPort || defaultPort) + "/"]);
});
next(null);
};
var printPorts = function(next) {
var table = new Table({
chars: {'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''},
head: ['Service', 'Default', bosco.options.environment, 'Duplicate', 'Localhost Url'],
colWidths: [40, 10, 10, 10, 40]
});
repoTable.forEach(function(item) {
//console.dir(item);
table.push(item);
});
console.log(table.toString());
console.log("\r");
next(null);
};
bosco.log("Getting ports for all the little mircoservices ...");
async.series([getRepoList, getPorts, printPorts], function(err) {
process.exit(0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment