Skip to content

Commit

Permalink
Lint commands directory files.
Browse files Browse the repository at this point in the history
issue #71
  • Loading branch information
tzmanics committed Feb 24, 2016
1 parent da4b70a commit 288421a
Show file tree
Hide file tree
Showing 11 changed files with 1,255 additions and 1,337 deletions.
257 changes: 125 additions & 132 deletions lib/commands/addOn.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,81 @@
var modulus = require('../modulus'),
addOnController = require('../controllers/addOn'),
projectController = require('../controllers/project'),
projectCommands = require('../commands/project'),
async = require('async'),
userConfig = require('../common/api').userConfig,
error = require('../common/error'),
util = require('util');
const Async = require('async');
const Util = require('util');

const Modulus = require('../modulus');
const AddOnController = require('../controllers/addOn');
const ProjectController = require('../controllers/project');
const ProjectCommands = require('../commands/project');
const UserConfig = require('../common/api').userConfig;
const Errors = require('../common/error');

var addOn = {};
var print = Modulus.io.print;

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Gets a project via project name param or project prompt
//-----------------------------------------------------------------------------
addOn.getProject = function(projectName, callback) {
projectController.find({
userId : userConfig.data.userId
// ----------------------------------------------------------------------------
addOn.getProject = function (projectName, callback) {
ProjectController.find({
userId: UserConfig.data.userId
},
function(err, projects) {
if(err) {
err = error.handleApiError(err, 'GET_PROJECTS', callback);
function (err, projects) {
if (err) {
err = Errors.handleApiError(err, 'GET_PROJECTS', callback);

if(err.length > 0) {
return callback(err);
}
if (err.length > 0) return callback(err);
}

if(projects.length === 0) {
if (projects.length === 0) {
return callback('You currently have no projects. One can be created using the create command.');
}

projectCommands.chooseProjectPrompt(projects, projectName, function(err, result) {
if(err) {
return callback(err);
}

if(!result) {
return callback('You must select a project.');
}

callback(null, result);
});
ProjectCommands.chooseProjectPrompt(
projects, projectName, function (err, result) {
if (err) return callback(err);
if (!result) return callback('You must select a project.');
callback(null, result);
});
});
};

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Prompts to choose an addon from a list
//-----------------------------------------------------------------------------
addOn.chooseAddOnPrompt = function(addons, callback) {
if(addons.length === 0) {
callback('This project has no provisioned add-ons.');
// ----------------------------------------------------------------------------
addOn.chooseAddOnPrompt = function (addons, callback) {
var i;
if (addons.length === 0) {
return callback('This project has no provisioned add-ons.');
}

modulus.io.print('Please choose an add-on:'.input);
print('Please choose an add-on:'.input);

for(var a = 0; a < addons.length; a++) {
modulus.io.print((' ' + (a + 1) + ') ' + addons[a].addon_name).input);
for (i = 0; i < addons.length; ++i) {
print((' ' + (i + 1) + ') ' + addons[i].addon_name).input);
}

modulus.io.prompt.get([{
name : 'addon',
description : 'Addon?',
Modulus.io.prompt.get([{
name: 'addon',
description: 'Addon?',
type: 'number',
warning : 'Add-On choice has to be between 1 and ' + addons.length,
minimum : 1,
maximum : addons.length
}], function(err, result) {
if(err) {
return error.handlePromptError(err, callback);
}
warning: 'Add-On choice has to be between 1 and ' + addons.length,
minimum: 1,
maximum: addons.length
}], function (err, result) {
if (err) return Errors.handlePromptError(err, callback);

print(
Util.format('Selecting %s\n', addons[result.addon - 1].addon_name.data)
);

modulus.io.print(util.format('Selecting %s\n', addons[result.addon - 1].addon_name.data));
callback(null, addons[result.addon - 1]);
});
};

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Prompts to choose an add-on region from a list
//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
addOn.chooseRegionPrompt = function (addon, callback) {
var i;
if (!addon.regions || addon.regions.length === 0) {
return callback(null, '');
}
Expand All @@ -86,13 +84,13 @@ addOn.chooseRegionPrompt = function (addon, callback) {
return callback(null, addon.regions[0]);
}

modulus.io.print('Please choose a region:'.input);
print('Please choose a region:'.input);

for (var i = 0; i < addon.regions.length; i++) {
modulus.io.print((' ' + (i + 1) + ') ' + addon.regions[i]).input);
for (i = 0; i < addon.regions.length; ++i) {
print((' ' + (i + 1) + ') ' + addon.regions[i]).input);
}

modulus.io.prompt.get([
Modulus.io.prompt.get([
{
name: 'region',
description: 'Region?',
Expand All @@ -102,79 +100,82 @@ addOn.chooseRegionPrompt = function (addon, callback) {
maximum: addon.regions.length
}
], function (err, result) {
if (err) {
return error.handlePromptError(err, callback);
}

modulus.io.print(util.format('Select %s\n', addon.regions[result.region - 1].data));
callback(null, addon.regions[result.region - 1]);
});
if (err) return Errors.handlePromptError(err, callback);
print(Util.format('Select %s\n', addon.regions[result.region - 1].data));
callback(null, addon.regions[result.region - 1]);
});
};

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Prints a list of Add-Ons
//-----------------------------------------------------------------------------
addOn.printList = function(projectName, addons) {
modulus.io.print('Add-Ons provisioned for ' + projectName.verbose);
// ----------------------------------------------------------------------------
addOn.printList = function (projectName, addons) {
var i, addonConfig, configItem;
print('Add-Ons provisioned for ' + projectName.verbose);

if(addons instanceof Array === false || addons.length === 0) {
modulus.io.print('No Add-Ons provisioned.');
if (addons instanceof Array === false || addons.length === 0) {
print('No Add-Ons provisioned.');
} else {
for(var a = 0; a < addons.length; a++) {
modulus.io.print('--------------------------------------'.grey);
for (i = 0; i < addons.length; ++i) {
print('--------------------------------------'.grey);

// Capitalized name.
modulus.io.print(addons[a].addon_name.yellow);
print(addons[i].addon_name.yellow);

// Id
modulus.io.print(('Id: ' + addons[a].addon_id).grey);
print(('Id: ' + addons[i].addon_id).grey);

// Plan
modulus.io.print(('Plan: ' + addons[a].plan).grey);
print(('Plan: ' + addons[i].plan).grey);

// Config
for(var k in addons[a].config) {
modulus.io.print((k + ' = ' + addons[a].config[k]).grey);
addonConfig = addons[i].config;
for (configItem in addonConfig) {
if ({}.hasOwnProperty.call(addonConfig, configItem)) {
print((configItem + ' = ' + addonConfig[configItem]).grey);
}
}
}
}
};

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Gets the Add-Ons provided to a project.
//-----------------------------------------------------------------------------
addOn.getForProject = function(projectName, callback) {
// ----------------------------------------------------------------------------
addOn.getForProject = function (projectName, callback) {
var project = null;

async.waterfall([
function(cb) {
Async.waterfall([
function (cb) {
addOn.getProject(projectName, cb);
},

function(pro, cb) {
function (pro, cb) {
project = pro;
addOnController.getForProject(project.id, cb);
AddOnController.getForProject(project.id, cb);
},

function(addons, cb) {
//Newline
console.log();
function (addons, cb) {
print('\n');

addOn.printList(project.name, addons);
cb(null);
}
],
function(err) {
function (err) {
callback(err);
});
};

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Provisions the specified add-on for the specified project.
//-----------------------------------------------------------------------------
addOn.provision = function(projectName, addon, callback) {
// ----------------------------------------------------------------------------
addOn.provision = function (projectName, addon, callback) {
var project;
if (typeof addon !== 'string' || addon.length === 1) {
return callback('Please provide an add-on and plan. Use --help for the command format.');
return callback(
'Please provide an add-on and plan. Use --help for the command format.'
);
}

addon = addon.split(':');
Expand All @@ -183,87 +184,79 @@ addOn.provision = function(projectName, addon, callback) {
return callback('A plan is required to provision an add-on.');
}

var project, provisioned;
async.waterfall([
function(cb) {
Async.waterfall([
function (cb) {
addOn.getProject(projectName, cb);
},

function(pro, cb) {
function (pro, cb) {
project = pro;
addOnController.getById(addon[0], cb);
AddOnController.getById(addon[0], cb);
},

function (addonData, cb) {
addOn.chooseRegionPrompt(addonData, cb);
},

function (region, cb) {
addOnController.provision(project.creator, project.id, addon[0], addon[1], region, cb);
AddOnController.provision(
project.creator, project.id, addon[0], addon[1], region, cb
);
},

function(add, cb) {
provisioned = add;
addOnController.getForProject(project.id, cb);
function (add, cb) { // FIXME: ignores add;
AddOnController.getForProject(project.id, cb);
},

function(addons) {

//Newline
console.log();
function (addons) {
print('\n');

addOn.printList(project.name, addons);

console.log();
modulus.io.success('Add-On \'' + addon[0] + '\' provisioned.');
print();
Modulus.io.success('Add-On \'' + addon[0] + '\' provisioned.');
}
],
function(err) {
if (err) {
err = error.handleApiError(err, 'PROVISION_ADDON', callback);
}

if (err.length > 0) {
callback(err);
}
function (err) {
if (err) err = Errors.handleApiError(err, 'PROVISION_ADDON', callback);
if (err.length > 0) return callback(err);
});
};

addOn.deprovision = function(projectName, addOnId, callback) {

addOn.deprovision = function (projectName, addOnId, callback) {
var project, addon;
async.waterfall([
function(cb) {

Async.waterfall([
function (cb) {
addOn.getProject(projectName, cb);
},

function(pro, cb) {
function (pro, cb) {
project = pro;
addOnController.getForProject(project.id, cb);
AddOnController.getForProject(project.id, cb);
},

function(addons, cb) {
if(addOnId) {
for(var a = 0; a < addons.length; a++) {
if(addons[a].addon_id === addOnId) {
return cb(null, addons[a]);
function (addons, cb) {
var i;
if (addOnId) {
for (i = 0; i < addons.length; ++i) {
if (addons[i].addon_id === addOnId) {
return cb(null, addons[i]);
}
}
}

addOn.chooseAddOnPrompt(addons, cb);
},

function(add, cb) {
function (add, cb) {
addon = add;
addOnController.deprovision(project.id, addon.modulus_id, cb);
AddOnController.deprovision(project.id, addon.modulus_id, cb);
}
],
function(err) {
if(!err) {
modulus.io.print('Add-On ' + addon.addon_name.data + ' has been deprovisioned.');
}

function (err) {
var msg = 'Add-On ' + addon.addon_name.data + ' has been deprovisioned.';
if (!err) print(msg);
callback(err);
});
};
Expand Down
Loading

0 comments on commit 288421a

Please sign in to comment.